apperr provides a unified application error generation interface. Errors can be localized and converted to GRPC, Twirp, HTTP, etc. equivalents
To install apperr, simly run:
$ go get github.com/harwoeck/apperr
To create errors use one of the provided functions from apperr, like apperr.Unauthenticated(msg)
err := apperr.Unauthenticated("provided password is invalid",
apperr.Localize("INVALID_PASSWORD"))
apperr can provide localized error messages to users, when a LocalizationProvider
is available. In order to add a translation message id to your *AppError
you can use one of the provided Option
(in the example Localize(messageID)
is used)
In order to render an *AppError
into a *RenderedError
use the static function Render
:
rendered, _ := apperr.Render(err, apperr.RenderLocalized(adapter, "en-US"))
Use the Convert
function from your installed converter to translate the *RenderedError
from last step to your frameworks native error type:
- GRPC
- Install converter
go get github.com/harwoeck/apperr/x/grpcerr
-
grpcStatus, err := grpcerr.Convert(*finalized.Error)
- Install converter
- HTTP
- Install converter
go get github.com/harwoeck/apperr/x/httperr
-
httpStatus, httpBody, err := httperr.Convert(*finalized.Error)
- Install converter
- Twirp
- Install converter
go get github.com/harwoeck/apperr/x/twirperr
-
twirpError := twirperr.Convert(*finalized.Error)
- Install converter
- Terminal or Console
- Install converter
go get github.com/harwoeck/apperr/x/terminalerr
-
output := terminalerr.Convert(*finalized.Error) fmt.Println(output)
- Install converter