Skip to content

Commit

Permalink
Pull request #70: Fixed the LogErrorFuncWithRequestInfo to return the…
Browse files Browse the repository at this point in the history
… error in order to pass the error to the global error handling mechanism

Merge in AW/go-starter from ek/panic-recover-error-handler-func to master

* commit '12d9c2818eabe998f881848f918bd73747dbc4d3':
  added test for recover log error func
  Fixed the LogErrorFuncWithRequestInfo to return the error in order to pass the error to the global error handling mechanism
  • Loading branch information
eklatzer committed May 28, 2024
2 parents 8e3374d + 12d9c28 commit 4f1edc0
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Please follow the update process in *[I just want to update / upgrade my project!](https://github.com/allaboutapps/go-starter/wiki/FAQ#i-just-want-to-update--upgrade-my-project)*.

## Unreleased
- Fixes the `LogErrorFuncWithRequestInfo` to return the error in order to pass the error to the global error handling mechanism

## 2024-05-14
- Switch [from Go 1.21.6 Go 1.21.10](https://go.dev/doc/devel/release#go1.21.0) (requires `./docker-helper.sh --rebuild`).
Expand Down
4 changes: 2 additions & 2 deletions internal/api/middleware/recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"github.com/labstack/echo/v4"
)

func LogErrorFuncWithRequestInfoFunc(c echo.Context, err error, stack []byte) error {
func LogErrorFuncWithRequestInfo(c echo.Context, err error, stack []byte) error {
log := util.LogFromContext(c.Request().Context())

log.Error().Err(err).Bytes("stack", stack).Msg("PANIC RECOVER")

return nil
return err
}
42 changes: 42 additions & 0 deletions internal/api/middleware/recover_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package middleware_test

import (
"fmt"
"io"
"net/http"
"testing"

"allaboutapps.dev/aw/go-starter/internal/api"
"allaboutapps.dev/aw/go-starter/internal/api/middleware"
"allaboutapps.dev/aw/go-starter/internal/test"
"github.com/labstack/echo/v4"
echoMiddleware "github.com/labstack/echo/v4/middleware"

"github.com/stretchr/testify/require"
)

func TestLogErrorFuncWithRequestInfo(t *testing.T) {
test.WithTestServer(t, func(s *api.Server) {
path := "/testing-e87bc94c-2d1f-4342-9ec2-f158c63ac6da"

s.Echo.Use(echoMiddleware.RecoverWithConfig(echoMiddleware.RecoverConfig{
LogErrorFunc: middleware.LogErrorFuncWithRequestInfo,
}))

s.Echo.POST(path, func(c echo.Context) error {

var val *int
fmt.Println(*val)

return c.NoContent(http.StatusNoContent)
})

res := test.PerformRequest(t, s, "POST", path, nil, nil)
require.Equal(t, http.StatusInternalServerError, res.Result().StatusCode)

body, err := io.ReadAll(res.Body)
require.NoError(t, err)

test.Snapshoter.SaveString(t, string(body))
})
}
2 changes: 1 addition & 1 deletion internal/api/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func Init(s *api.Server) {

if s.Config.Echo.EnableRecoverMiddleware {
s.Echo.Use(echoMiddleware.RecoverWithConfig(echoMiddleware.RecoverConfig{
LogErrorFunc: middleware.LogErrorFuncWithRequestInfoFunc,
LogErrorFunc: middleware.LogErrorFuncWithRequestInfo,
}))
} else {
log.Warn().Msg("Disabling recover middleware due to environment config")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"status":500,"title":"Internal Server Error","type":"generic"}

0 comments on commit 4f1edc0

Please sign in to comment.