Skip to content

Commit cd32441

Browse files
authored
Merge pull request #93 from gone-io/feature/add-warap-err
feat: add error wrapping and unwrapping support
2 parents 51d888c + aee09f3 commit cd32441

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

default.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ func Run(fn ...any) {
3434

3535
// Serve starts all daemons and waits for termination signal using the default application instance.
3636
// This function will start all registered daemons and block until a shutdown signal is received.
37-
func Serve() {
38-
Default.Serve()
37+
func Serve(fn ...any) {
38+
Default.Serve(fn...)
3939
}
4040

4141
// End triggers application termination

error.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ type Error interface {
1515
Code() int
1616

1717
GetStatusCode() int
18+
19+
Unwrap() error
1820
}
1921

2022
// InnerError which has stack, and which is used for Internal error
@@ -35,6 +37,10 @@ type BError struct {
3537
data any
3638
}
3739

40+
func (e *BError) Unwrap() error {
41+
return e.err
42+
}
43+
3844
func (e *BError) SetMsg(msg string) {
3945
e.err.SetMsg(msg)
4046
}
@@ -60,6 +66,11 @@ type defaultErr struct {
6066
code int
6167
msg string
6268
statusCode int
69+
cause error
70+
}
71+
72+
func (e *defaultErr) Unwrap() error {
73+
return e.cause
6374
}
6475

6576
func (e *defaultErr) Error() string {
@@ -193,6 +204,16 @@ func ToErrorf(input any, format string, params ...any) error {
193204
return ToErrorWithMsg(input, fmt.Sprintf(format, params...))
194205
}
195206

207+
// WrapError wraps any input error with an additional message prefix.
208+
// Parameters:
209+
//
210+
// input: any input error or message,
211+
// format: format string for the message,
212+
// params: parameters to format the message string
213+
func WrapError(input any, format string, params ...any) Error {
214+
return ToErrorWithMsg(input, fmt.Sprintf(format, params...))
215+
}
216+
196217
type iError struct {
197218
*defaultErr
198219
trace []byte

error_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,3 +381,50 @@ func TestToErrorf(t *testing.T) {
381381

382382
})
383383
}
384+
385+
func TestWrapError(t *testing.T) {
386+
t.Run("wrap err", func(t *testing.T) {
387+
tests := []struct {
388+
name string
389+
err error
390+
}{
391+
{
392+
name: "wrap normal error",
393+
err: fmt.Errorf("normal error"),
394+
},
395+
{
396+
name: "wrap gone InnerError",
397+
err: NewInnerError("test error", http.StatusInternalServerError),
398+
},
399+
{
400+
name: "wrap gone BusinessError",
401+
err: NewBusinessError("test error", http.StatusOK),
402+
},
403+
{
404+
name: "wrap gone ParameterError",
405+
err: NewParameterError("test error", http.StatusBadRequest),
406+
},
407+
{
408+
name: "wrap gone Error",
409+
err: NewError(1101, "test error", http.StatusBadRequest),
410+
},
411+
}
412+
413+
for _, tt := range tests {
414+
t.Run(tt.name, func(t *testing.T) {
415+
wrapError := WrapError(tt.err, "wrap error")
416+
if !errors.Is(wrapError, tt.err) {
417+
t.Error("must be input error")
418+
}
419+
})
420+
}
421+
})
422+
t.Run("wrap none error", func(t *testing.T) {
423+
wrapError := WrapError("some thing", "wrap error")
424+
425+
msg := wrapError.Msg()
426+
if msg != "wrap error" {
427+
t.Error("must be wrap error")
428+
}
429+
})
430+
}

0 commit comments

Comments
 (0)