From 309a859e645a99389986c7b5a5d4c10c8907b07c Mon Sep 17 00:00:00 2001 From: Matias Navarro Carter Date: Thu, 18 Aug 2022 04:15:09 +0100 Subject: [PATCH] fix: improve the code --- function.go | 6 ++---- handler_test.go | 13 +++++++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/function.go b/function.go index 1e49518..8f3d007 100644 --- a/function.go +++ b/function.go @@ -34,11 +34,9 @@ func reflectFunc(fn any) *reflectedFn { case 1: if t.Out(0).Implements(errorType) { rFn.outFn = func(out []reflect.Value) (interface{}, error) { - if err, ok := out[0].Interface().(error); ok { - return nil, err - } + err, _ := out[0].Interface().(error) - return nil, nil + return nil, err } } else { rFn.outFn = func(out []reflect.Value) (interface{}, error) { diff --git a/handler_test.go b/handler_test.go index 179594b..be82363 100644 --- a/handler_test.go +++ b/handler_test.go @@ -145,6 +145,19 @@ func TestHandler(t *testing.T) { expectedResponse: []byte(""), expectedStatus: http.StatusNoContent, }, + { + name: "double nil error response", + req: func() *http.Request { + return httptest.NewRequest(http.MethodGet, "https://example.com", mustOpen(t, "valid.json")) + }(), + handler: func() (map[string]interface{}, error) { + return map[string]interface{}{ + "msg": "hello", + }, nil + }, + expectedResponse: []byte(`{"msg":"hello"}` + "\n"), + expectedStatus: http.StatusOK, + }, { name: "panic response", req: func() *http.Request {