@@ -59,6 +59,21 @@ func (e *ErrorX) append(errs ...error) {
5959 }
6060}
6161
62+ func (e ErrorX ) MarshalJSON () ([]byte , error ) {
63+ tmp := []string {}
64+ for _ , err := range e .errs {
65+ tmp = append (tmp , err .Error ())
66+ }
67+ m := map [string ]interface {}{
68+ "kind" : e .kind .String (),
69+ "errors" : tmp ,
70+ }
71+ if len (e .attrs ) > 0 {
72+ m ["attrs" ] = slog .GroupValue (maps .Values (e .attrs )... )
73+ }
74+ return json .Marshal (m )
75+ }
76+
6277// Errors returns all errors parsed by the error
6378func (e * ErrorX ) Errors () []error {
6479 return e .errs
@@ -97,18 +112,6 @@ func (e *ErrorX) Is(err error) bool {
97112 return false
98113}
99114
100- // MarshalJSON returns the json representation of the error
101- func (e * ErrorX ) MarshalJSON () ([]byte , error ) {
102- m := map [string ]interface {}{
103- "kind" : e .kind .String (),
104- "errors" : e .errs ,
105- }
106- if len (e .attrs ) > 0 {
107- m ["attrs" ] = slog .GroupValue (maps .Values (e .attrs )... )
108- }
109- return json .Marshal (m )
110- }
111-
112115// Error returns the error string
113116func (e * ErrorX ) Error () string {
114117 var sb strings.Builder
@@ -158,7 +161,9 @@ func FromError(err error) *ErrorX {
158161
159162// New creates a new error with the given message
160163func New (format string , args ... interface {}) * ErrorX {
161- return & ErrorX {errs : []error {fmt .Errorf (format , args ... )}}
164+ e := & ErrorX {}
165+ e .append (fmt .Errorf (format , args ... ))
166+ return e
162167}
163168
164169// Msgf adds a message to the error
0 commit comments