-
Notifications
You must be signed in to change notification settings - Fork 5
/
stacktrace.go
74 lines (63 loc) · 2.14 KB
/
stacktrace.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package raml
import (
"errors"
"fmt"
"strings"
"github.com/acronis/go-stacktrace"
"gopkg.in/yaml.v3"
)
// NewNodePosition creates a new position from the given node.
func NewNodePosition(node *yaml.Node) *stacktrace.Position {
return &stacktrace.Position{Line: node.Line, Column: node.Column}
}
// optErrNodePosition is an option to set the position of the error to the position of the given node.
type optErrNodePosition struct {
pos *stacktrace.Position
}
// Apply sets the position of the error to the given position.
// implements Option
func (o optErrNodePosition) Apply(e *stacktrace.StackTrace) {
e.Position = o.pos
}
// WithNodePosition sets the position of the error to the position of the given node.
func WithNodePosition(node *yaml.Node) stacktrace.Option {
return optErrNodePosition{pos: NewNodePosition(node)}
}
// GetYamlError returns the yaml type error from the given error.
// nil if the error is not a yaml type error.
func GetYamlError(err error) *yaml.TypeError {
var yamlError *yaml.TypeError
if errors.As(err, &yamlError) {
return yamlError
}
wErr := errors.Unwrap(err)
if wErr == nil {
return nil
}
if yamlErr := GetYamlError(wErr); yamlErr != nil {
toAppend := strings.ReplaceAll(err.Error(), yamlErr.Error(), "")
toAppend = strings.TrimSuffix(toAppend, ": ")
// insert the error message in the correct order to the first index
yamlErr.Errors = append([]string{toAppend}, yamlErr.Errors...)
return yamlErr
}
return nil
}
// FixYamlError fixes the yaml type error from the given error.
func FixYamlError(err error) error {
if err == nil {
return nil
}
if yamlError := GetYamlError(err); yamlError != nil {
err = fmt.Errorf("%s", strings.Join(yamlError.Errors, ": "))
}
return err
}
func StacktraceNewWrapped(msg string, err error, location string, opts ...stacktrace.Option) *stacktrace.StackTrace {
opts = append(opts, stacktrace.WithLocation(location))
return stacktrace.NewWrapped(msg, FixYamlError(err), opts...)
}
func StacktraceNew(msg string, location string, opts ...stacktrace.Option) *stacktrace.StackTrace {
opts = append(opts, stacktrace.WithLocation(location))
return stacktrace.New(msg, opts...)
}