You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: documentation/docs/guides/errors.md
+72-8Lines changed: 72 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,22 +4,85 @@ Error handling is a crucial part of any application. It is important to handle e
4
4
5
5
## Error handling in Fuego
6
6
7
-
Fuego [controllers](./controllers) returns a value and an error. If the error is not `nil`, it means that an error occurred while processing the request. The error will be returned to the client as a JSON response.
7
+
Fuego [controllers](./controllers) returns a value and an error. If the error is not `nil`,
8
+
it means that an error occurred while processing the request.
9
+
The error will be returned to the client as a JSON or XML response.
8
10
9
-
By default, Fuego implements [RFC 9457](https://www.rfc-editor.org/rfc/rfc9457), which defines a standard error format for HTTP APIs. We strongly recommend following this standard, but you can also use your own errors.
11
+
The default error handler will transform any error that implements the
12
+
`fuego.ErrorWithStatus` or `fuego.ErrorWithDetail` interfaces into a `fuego.HTTPError`. The `fuego.HTTPError` implements
13
+
[RFC 9457](https://www.rfc-editor.org/rfc/rfc9457), which defines a standard error format for HTTP APIs.
14
+
We strongly recommend following this standard, but you can also use your own errors.
10
15
11
-
The error type returned as JSON is `fuego.HTTPError`. It has a `Status` and a `Info` field. The `Status` field is an integer that represents the error code. The `Info` field is a string that contains a human-readable error message.
16
+
The default `fuego.ErrorHandler` can be overridden using `fuego.WithErrorHandler` at fuego Server creation time.
12
17
13
-
If your error implements `Status() int` and `Info()` methods, the error will include the status code and the error message in the `fuego.HTTPError` response.
18
+
The error type of `fuego.HTTPError` is returned as JSON or XML depending on the content-type specified.
19
+
It's structure is the following:
20
+
21
+
```go
22
+
// HTTPError is the error response used by the serialization part of the framework.
23
+
typeHTTPErrorstruct {
24
+
// Developer readable error message. Not shown to the user to avoid security leaks.
25
+
Errerror`json:"-" xml:"-"`
26
+
// URL of the error type. Can be used to lookup the error in a documentation
27
+
Typestring`json:"type,omitempty" xml:"type,omitempty" description:"URL of the error type. Can be used to lookup the error in a documentation"`
28
+
// Short title of the error
29
+
Titlestring`json:"title,omitempty" xml:"title,omitempty" description:"Short title of the error"`
30
+
// HTTP status code. If using a different type than [HTTPError], for example [BadRequestError],
31
+
// this will be automatically overridden after Fuego error handling.
32
+
Statusint`json:"status,omitempty" xml:"status,omitempty" description:"HTTP status code" example:"403"`
Copy file name to clipboardExpand all lines: errors.go
+13-6Lines changed: 13 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -8,24 +8,31 @@ import (
8
8
)
9
9
10
10
// ErrorWithStatus is an interface that can be implemented by an error to provide
11
-
// additional information about the error.
11
+
// a status code
12
12
typeErrorWithStatusinterface {
13
13
error
14
14
StatusCode() int
15
15
}
16
16
17
+
// ErrorWithDetail is an interface that can be implemented by an error to provide
18
+
// an additional detail message about the error
17
19
typeErrorWithDetailinterface {
18
20
error
19
21
DetailMsg() string
20
22
}
21
23
22
24
// HTTPError is the error response used by the serialization part of the framework.
23
25
typeHTTPErrorstruct {
24
-
Errerror`json:"-" xml:"-"`// Developer readable error message. Not shown to the user to avoid security leaks.
25
-
Typestring`json:"type,omitempty" xml:"type,omitempty" description:"URL of the error type. Can be used to lookup the error in a documentation"`// URL of the error type. Can be used to lookup the error in a documentation
26
-
Titlestring`json:"title,omitempty" xml:"title,omitempty" description:"Short title of the error"`// Short title of the error
27
-
Statusint`json:"status,omitempty" xml:"status,omitempty" description:"HTTP status code" example:"403"`// HTTP status code. If using a different type than [HTTPError], for example [BadRequestError], this will be automatically overridden after Fuego error handling.
28
-
Detailstring`json:"detail,omitempty" xml:"detail,omitempty" description:"Human readable error message"`// Human readable error message
26
+
// Developer readable error message. Not shown to the user to avoid security leaks.
27
+
Errerror`json:"-" xml:"-"`
28
+
// URL of the error type. Can be used to lookup the error in a documentation
29
+
Typestring`json:"type,omitempty" xml:"type,omitempty" description:"URL of the error type. Can be used to lookup the error in a documentation"`
30
+
// Short title of the error
31
+
Titlestring`json:"title,omitempty" xml:"title,omitempty" description:"Short title of the error"`
32
+
// HTTP status code. If using a different type than [HTTPError], for example [BadRequestError], this will be automatically overridden after Fuego error handling.
33
+
Statusint`json:"status,omitempty" xml:"status,omitempty" description:"HTTP status code" example:"403"`
0 commit comments