renderError function

dynamic renderError(
  1. HttpRequest req,
  2. Object error,
  3. StackTrace stackTrace
)

Report error and give the response for it, from the ExceptionHandler of AppConfig. It is not sent yet.

Implementation

dynamic renderError(HttpRequest req, Object error, StackTrace stackTrace) {
  /// `schema.parse()` in a controller: answer 422 like `req.validate()`
  if (error is dv.ValidationException) {
    error = ValidationException(message: error.errors, issues: error.issues);
  }
  ExceptionHandler handler = Dox().config.exceptionHandler;
  dynamic payload = error;
  try {
    /// 4xx answers are the client's fault, only 5xx are reported
    if (error is! IHttpException || error.code >= 500) {
      handler.reportFor(req, error, stackTrace);
    }
    payload = handler.render(error, req);
  } catch (handlerError, handlerStack) {
    /// a broken handler must not leave the client waiting.
    /// send the default answer for the first error.
    Logger.danger('ExceptionHandler failed: $handlerError\n$handlerStack');
  }
  return payload;
}