Error handling
Throw an HttpError to fail with a specific status. RiserBob handles everything else.
Failing with a status
To fail with a specific status, throw an HttpError with the status and a message. The central error handler turns it into a JSON answer.
import { HttpError } from 'riser-bob' bob.spongGet('/secret', async () => { throw new HttpError(403, 'Forbidden')}){ "error": "Forbidden" }An HttpError has a status and a message. The message is sent to the client as it is, so never put sensitive details in it.
What RiserBob handles for you
| Situation | Response |
|---|---|
| No matching route | 404 Not Found |
| Path exists, method does not | 405 Method Not Allowed with an Allow header |
Handler throws an HttpError | The error's status and message |
| Handler throws any other error | 500 Internal Server Error. Details are never exposed to the client. |
Handler returns no Response | 500 with the message Handler did not send a response |
| Body too large | 413 Content Too Large |
Errors in middleware
Middleware can throw too, and the same handler answers it. An HttpError keeps its status, anything else becomes 500. If a verifier passed to AuthMiddleware throws, the error goes through the same path.
Errors from your database
A database error that you do not catch becomes a 500. When the cause is the client, such as a malformed id, check the input first and answer with a clear status instead of letting the driver throw.
const id = spongReq.params.id if (!id || !isUuid(id)) { return spongRes.SpongBadRequest({ success: false, message: 'Invalid id' })}The Troubleshooting page lists the errors you are most likely to meet and their causes.