Skip to content

Commit

Permalink
Updates docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mfridman committed Nov 1, 2019
1 parent 3b38b7e commit 460ff10
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions docs/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,24 @@ If your service requires errors with complex metadata, you should consider addin
wrappers on top of the auto-generated clients, or just include business-logic errors as
part of the Protobuf messages (add an error field to proto messages).

### Writing HTTP Errors outside Twirp services

Twirp services can be [muxed with other HTTP services](mux.md). For consistent responses
and error codes _outside_ Twirp servers, such as http middlewares, you can call `twirp.WriteError`.

The error is expected to satisfy a `twirp.Error`, otherwise it is wrapped with `twirp.InternalError`.

Usage:

```go
rpc.WriteError(w, twirp.NewError(twirp.Unauthenticated, "invalid token"))
```

To simplify `twirp.Error` composition, a few constructors are available, such as `NotFoundError`
and `RequiredArgumentError`. See [docs](https://godoc.org/github.com/twitchtv/twirp#Error).

With constructor:

```go
rpc.WriteError(w, twirp.RequiredArgumentError("user_id"))
```

0 comments on commit 460ff10

Please sign in to comment.