Skip to content

Commit 440cef2

Browse files
authored
create json serializable error messages (#26)
1 parent 35fec87 commit 440cef2

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

must/must.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,28 @@ func (x *Error) Wrapped() error {
2323
return x.wrapped
2424
}
2525

26+
type ErrorString struct {
27+
Msg string `json:"msg"`
28+
}
29+
30+
func (x ErrorString) Error() string {
31+
return x.Msg
32+
}
33+
2634
func mkErr(ctx context.Context, wrapped error) *Error {
27-
return &Error{Ctx: ctx, Stack: debug.Stack(), wrapped: wrapped}
35+
return &Error{
36+
Ctx: ctx,
37+
Stack: debug.Stack(),
38+
wrapped: wrapped,
39+
}
2840
}
2941

3042
func Panic(ctx context.Context, err error) {
3143
panic(mkErr(ctx, err))
3244
}
3345

3446
func Errorf(ctx context.Context, format string, args ...any) {
35-
Panic(ctx, fmt.Errorf(format, args...))
47+
Panic(ctx, ErrorString{Msg: fmt.Sprintf(format, args...)})
3648
}
3749

3850
func Assert(ctx context.Context, cond bool, err error) {

0 commit comments

Comments
 (0)