Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
srahkmli committed Dec 30, 2024
2 parents 3a93030 + 64248a1 commit 8e276d4
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 17 deletions.
17 changes: 0 additions & 17 deletions .idea/material_theme_project_new.xml

This file was deleted.

58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,61 @@ To install the `panicrecovery` package, run the following command in Go project

```bash
go get github.com/srahkmli/panicrecovery
```
## Overview

### `HTTPRecover`

- **Description:** A simple middleware that recovers from panics, logs the error, and sends a default `500 Internal Server Error` response.
- **Key Features:**
- Logs the panic error and stack trace.
- Responds with a fixed error message (`"Internal Server Error"`) and HTTP status code (`500`).
- **Use Case:** Suitable for applications requiring a straightforward panic recovery mechanism without customization.

#### Example Usage:
```go
http.Handle("/example", HTTPRecover(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
panic("example panic")
})))
```

---

### `HTTPRecoverWithHandler`

- **Description:** A flexible middleware that allows custom handling of errors via a user-provided `HTTPErrorHandler` function.
- **Key Features:**
- Accepts an `HTTPErrorHandler` function for customizable error responses.
- Provides a fallback behavior similar to `HTTPRecover` if no handler is specified.
- **Use Case:** Ideal for applications requiring detailed and tailored error responses or additional error-handling logic, such as sending alerts or metrics.

#### Example Usage:
```go
customHandler := func(w http.ResponseWriter, r *http.Request, err interface{}) {
log.Printf("Custom handler: panic %v", err)
http.Error(w, "Something went wrong", http.StatusInternalServerError)
}

http.Handle("/example", HTTPRecoverWithHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
panic("example panic")
}), customHandler))
```

- Use **`HTTPRecover`** for simple applications that do not require custom error handling.

- 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.
---

## Differences

| Aspect | `HTTPRecover` | `HTTPRecoverWithHandler` |
|-----------------------|---------------------------------------------------------|----------------------------------------------------------|
| **Error Handling** | Fixed behavior: log + `500 Internal Server Error`. | Customizable via `HTTPErrorHandler`. |
| **Flexibility** | Minimal flexibility. | Highly flexible due to user-provided handler. |
| **Custom Error Output**| Not supported. | Fully supported via the handler. |
| **Default Fallback** | Logs the error and responds with `500`. | Logs the error and responds with `500` if no handler. |

---



0 comments on commit 8e276d4

Please sign in to comment.