Skip to Content
Senri
Errors

onError

Decide the response for errors with app.onError

import { ,  } from "@senrijs/senri";

const  = ();

.((, ) => {
    if ( instanceof ) return { : . };   // with the HttpError's status
    .();
    return { : "something went wrong" };                       // with 500
});
  • Everything thrown by handlers and middleware reaches app.onError
  • For an HttpError, its status and headers are already set, so returning a value gives the right status. Anything else is 500
  • You can also change it with c.status()
  • Throwing inside onError gives a fixed 500

Development mode

Setting app.onError also replaces the development error pages. To keep the error pages during development, set it only in production.

if (..NODE_ENV === "production") {
    .(() => ({ : "something went wrong" }));
}

Other error formats

  • Requests that match nothing go to app.notFound, not app.onError (Not found)
  • For RFC 9457 application/problem+json, use Problem Details

On this page