sendErrorMessage static method

void sendErrorMessage(
  1. HttpRequest req,
  2. dynamic code
)

Implementation

static void sendErrorMessage(HttpRequest req, code) {
  var res = req.response;
  var isForbidden = !serverErrorMessages.containsKey(code);
  if (isForbidden) {
    res.statusCode = HttpStatus.forbidden;
    res.headers.contentType = ContentType.json;
    res.write(json.encode({
      'code': ServerErrors.forbidden,
      'message': code ?? serverErrorMessages[ServerErrors.forbidden]
    }));
    return;
  }
  if (req.headers.value('origin') != null) {
    res.headers.add('Access-Control-Allow-Credentials', 'true');
    res.headers
        .add('Access-Control-Allow-Origin', req.headers.value('origin')!);
  } else {
    res.headers.add('Access-Control-Allow-Origin', '*');
  }
  res.statusCode = HttpStatus.badRequest;
  res.write(
      json.encode({'code': code, 'message': serverErrorMessages[code]}));
}