sq/cli/output/tablew/errorwriter.go
Neil O'Toole db55986980
#307: Ingest cache (#354)
- Support for ingest cache, download cache, and progress bars.
2024-01-14 18:45:34 -07:00

69 lines
1.3 KiB
Go

package tablew
import (
"bytes"
"fmt"
"io"
"strings"
"github.com/neilotoole/sq/cli/output"
"github.com/neilotoole/sq/libsq/core/errz"
)
// errorWriter implements output.ErrorWriter.
type errorWriter struct {
w io.Writer
pr *output.Printing
}
// NewErrorWriter returns an output.ErrorWriter that
// outputs in text format.
func NewErrorWriter(w io.Writer, pr *output.Printing) output.ErrorWriter {
return &errorWriter{w: w, pr: pr}
}
// Error implements output.ErrorWriter.
func (w *errorWriter) Error(systemErr, humanErr error) {
fmt.Fprintln(w.w, w.pr.Error.Sprintf("sq: %v", humanErr))
if !w.pr.Verbose {
return
}
stacks := errz.Stacks(systemErr)
if len(stacks) == 0 {
return
}
buf := &bytes.Buffer{}
var count int
for _, stack := range stacks {
if stack == nil {
continue
}
stackPrint := fmt.Sprintf("%+v", stack.Frames)
stackPrint = strings.ReplaceAll(strings.TrimSpace(stackPrint), "\n\t", "\n ")
if stackPrint == "" {
continue
}
if count > 0 {
buf.WriteString("\n")
}
if stack.Error != nil {
w.pr.StackErrorType.Fprintln(buf, errz.SprintTreeTypes(stack.Error))
w.pr.StackError.Fprintln(buf, stack.Error.Error())
}
lines := strings.Split(stackPrint, "\n")
for _, line := range lines {
w.pr.Stack.Fprint(buf, line)
buf.WriteByte('\n')
}
count++
}
_, _ = buf.WriteTo(w.w)
}