Skip to content

Commit 82b8a6a

Browse files
committed
feat: added HandleError function to Handler interface
1 parent 3ffe037 commit 82b8a6a

File tree

1 file changed

+54
-20
lines changed

1 file changed

+54
-20
lines changed

http/handler/handler.go

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
11
package handler
22

33
import (
4+
"errors"
45
goflagsmode "github.com/ralvarezdev/go-flags/mode"
56
gonethttpjson "github.com/ralvarezdev/go-net/http/json"
67
gonethttpresponse "github.com/ralvarezdev/go-net/http/response"
7-
gonethttperrors "github.com/ralvarezdev/go-net/http/status/errors"
8+
gonethttpstatuserrors "github.com/ralvarezdev/go-net/http/status/errors"
89
"net/http"
910
)
1011

1112
type (
1213
// Handler interface for handling the responses
1314
Handler interface {
14-
HandleRequest(
15+
Decode(
1516
w http.ResponseWriter,
1617
r *http.Request,
17-
body interface{},
18+
dest interface{},
1819
) error
19-
HandleValidations(
20+
Validate(
2021
w http.ResponseWriter,
2122
body interface{},
2223
validatorFn func(body interface{}) (interface{}, error),
2324
) bool
24-
HandleRequestAndValidations(
25+
DecodeAndValidate(
2526
w http.ResponseWriter,
2627
r *http.Request,
27-
body interface{},
28+
dest interface{},
2829
validatorFn func(body interface{}) (interface{}, error),
2930
) bool
31+
HandleError(
32+
w http.ResponseWriter,
33+
err error,
34+
httpStatus int,
35+
)
3036
HandleResponse(
3137
w http.ResponseWriter,
3238
response *gonethttpresponse.Response,
@@ -70,17 +76,17 @@ func NewDefaultHandler(
7076
}, nil
7177
}
7278

73-
// HandleRequest handles the request
74-
func (d *DefaultHandler) HandleRequest(
79+
// Decode decodes the request body into the destination
80+
func (d *DefaultHandler) Decode(
7581
w http.ResponseWriter,
7682
r *http.Request,
77-
body interface{},
83+
dest interface{},
7884
) (err error) {
79-
return d.jsonDecoder.Decode(w, r, body)
85+
return d.jsonDecoder.Decode(w, r, dest)
8086
}
8187

82-
// HandleValidations handles the validations
83-
func (d *DefaultHandler) HandleValidations(
88+
// Validate validates the request body
89+
func (d *DefaultHandler) Validate(
8490
w http.ResponseWriter,
8591
body interface{},
8692
validatorFn func(body interface{}) (interface{}, error),
@@ -98,7 +104,7 @@ func (d *DefaultHandler) HandleValidations(
98104
d.HandleResponse(
99105
w,
100106
gonethttpresponse.NewDebugErrorResponse(
101-
gonethttperrors.InternalServerError,
107+
gonethttpstatuserrors.InternalServerError,
102108
err,
103109
nil, nil,
104110
http.StatusInternalServerError,
@@ -117,20 +123,48 @@ func (d *DefaultHandler) HandleValidations(
117123
return false
118124
}
119125

120-
// HandleRequestAndValidations handles the request and the validations
121-
func (d *DefaultHandler) HandleRequestAndValidations(
126+
// DecodeAndValidate decodes and validates the request body
127+
func (d *DefaultHandler) DecodeAndValidate(
122128
w http.ResponseWriter,
123129
r *http.Request,
124130
body interface{},
125131
validatorFn func(body interface{}) (interface{}, error),
126132
) bool {
127-
// Handle the request
128-
if err := d.HandleRequest(w, r, body); err != nil {
133+
// Decode the request body
134+
if err := d.Decode(w, r, body); err != nil {
129135
return false
130136
}
131137

132-
// Handle the validations
133-
return d.HandleValidations(w, body, validatorFn)
138+
// Validate the request body
139+
return d.Validate(w, body, validatorFn)
140+
}
141+
142+
// HandleError handles the error response
143+
func (d *DefaultHandler) HandleError(
144+
w http.ResponseWriter,
145+
err error,
146+
httpStatus int,
147+
) {
148+
// Check if the errors is a request error
149+
var e gonethttpresponse.RequestError
150+
if errors.As(err, &e) {
151+
d.HandleResponse(
152+
w, gonethttpresponse.NewFailResponse(
153+
gonethttpresponse.NewRequestErrorsBodyData(e),
154+
nil,
155+
httpStatus,
156+
),
157+
)
158+
return
159+
}
160+
161+
d.HandleResponse(
162+
w, gonethttpresponse.NewDebugErrorResponse(
163+
gonethttpstatuserrors.InternalServerError,
164+
err,
165+
nil, nil, http.StatusInternalServerError,
166+
),
167+
)
134168
}
135169

136170
// HandleResponse handles the response
@@ -143,7 +177,7 @@ func (d *DefaultHandler) HandleResponse(
143177
d.HandleResponse(
144178
w,
145179
gonethttpresponse.NewDebugErrorResponse(
146-
gonethttperrors.InternalServerError,
180+
gonethttpstatuserrors.InternalServerError,
147181
gonethttpresponse.ErrNilResponse,
148182
nil, nil,
149183
http.StatusInternalServerError,

0 commit comments

Comments
 (0)