2020-08-06 20:58:47 +03:00
|
|
|
package tablew
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2023-07-27 07:19:11 +03:00
|
|
|
"strings"
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/neilotoole/sq/cli/output"
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/errz"
|
2020-08-06 20:58:47 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// errorWriter implements output.ErrorWriter.
|
|
|
|
type errorWriter struct {
|
2023-04-22 06:36:32 +03:00
|
|
|
w io.Writer
|
|
|
|
pr *output.Printing
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewErrorWriter returns an output.ErrorWriter that
|
|
|
|
// outputs in text format.
|
2023-04-22 06:36:32 +03:00
|
|
|
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(err error) {
|
2023-04-22 06:36:32 +03:00
|
|
|
fmt.Fprintln(w.w, w.pr.Error.Sprintf("sq: %v", err))
|
2023-07-27 07:19:11 +03:00
|
|
|
if !w.pr.Verbose {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
stacks := errz.Stack(err)
|
|
|
|
for i, stack := range stacks {
|
|
|
|
if i > 0 {
|
|
|
|
fmt.Fprintln(w.w)
|
|
|
|
}
|
|
|
|
|
|
|
|
s := fmt.Sprintf("%+v", stack)
|
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
w.pr.Faint.Fprintln(w.w, s)
|
|
|
|
}
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|