Skip to content

Commit 0895a03

Browse files
authored
Merge pull request #2357 from amankp-zop/EN/Error-documentation
Fix #2351: Enhance error documentation with status codes and import paths
2 parents 2bee027 + d1dd3ee commit 0895a03

File tree

1 file changed

+62
-16
lines changed
  • docs/advanced-guide/gofr-errors

1 file changed

+62
-16
lines changed

docs/advanced-guide/gofr-errors/page.md

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,76 @@ and database errors, as well as the ability to create custom errors with additio
66

77
## Pre-defined HTTP Errors
88

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

12-
- `ErrorInvalidParam`: Represents an error due to an invalid parameter.
13-
- `ErrorMissingParam`: Represents an error due to a missing parameter.
14-
- `ErrorEntityNotFound`: Represents an error due to a not found entity.
15-
- `ErrorEntityAlreadyExist`: Represents an error due to creation of duplicate entity.
16-
- `ErrorInvalidRoute`: Represents an error for invalid route.
17-
- `ErrorRequestTimeout`: Represents an error for request which timed out.
18-
- `ErrorPanicRecovery`: Represents an error for request which panicked.
12+
{% table %}
13+
14+
- Error Type
15+
- Description
16+
- Status Code
17+
18+
---
19+
20+
- `ErrorInvalidParam`
21+
- Represents an error due to an invalid parameter
22+
- 400 (Bad Request)
23+
24+
---
25+
26+
- `ErrorMissingParam`
27+
- Represents an error due to a missing parameter
28+
- 400 (Bad Request)
29+
30+
---
31+
32+
- `ErrorEntityNotFound`
33+
- Represents an error due to a not found entity
34+
- 404 (Not Found)
35+
36+
---
37+
38+
- `ErrorEntityAlreadyExist`
39+
- Represents an error due to creation of duplicate entity
40+
- 409 (Conflict)
41+
42+
---
43+
44+
- `ErrorInvalidRoute`
45+
- Represents an error for invalid route
46+
- 404 (Not Found)
47+
48+
---
49+
50+
- `ErrorRequestTimeout`
51+
- Represents an error for request which timed out
52+
- 408 (Request Timeout)
53+
54+
---
55+
56+
- `ErrorPanicRecovery`
57+
- Represents an error for request which panicked
58+
- 500 (Internal Server Error)
59+
60+
{% /table %}
1961

2062
#### Usage:
21-
To use the predefined HTTP errors, users can simply call them using GoFr's http package:
63+
To use the predefined HTTP errors, users need to import the GoFr http package and can simply call them:
2264
```go
23-
err := http.ErrorMissingParam{Param: []string{"id"}}
65+
import "gofr.dev/pkg/gofr/http"
66+
67+
err := http.ErrorMissingParam{Params: []string{"id"}}
2468
```
2569

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

3175
#### Usage:
3276
```go
77+
import "gofr.dev/pkg/gofr/datasource"
78+
3379
// Creating a custom error wrapped in underlying error for database operations
3480
dbErr := datasource.ErrorDB{Err: err, Message: "error from sql db"}
3581

@@ -49,19 +95,19 @@ Users can optionally define a log level for your error with the `LogLevel() log
4995
#### Usage:
5096
```go
5197
type customError struct {
52-
error string
98+
error string
5399
}
54100

55101
func (c customError) Error() string {
56-
return fmt.Sprintf("custom error: %s", c.error)
102+
return fmt.Sprintf("custom error: %s", c.error)
57103
}
58104

59105
func (c customError) StatusCode() int {
60-
return http.StatusMethodNotAllowed
106+
return http.StatusMethodNotAllowed
61107
}
62108

63109
func (c customError) LogLevel() logging.Level {
64-
return logging.WARN
110+
return logging.WARN
65111
}
66112
```
67113

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

72118
```go
73119
type ResponseMarshaller interface {
74-
Response() map[string]any
120+
Response() map[string]any
75121
}
76122
```
77123

0 commit comments

Comments
 (0)