Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/harwoeck/apperr
🗑 apperr provides a unified framework- and network-agnostic error generation interface. Errors can be localized and converted to GRPC, Twirp, HTTP, etc. equivalents
https://github.com/harwoeck/apperr
apperr application-error error error-handling errors go golang grpc i18n localization twirp
Last synced: about 1 month ago
JSON representation
🗑 apperr provides a unified framework- and network-agnostic error generation interface. Errors can be localized and converted to GRPC, Twirp, HTTP, etc. equivalents
- Host: GitHub
- URL: https://github.com/harwoeck/apperr
- Owner: harwoeck
- License: apache-2.0
- Created: 2021-07-21T09:59:31.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-10-28T07:41:21.000Z (about 2 years ago)
- Last Synced: 2024-10-01T15:29:39.950Z (about 1 month ago)
- Topics: apperr, application-error, error, error-handling, errors, go, golang, grpc, i18n, localization, twirp
- Language: Go
- Homepage:
- Size: 77.1 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 🗑 apperr
_apperr_ provides a unified application error generation interface. Errors can be localized and converted to GRPC, Twirp, HTTP, etc. equivalents
[![Go Reference](https://pkg.go.dev/badge/github.com/harwoeck/apperr.svg)](https://pkg.go.dev/github.com/harwoeck/apperr)
## Installation
To install _apperr_, simly run:
```bash
$ go get github.com/harwoeck/apperr
```## Usage
#### Create
To create errors use one of the [provided functions](https://pkg.go.dev/github.com/harwoeck/apperr#pkg-index) from apperr, like `apperr.Unauthenticated(msg)`
```go
err := apperr.Unauthenticated("provided password is invalid",
apperr.Localize("INVALID_PASSWORD"))
```_apperr_ can provide localized error messages to users, when a [`LocalizationProvider`](https://pkg.go.dev/github.com/harwoeck/apperr#LocalizationProvider) is available. In order to add a translation message id to your [`*AppError`](https://pkg.go.dev/github.com/harwoeck/apperr#AppError) you can use one of the provided [`Option`](https://pkg.go.dev/github.com/harwoeck/apperr#Option) (in the example [`Localize(messageID)`](https://pkg.go.dev/github.com/harwoeck/apperr#Localize) is used)
#### Render
In order to render an [`*AppError`](https://pkg.go.dev/github.com/harwoeck/apperr#Render) into a [`*RenderedError`](https://pkg.go.dev/github.com/harwoeck/apperr#RenderedError) use the static function [`Render`](https://pkg.go.dev/github.com/harwoeck/apperr#Render):
```go
rendered, _ := apperr.Render(err, apperr.RenderLocalized(adapter, "en-US"))
```#### Convert
Use the `Convert` function from your installed converter to translate the `*RenderedError` from last step to your frameworks native error type:
- [GRPC](https://pkg.go.dev/github.com/harwoeck/apperr/x/grpcerr)
- Install converter `go get github.com/harwoeck/apperr/x/grpcerr`
- ```go
grpcStatus, err := grpcerr.Convert(*finalized.Error)
```
- [HTTP](https://pkg.go.dev/github.com/harwoeck/apperr/x/httperr)
- Install converter `go get github.com/harwoeck/apperr/x/httperr`
- ```go
httpStatus, httpBody, err := httperr.Convert(*finalized.Error)
```
- [Twirp](https://pkg.go.dev/github.com/harwoeck/apperr/x/twirperr)
- Install converter `go get github.com/harwoeck/apperr/x/twirperr`
- ```go
twirpError := twirperr.Convert(*finalized.Error)
```
- [Terminal or Console](https://pkg.go.dev/github.com/harwoeck/apperr/x/terminalerr)
- Install converter `go get github.com/harwoeck/apperr/x/terminalerr`
- ```go
output := terminalerr.Convert(*finalized.Error)
fmt.Println(output)
```