Melkeydev / go-blueprint

Go-blueprint allows users to spin up a quick Go project using a popular framework
MIT License
2.07k stars 141 forks source link

[Feature Request] [Fiber Framework Middleware] A Better Panic Recovery #222

Closed H0llyW00dzZ closed 1 month ago

H0llyW00dzZ commented 1 month ago

Tell us about your feature request

As currently there is no middleware feature especially for panic recovery, if anyone agrees, I can submit a PR.

Example:

Default Fiber

$ curl -i -H "Accept: application/json" http://localhost:8080/panic
HTTP/1.1 500 Internal Server Error
Server: H0llyW00dzZ Project
Date: Sun, 07 Apr 2024 17:04:27 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 12
X-Xss-Protection: 0
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Cross-Origin-Embedder-Policy: require-corp
Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Resource-Policy: same-origin
Origin-Agent-Cluster: ?1
Referrer-Policy: strict-origin-when-cross-origin
X-Dns-Prefetch-Control: off
X-Download-Options: noopen
X-Permitted-Cross-Domain-Policies: none

I'm an error

In the logger:

2024/04/08 00:04:27 [H0llyW00dzZ Project] [CRASH] Panic occurred: I'm an error
2024/04/08 00:04:27 [H0llyW00dzZ Project] [CRASH] Stack trace:
goroutine 67 [running]:
runtime/debug.Stack()
        C:/Program Files/Go/src/runtime/debug/stack.go:24 +0x5e

A Better Panic Recovery by Manipulation

$ curl -i -H "Accept: application/json" http://localhost:8080/panic
HTTP/1.1 500 Internal Server Error
Server: H0llyW00dzZ Project
Date: Sun, 07 Apr 2024 17:07:03 GMT
Content-Type: application/json
Content-Length: 33
X-Xss-Protection: 0
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Cross-Origin-Embedder-Policy: require-corp
Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Resource-Policy: same-origin
Origin-Agent-Cluster: ?1
Referrer-Policy: strict-origin-when-cross-origin
X-Dns-Prefetch-Control: off
X-Download-Options: noopen
X-Permitted-Cross-Domain-Policies: none

{"error":"Internal Server Error"}

In the logger:

2024/04/08 00:06:47 [H0llyW00dzZ Project] [CRASH] Panic occurred: I'm an error
2024/04/08 00:06:47 [H0llyW00dzZ Project] [CRASH] Stack trace:
goroutine 28 [running]:
runtime/debug.Stack()
        C:/Program Files/Go/src/runtime/debug/stack.go:24 +0x5e

[!NOTE] This is a method to manipulate panic, because by default, Fiber shows the panic in the front-end.

Disclaimer

H0llyW00dzZ commented 1 month ago

It works well in combination with a rate limiter for scalability:

$ curl -i -H "Accept: application/json" http://localhost:8080/panic
HTTP/1.1 429 Too Many Requests
Server: H0llyW00dzZ Project
Date: Sun, 07 Apr 2024 17:40:23 GMT
Content-Type: application/json
Content-Length: 54
Retry-After: 44

{"error":"Too many requests, please try again later."}
briancbarrow commented 1 month ago

Closing this issue as well as the linked PR. See the comment in the PR for explanation.