RiserBob logoRiserBobv1.0.0
HomeDocsCLIRelease notes

Getting started

  • Introduction
  • Installation
  • Quick start
  • Project structure

Core concepts

  • Routing
  • Request
  • Responses
  • Error handling

Middleware

  • Middleware
  • Authentication
  • Logger and CORS

Data

  • Validation

Structure and tooling

  • Modules
  • CLI

Examples

  • Build a project
  • Mongoose
  • Drizzle ORM
  • Sequelize
  • JWT and cookies
  • File uploads

Packages

  • Cookies

Resources

  • Troubleshooting
  • Release notes
  • License
  • Security
  • Contributing
Core concepts

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.

TypeScript
import { HttpError } from 'riser-bob' bob.spongGet('/secret', async () => {  throw new HttpError(403, 'Forbidden')})
Output
{ "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

SituationResponse
No matching route404 Not Found
Path exists, method does not405 Method Not Allowed with an Allow header
Handler throws an HttpErrorThe error's status and message
Handler throws any other error500 Internal Server Error. Details are never exposed to the client.
Handler returns no Response500 with the message Handler did not send a response
Body too large413 Content Too Large
Unexpected errors are logged on the server with their full details, while the client only ever sees Internal Server Error. Nothing sensitive leaks through a stack trace.

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.

TypeScript
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.

PreviousResponsesNextMiddleware

On this page

  • Failing with a status
  • What RiserBob handles for you
  • Errors in middleware
  • Errors from your database