@@ -6,30 +6,76 @@ and database errors, as well as the ability to create custom errors with additio
6
6
7
7
## Pre-defined HTTP Errors
8
8
9
- GoFr’ s ` 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
10
10
automatically handle HTTP status code selection. These include:
11
11
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 %}
19
61
20
62
#### 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 :
22
64
``` go
23
- err := http.ErrorMissingParam {Param: []string {" id" }}
65
+ import " gofr.dev/pkg/gofr/http"
66
+
67
+ err := http.ErrorMissingParam {Params: []string {" id" }}
24
68
```
25
69
26
70
## Database Errors
27
71
Database errors in GoFr, represented in the ` datasource ` package, encapsulate errors related to database operations such
28
72
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) **
30
74
31
75
#### Usage:
32
76
``` go
77
+ import " gofr.dev/pkg/gofr/datasource"
78
+
33
79
// Creating a custom error wrapped in underlying error for database operations
34
80
dbErr := datasource.ErrorDB {Err: err, Message : " error from sql db" }
35
81
@@ -49,19 +95,19 @@ Users can optionally define a log level for your error with the `LogLevel() log
49
95
#### Usage:
50
96
``` go
51
97
type customError struct {
52
- error string
98
+ error string
53
99
}
54
100
55
101
func (c customError ) Error () string {
56
- return fmt.Sprintf (" custom error: %s " , c.error )
102
+ return fmt.Sprintf (" custom error: %s " , c.error )
57
103
}
58
104
59
105
func (c customError ) StatusCode () int {
60
- return http.StatusMethodNotAllowed
106
+ return http.StatusMethodNotAllowed
61
107
}
62
108
63
109
func (c customError ) LogLevel () logging .Level {
64
- return logging.WARN
110
+ return logging.WARN
65
111
}
66
112
```
67
113
@@ -71,7 +117,7 @@ For [RFC 9457](https://www.rfc-editor.org/rfc/rfc9457.html) style error response
71
117
72
118
``` go
73
119
type ResponseMarshaller interface {
74
- Response () map [string ]any
120
+ Response () map [string ]any
75
121
}
76
122
```
77
123
0 commit comments