samber / slog-fiber

🚨 Fiber middleware for slog logger
https://pkg.go.dev/github.com/samber/slog-fiber
MIT License
53 stars 9 forks source link

Panic when an 4xx-5xx status is returned without an error #12

Closed griggsca91 closed 11 months ago

griggsca91 commented 11 months ago

I encountered a panic when a route returns a status of 4xx or 5xx but not an error.

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x18 pc=0x104989910]

goroutine 7 [running]:
main.main.New.NewWithConfig.func10(0x14000014600?)
    /Users/chris/Documents/slog-fiber/middleware.go:194 +0x1490
github.com/gofiber/fiber/v2.(*App).next(0x14000140a00, 0x14000014600)
    /Users/chris/go/pkg/mod/github.com/gofiber/fiber/v2@v2.50.0/router.go:145 +0x188
github.com/gofiber/fiber/v2.(*App).handler(0x14000140a00, 0x104950358?)
    /Users/chris/go/pkg/mod/github.com/gofiber/fiber/v2@v2.50.0/router.go:172 +0x74
github.com/valyala/fasthttp.(*Server).serveConn(0x14000174000, {0x104aa0cc8?, 0x1400004c228})
    /Users/chris/go/pkg/mod/github.com/valyala/fasthttp@v1.50.0/server.go:2359 +0xdd0
github.com/valyala/fasthttp.(*workerPool).workerFunc(0x1400010e960, 0x14000070540)
    /Users/chris/go/pkg/mod/github.com/valyala/fasthttp@v1.50.0/workerpool.go:224 +0x70
github.com/valyala/fasthttp.(*workerPool).getCh.func1()
    /Users/chris/go/pkg/mod/github.com/valyala/fasthttp@v1.50.0/workerpool.go:196 +0x38
created by github.com/valyala/fasthttp.(*workerPool).getCh in goroutine 1
    /Users/chris/go/pkg/mod/github.com/valyala/fasthttp@v1.50.0/workerpool.go:195 +0x208

I have an example in this draft pr of how to replicate it, just call localhost:4242/crashme and you should get a panic that points to https://github.com/griggsca91/slog-fiber/blob/main/middleware.go#L194.

I did test a small fix that would be to just replace the err with the logErr above, so if that's an appropriate solution, I can go ahead and make the PR for it.

griggsca91 commented 11 months ago

also like the lib, thx for it

samber commented 11 months ago

oops, it was a regression, thanks for the fix