https://github.com/quentinguidee/errors
Replacement for go errors with http errors support
https://github.com/quentinguidee/errors
errors go golang
Last synced: 3 months ago
JSON representation
Replacement for go errors with http errors support
- Host: GitHub
- URL: https://github.com/quentinguidee/errors
- Owner: quentinguidee
- License: mit
- Created: 2025-01-04T22:22:52.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2025-01-13T11:16:39.000Z (4 months ago)
- Last Synced: 2025-01-13T12:27:57.230Z (4 months ago)
- Topics: errors, go, golang
- Language: Go
- Homepage:
- Size: 14.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# errors
A Go package designed to propagate http errors in an API codebase.
## Installation
```go
import "github.com/quentinguidee/errors"
```## Usage
In this example, we want to create a 404 "not found" error and a 503 "service unavailable" error:
```go
var err error
err = errors.NotFound("your resource was not found")
err = errors.ServiceUnavailable("the api cannot be accessed for the moment")
```You can see all implemented errors in [errors.go](https://github.com/quentinguidee/errors/blob/32e33399f18a58341b2c2af0809799708d1e56e0/errors.go#L90-L214).
Then, we can propagate these errors in the application, to catch them later in an http middleware:
```go
err := c.Error()
var target *errors.HTTPError
if errors.As(err, &target) && target.Code < 500 {
// Send the 'target' error directly as a json to the client.
} else {
// Log this error locally and return a generic error to hide implementation logic errors.
}
```A basic echo middleware is already implemented if needed [here](./middleware/echo/middleware.go).
## LICENSE
This package is licensed under the [MIT License](./LICENSE.md).