mirror of
https://github.com/NVIDIA/nvidia-container-toolkit
synced 2024-11-26 13:59:45 +00:00
6e1436cefb
Signed-off-by: Evan Lezar <elezar@nvidia.com>
38 lines
831 B
Go
38 lines
831 B
Go
package errutil
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
)
|
|
|
|
// NewJSONFormatter create JSONFormatter instance
|
|
func NewJSONFormatter() *JSONFormatter {
|
|
return &JSONFormatter{}
|
|
}
|
|
|
|
// JSONFormatter used to format error to json
|
|
type JSONFormatter struct{}
|
|
|
|
// Format error to json
|
|
func (t *JSONFormatter) Format(errin error) (errtext string, err error) {
|
|
return t.FormatSkip(errin, 1)
|
|
}
|
|
|
|
// FormatSkip trace error line and format to json
|
|
func (t *JSONFormatter) FormatSkip(errin error, skip int) (errtext string, err error) {
|
|
errjson, err := newJSON(skip+1, errin)
|
|
if errjson == nil || err != nil {
|
|
return "", err
|
|
}
|
|
|
|
buffer := &bytes.Buffer{}
|
|
if err = json.NewEncoder(buffer).Encode(errjson); err != nil {
|
|
return
|
|
}
|
|
|
|
return buffer.String(), nil
|
|
}
|
|
|
|
var _ ErrorFormatter = &JSONFormatter{}
|
|
var _ TraceFormatter = &JSONFormatter{}
|