renderErrorsWith method

Route renderErrorsWith(
  1. RouteErrorRenderer renderer
)

send the errors of the route added last to renderer, not to the ExceptionHandler.render of AppConfig. It covers the errors of the middleware too, and a body that fails to read (eg. a broken multipart body). A request with a _method override is not covered for a body error, because its route is not known yet. For a body that failed to read, the renderer gets a request with an empty body. The error is still reported when it is a 5xx or not an HTTP error. A 4xx error (like 400 or 413) is not reported, as everywhere in Dox. Return null to let Dox render it as usual. Use it when the route answers in its own format, like gRPC.

Routes are sent to every isolate, so renderer must be a top-level or static function, or a function that captures nothing.

DoxResponse? rpcError(Object error, DoxRequest req) {
  return response(<String, String>{'error': '$error'}).statusCode(500);
}

Route.post('rpc', controller).renderErrorsWith(rpcError);

Implementation

Route renderErrorsWith(RouteErrorRenderer renderer) {
  RouteData? last = _last;
  if (last == null) {
    throw StateError('Route.renderErrorsWith(): there is no route.');
  }
  for (RouteData data in _lastGroup ?? <RouteData>[last]) {
    data.errorRenderer = renderer;
  }
  return this;
}