sq/cli/output/tablew/errorwriter.go

69 lines
1.3 KiB
Go
Raw Normal View History

2020-08-06 20:58:47 +03:00
package tablew
import (
"bytes"
2020-08-06 20:58:47 +03:00
"fmt"
"io"
"strings"
2020-08-06 20:58:47 +03:00
"github.com/neilotoole/sq/cli/output"
"github.com/neilotoole/sq/libsq/core/errz"
2020-08-06 20:58:47 +03:00
)
// errorWriter implements output.ErrorWriter.
type errorWriter struct {
w io.Writer
pr *output.Printing
2020-08-06 20:58:47 +03:00
}
// 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}
2020-08-06 20:58:47 +03:00
}
// 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)
2020-08-06 20:58:47 +03:00
}