Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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)
```