{"id":23705278,"url":"https://github.com/srahkmli/go-panic-proof","last_synced_at":"2026-02-02T03:30:21.296Z","repository":{"id":268726046,"uuid":"905276099","full_name":"srahkmli/go-panic-proof","owner":"srahkmli","description":"panicrecovery is a Go package that provides middleware for recovering from panics in both HTTP and gRPC servers. This middleware helps ensure that server doesn't crash when an unexpected panic occurs, logging the error and providing an appropriate response.","archived":false,"fork":false,"pushed_at":"2024-12-30T11:54:19.000Z","size":8,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-12-30T12:28:04.734Z","etag":null,"topics":["go","go-panic","grpc-server","http-server","middleware","panic","panic-recovery"],"latest_commit_sha":null,"homepage":"https://github.com/srahkmli/go-panic-proof","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/srahkmli.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-12-18T13:59:12.000Z","updated_at":"2024-12-30T11:54:24.000Z","dependencies_parsed_at":"2024-12-18T15:22:52.372Z","dependency_job_id":"e1fc4d5f-ac07-4c49-97f3-ab2ab379a250","html_url":"https://github.com/srahkmli/go-panic-proof","commit_stats":null,"previous_names":["srahkmli/go-panic-proof"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/srahkmli%2Fgo-panic-proof","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/srahkmli%2Fgo-panic-proof/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/srahkmli%2Fgo-panic-proof/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/srahkmli%2Fgo-panic-proof/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/srahkmli","download_url":"https://codeload.github.com/srahkmli/go-panic-proof/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239786211,"owners_count":19696770,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["go","go-panic","grpc-server","http-server","middleware","panic","panic-recovery"],"created_at":"2024-12-30T14:34:31.313Z","updated_at":"2026-02-02T03:30:21.143Z","avatar_url":"https://github.com/srahkmli.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Go Panic Proof\n\n`go-panic-proof` is a Go package that provides middleware for recovering from panics in both HTTP and gRPC servers. This middleware helps ensure that  server doesn't crash when an unexpected panic occurs, logging the error and providing an appropriate response.\n\n## Features\n\n- Recover from panics in HTTP handlers.\n- Recover from panics in gRPC service methods.\n- Logs the panic details along with the stack trace for debugging.\n- Responds with `500 Internal Server Error` for HTTP, ensuring clients are informed about the issue.\n```bash\n2024/12/18 15:32:45 Recovered from panic in gRPC method /service/SomeMethod: something went wrong!\nStack Trace: \ngoroutine 10 [running]:\nruntime/debug.Stack(0x0, 0x0, 0x0)\n\t/usr/local/go/src/runtime/debug/stack.go:24 +0x88\ngithub.com/srahkmli/go-panic-proof.RecoverInterceptor.func1(0x1f7cae0, 0x0, 0x1f7cae0, 0x0, 0x7fddc35d19b0)\n\t/project/go-panic-proof/grpc.go:23 +0xd3\ngoogle.golang.org/grpc.(*Server).processUnaryRPC(0xc0004db5e0, 0x7fddc35d1928, 0x1f9b960, 0xc0004d7030, 0xc00023bc00, 0x1f7cb80, 0x0, 0x0, 0x0)\n\t/project/go/pkg/mod/google.golang.org/grpc@v1.42.0/server.go:1281 +0x45b\n...\n\n```\n  \n## Installation\n\nTo install the `go-panic-proof` package, run the following command in  Go project:\n\n```bash\ngo get github.com/srahkmli/go-panic-proof\n``` \n## Overview\n\n### `HTTPRecover`\n\n- **Description:** A simple middleware that recovers from panics, logs the error, and sends a default `500 Internal Server Error` response.\n- **Key Features:**\n  - Logs the panic error and stack trace.\n  - Responds with a fixed error message (`\"Internal Server Error\"`) and HTTP status code (`500`).\n- **Use Case:** Suitable for applications requiring a straightforward panic recovery mechanism without customization.\n\n#### Example Usage:\n```go\nhttp.Handle(\"/example\", HTTPRecover(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {\n    panic(\"example panic\")\n})))\n```\n\n---\n\n### `HTTPRecoverWithHandler`\n\n- **Description:** A flexible middleware that allows custom handling of errors via a user-provided `HTTPErrorHandler` function.\n- **Key Features:**\n  - Accepts an `HTTPErrorHandler` function for customizable error responses.\n  - Provides a fallback behavior similar to `HTTPRecover` if no handler is specified.\n- **Use Case:** Ideal for applications requiring detailed and tailored error responses or additional error-handling logic, such as sending alerts or metrics.\n\n#### Example Usage:\n```go\ncustomHandler := func(w http.ResponseWriter, r *http.Request, err interface{}) {\n    log.Printf(\"Custom handler: panic %v\", err)\n    http.Error(w, \"Something went wrong\", http.StatusInternalServerError)\n}\n\nhttp.Handle(\"/example\", HTTPRecoverWithHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {\n    panic(\"example panic\")\n}), customHandler))\n```\n\n- Use **`HTTPRecover`** for simple applications that do not require custom error handling.\n\n- Use **`HTTPRecoverWithHandler`** when you need to provide detailed error responses or additional error-handling logic. The additional flexibility ensures the middleware can adapt to complex application requirements.\n---\n\n## Differences\n\n| Aspect                | `HTTPRecover`                                           | `HTTPRecoverWithHandler`                                  |\n|-----------------------|---------------------------------------------------------|----------------------------------------------------------|\n| **Error Handling**     | Fixed behavior: log + `500 Internal Server Error`.      | Customizable via `HTTPErrorHandler`.                     |\n| **Flexibility**        | Minimal flexibility.                                    | Highly flexible due to user-provided handler.            |\n| **Custom Error Output**| Not supported.                                          | Fully supported via the handler.                        |\n| **Default Fallback**   | Logs the error and responds with `500`.                 | Logs the error and responds with `500` if no handler.    |\n\n---\n \n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsrahkmli%2Fgo-panic-proof","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsrahkmli%2Fgo-panic-proof","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsrahkmli%2Fgo-panic-proof/lists"}