Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 62 additions & 16 deletions docs/advanced-guide/gofr-errors/page.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,76 @@ and database errors, as well as the ability to create custom errors with additio

## Pre-defined HTTP Errors

GoFrs `http` package offers several predefined error types to represent common HTTP error scenarios. These errors
GoFr's `http` package offers several predefined error types to represent common HTTP error scenarios. These errors
automatically handle HTTP status code selection. These include:

- `ErrorInvalidParam`: Represents an error due to an invalid parameter.
- `ErrorMissingParam`: Represents an error due to a missing parameter.
- `ErrorEntityNotFound`: Represents an error due to a not found entity.
- `ErrorEntityAlreadyExist`: Represents an error due to creation of duplicate entity.
- `ErrorInvalidRoute`: Represents an error for invalid route.
- `ErrorRequestTimeout`: Represents an error for request which timed out.
- `ErrorPanicRecovery`: Represents an error for request which panicked.
{% table %}

- Error Type
- Description
- Status Code

---

- `ErrorInvalidParam`
- Represents an error due to an invalid parameter
- 400 (Bad Request)

---

- `ErrorMissingParam`
- Represents an error due to a missing parameter
- 400 (Bad Request)

---

- `ErrorEntityNotFound`
- Represents an error due to a not found entity
- 404 (Not Found)

---

- `ErrorEntityAlreadyExist`
- Represents an error due to creation of duplicate entity
- 409 (Conflict)

---

- `ErrorInvalidRoute`
- Represents an error for invalid route
- 404 (Not Found)

---

- `ErrorRequestTimeout`
- Represents an error for request which timed out
- 408 (Request Timeout)

---

- `ErrorPanicRecovery`
- Represents an error for request which panicked
- 500 (Internal Server Error)

{% /table %}

#### Usage:
To use the predefined HTTP errors, users can simply call them using GoFr's http package:
To use the predefined HTTP errors, users need to import the GoFr http package and can simply call them:
```go
err := http.ErrorMissingParam{Param: []string{"id"}}
import "gofr.dev/pkg/gofr/http"

err := http.ErrorMissingParam{Params: []string{"id"}}
```

## Database Errors
Database errors in GoFr, represented in the `datasource` package, encapsulate errors related to database operations such
as database connection, query failure, availability etc. The `ErrorDB` struct can be used to populate `error` as well as
any custom message to it.
any custom message to it. **Status Code: 500 (Internal Server Error)**

#### Usage:
```go
import "gofr.dev/pkg/gofr/datasource"

// Creating a custom error wrapped in underlying error for database operations
dbErr := datasource.ErrorDB{Err: err, Message: "error from sql db"}

Expand All @@ -49,19 +95,19 @@ Users can optionally define a log level for your error with the `LogLevel() log
#### Usage:
```go
type customError struct {
error string
error string
}

func (c customError) Error() string {
return fmt.Sprintf("custom error: %s", c.error)
return fmt.Sprintf("custom error: %s", c.error)
}

func (c customError) StatusCode() int {
return http.StatusMethodNotAllowed
return http.StatusMethodNotAllowed
}

func (c customError) LogLevel() logging.Level {
return logging.WARN
return logging.WARN
}
```

Expand All @@ -71,7 +117,7 @@ For [RFC 9457](https://www.rfc-editor.org/rfc/rfc9457.html) style error response

```go
type ResponseMarshaller interface {
Response() map[string]any
Response() map[string]any
}
```

Expand Down