From f4eacf217638fff9bf0c71618d3672334feba82c Mon Sep 17 00:00:00 2001 From: EwenQuim Date: Tue, 13 Feb 2024 23:57:45 +0100 Subject: [PATCH] docs: error handling --- documentation/docs/guides/errors.mdx | 30 ++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/documentation/docs/guides/errors.mdx b/documentation/docs/guides/errors.mdx index adb6a227..e686db6c 100644 --- a/documentation/docs/guides/errors.mdx +++ b/documentation/docs/guides/errors.mdx @@ -6,3 +6,33 @@ Error handling is a crucial part of any application. It is important to handle e + +## Error handling in Fuego + +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. + +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. + +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. + +```go +type MyCustomError struct { + Status int + Message string +} + +func (e MyCustomError) Status() int { + return e.Status +} +``` + +## Default errors + +Fuego provides a set of default errors that you can use in your application. + +- `fuego.BadRequestError`: 400 Bad Request +- `fuego.UnauthorizedError`: 401 Unauthorized +- `fuego.ForbiddenError`: 403 Forbidden +- `fuego.NotFoundError`: 404 Not Found +- `fuego.ConflictError`: 409 Conflict +- `fuego.InternalServerError`: 500 Internal Server Error