Skip to content

Commit dbc4b44

Browse files
authored
feat(httpserver): Updated error handler (#312)
1 parent 6ccae49 commit dbc4b44

File tree

4 files changed

+62
-48
lines changed

4 files changed

+62
-48
lines changed

httpserver/.golangci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ linters:
3838
- importas
3939
- ineffassign
4040
- interfacebloat
41-
- logrlint
41+
- loggercheck
4242
- maintidx
4343
- makezero
4444
- misspell

httpserver/error.go

+18-4
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,23 @@ import (
88
"github.com/labstack/echo/v4"
99
)
1010

11-
// JsonErrorHandler is an [echo.HTTPErrorHandler] that outputs errors in JSON format.
11+
// JsonErrorHandler provides a [echo.HTTPErrorHandler] that outputs errors in JSON format.
1212
// It can also be configured to obfuscate error message (to avoid to leak sensitive details), and to add the error stack to the response.
13-
func JsonErrorHandler(obfuscate bool, stack bool) echo.HTTPErrorHandler {
13+
type JsonErrorHandler struct {
14+
obfuscate bool
15+
stack bool
16+
}
17+
18+
// NewJsonErrorHandler returns a new JsonErrorHandler instance.
19+
func NewJsonErrorHandler(obfuscate bool, stack bool) *JsonErrorHandler {
20+
return &JsonErrorHandler{
21+
obfuscate: obfuscate,
22+
stack: stack,
23+
}
24+
}
25+
26+
// Handle handles errors.
27+
func (h *JsonErrorHandler) Handle() echo.HTTPErrorHandler {
1428
return func(err error, c echo.Context) {
1529
logger := log.CtxLogger(c.Request().Context())
1630

@@ -35,7 +49,7 @@ func JsonErrorHandler(obfuscate bool, stack bool) echo.HTTPErrorHandler {
3549

3650
var logRespFields map[string]interface{}
3751

38-
if stack {
52+
if h.stack {
3953
errStack := "n/a"
4054
if err != nil {
4155
errStack = errors.New(err).ErrorStack()
@@ -70,7 +84,7 @@ func JsonErrorHandler(obfuscate bool, stack bool) echo.HTTPErrorHandler {
7084

7185
httpRespFields := logRespFields
7286

73-
if obfuscate {
87+
if h.obfuscate {
7488
httpRespFields["message"] = http.StatusText(httpError.Code)
7589
}
7690

httpserver/error_test.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestErrorHandling(t *testing.T) {
2525

2626
httpServer := echo.New()
2727
httpServer.Logger = httpserver.NewEchoLogger(logger)
28-
httpServer.HTTPErrorHandler = httpserver.JsonErrorHandler(false, false)
28+
httpServer.HTTPErrorHandler = httpserver.NewJsonErrorHandler(false, false).Handle()
2929

3030
httpServer.GET("/test", func(c echo.Context) error {
3131
return fmt.Errorf("custom error")
@@ -57,7 +57,7 @@ func TestErrorHandlingWithObfuscate(t *testing.T) {
5757

5858
httpServer := echo.New()
5959
httpServer.Logger = httpserver.NewEchoLogger(logger)
60-
httpServer.HTTPErrorHandler = httpserver.JsonErrorHandler(true, false)
60+
httpServer.HTTPErrorHandler = httpserver.NewJsonErrorHandler(true, false).Handle()
6161

6262
httpServer.GET("/test", func(c echo.Context) error {
6363
return fmt.Errorf("custom error")
@@ -89,7 +89,7 @@ func TestErrorHandlingWithStack(t *testing.T) {
8989

9090
httpServer := echo.New()
9191
httpServer.Logger = httpserver.NewEchoLogger(logger)
92-
httpServer.HTTPErrorHandler = httpserver.JsonErrorHandler(false, true)
92+
httpServer.HTTPErrorHandler = httpserver.NewJsonErrorHandler(false, true).Handle()
9393

9494
httpServer.GET("/test", func(c echo.Context) error {
9595
return fmt.Errorf("custom error")
@@ -123,7 +123,7 @@ func TestErrorHandlingWithObfuscateAndStack(t *testing.T) {
123123

124124
httpServer := echo.New()
125125
httpServer.Logger = httpserver.NewEchoLogger(logger)
126-
httpServer.HTTPErrorHandler = httpserver.JsonErrorHandler(true, true)
126+
httpServer.HTTPErrorHandler = httpserver.NewJsonErrorHandler(true, true).Handle()
127127

128128
httpServer.GET("/test", func(c echo.Context) error {
129129
return fmt.Errorf("custom error")
@@ -157,7 +157,7 @@ func TestErrorHandlingWithHeadRequest(t *testing.T) {
157157

158158
httpServer := echo.New()
159159
httpServer.Logger = httpserver.NewEchoLogger(logger)
160-
httpServer.HTTPErrorHandler = httpserver.JsonErrorHandler(false, false)
160+
httpServer.HTTPErrorHandler = httpserver.NewJsonErrorHandler(false, false).Handle()
161161

162162
httpServer.HEAD("/test", func(c echo.Context) error {
163163
return fmt.Errorf("custom error")
@@ -189,7 +189,7 @@ func TestErrorHandlingWithAlreadyCommittedResponse(t *testing.T) {
189189

190190
httpServer := echo.New()
191191
httpServer.Logger = httpserver.NewEchoLogger(logger)
192-
httpServer.HTTPErrorHandler = httpserver.JsonErrorHandler(false, false)
192+
httpServer.HTTPErrorHandler = httpserver.NewJsonErrorHandler(false, false).Handle()
193193

194194
httpServer.GET("/test", func(c echo.Context) error {
195195
err := fmt.Errorf("custom error")
@@ -224,7 +224,7 @@ func TestErrorHandlingWithHttpError(t *testing.T) {
224224

225225
httpServer := echo.New()
226226
httpServer.Logger = httpserver.NewEchoLogger(logger)
227-
httpServer.HTTPErrorHandler = httpserver.JsonErrorHandler(false, false)
227+
httpServer.HTTPErrorHandler = httpserver.NewJsonErrorHandler(false, false).Handle()
228228

229229
httpServer.GET("/test", func(c echo.Context) error {
230230
return echo.NewHTTPError(http.StatusBadRequest, "bad request error")
@@ -256,7 +256,7 @@ func TestErrorHandlingWithWrappedError(t *testing.T) {
256256

257257
httpServer := echo.New()
258258
httpServer.Logger = httpserver.NewEchoLogger(logger)
259-
httpServer.HTTPErrorHandler = httpserver.JsonErrorHandler(false, false)
259+
httpServer.HTTPErrorHandler = httpserver.NewJsonErrorHandler(false, false).Handle()
260260

261261
httpServer.GET("/test", func(c echo.Context) error {
262262
return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("wrapped error"))
@@ -288,7 +288,7 @@ func TestErrorHandlingWithWrappedErrorWithStack(t *testing.T) {
288288

289289
httpServer := echo.New()
290290
httpServer.Logger = httpserver.NewEchoLogger(logger)
291-
httpServer.HTTPErrorHandler = httpserver.JsonErrorHandler(false, true)
291+
httpServer.HTTPErrorHandler = httpserver.NewJsonErrorHandler(false, true).Handle()
292292

293293
httpServer.GET("/test", func(c echo.Context) error {
294294
return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("wrapped error"))
@@ -322,7 +322,7 @@ func TestErrorHandlingWithWrappedErrorWithObfuscateAndStack(t *testing.T) {
322322

323323
httpServer := echo.New()
324324
httpServer.Logger = httpserver.NewEchoLogger(logger)
325-
httpServer.HTTPErrorHandler = httpserver.JsonErrorHandler(true, true)
325+
httpServer.HTTPErrorHandler = httpserver.NewJsonErrorHandler(true, true).Handle()
326326

327327
httpServer.GET("/test", func(c echo.Context) error {
328328
return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("wrapped error"))
@@ -356,7 +356,7 @@ func TestErrorHandlingWithHttpErrorWithStack(t *testing.T) {
356356

357357
httpServer := echo.New()
358358
httpServer.Logger = httpserver.NewEchoLogger(logger)
359-
httpServer.HTTPErrorHandler = httpserver.JsonErrorHandler(false, true)
359+
httpServer.HTTPErrorHandler = httpserver.NewJsonErrorHandler(false, true).Handle()
360360

361361
httpServer.GET("/test", func(c echo.Context) error {
362362
return echo.NewHTTPError(http.StatusBadRequest, "bad request error")
@@ -390,7 +390,7 @@ func TestErrorHandlingWithHttpErrorWithObfuscateAndStack(t *testing.T) {
390390

391391
httpServer := echo.New()
392392
httpServer.Logger = httpserver.NewEchoLogger(logger)
393-
httpServer.HTTPErrorHandler = httpserver.JsonErrorHandler(true, true)
393+
httpServer.HTTPErrorHandler = httpserver.NewJsonErrorHandler(true, true).Handle()
394394

395395
httpServer.GET("/test", func(c echo.Context) error {
396396
return echo.NewHTTPError(http.StatusBadRequest, "bad request error")
@@ -424,7 +424,7 @@ func TestErrorHandlingWithInternalHttpError(t *testing.T) {
424424

425425
httpServer := echo.New()
426426
httpServer.Logger = httpserver.NewEchoLogger(logger)
427-
httpServer.HTTPErrorHandler = httpserver.JsonErrorHandler(false, false)
427+
httpServer.HTTPErrorHandler = httpserver.NewJsonErrorHandler(false, false).Handle()
428428

429429
httpServer.GET("/test", func(c echo.Context) error {
430430
internalError := echo.NewHTTPError(http.StatusInternalServerError, "internal error")
@@ -462,7 +462,7 @@ func TestErrorHandlingWithInternalHttpErrorWithStack(t *testing.T) {
462462

463463
httpServer := echo.New()
464464
httpServer.Logger = httpserver.NewEchoLogger(logger)
465-
httpServer.HTTPErrorHandler = httpserver.JsonErrorHandler(false, true)
465+
httpServer.HTTPErrorHandler = httpserver.NewJsonErrorHandler(false, true).Handle()
466466

467467
httpServer.GET("/test", func(c echo.Context) error {
468468
internalError := echo.NewHTTPError(http.StatusInternalServerError, "internal error")
@@ -502,7 +502,7 @@ func TestErrorHandlingWithInternalHttpErrorWithObfuscateAndStack(t *testing.T) {
502502

503503
httpServer := echo.New()
504504
httpServer.Logger = httpserver.NewEchoLogger(logger)
505-
httpServer.HTTPErrorHandler = httpserver.JsonErrorHandler(true, true)
505+
httpServer.HTTPErrorHandler = httpserver.NewJsonErrorHandler(true, true).Handle()
506506

507507
httpServer.GET("/test", func(c echo.Context) error {
508508
internalError := echo.NewHTTPError(http.StatusInternalServerError, "internal error")

0 commit comments

Comments
 (0)