make500 method

Response make500(
  1. Context ctx,
  2. Object error, [
  3. StackTrace? stack
])
override

Makes Response for 500 error

Respects 'accept' request header and returns corresponding Response

Implementation

Response make500(Context ctx, Object error, [StackTrace? stack]) {
  final String accept = ctx.req.headers.value(HttpHeaders.acceptHeader) ?? '';
  final List<String> acceptList = accept.split(',');

  if (acceptList.contains('text/html')) {
    return Response(
        body: _write500Html(ctx, error, stack),
        statusCode: HttpStatus.notFound,
        mimeType: MimeTypes.html);
  } else if (acceptList.contains('application/json') ||
      acceptList.contains('text/json')) {
    final data = <String, dynamic>{
      'error': error.toString(),
    };
    if (stack != null)
      data['stack'] =
          Trace.from(stack).frames.map((f) => f.toString()).toList();

    return Response.json(data, statusCode: 500);
  } /* TODO else if (acceptList.contains('application/xml')) {
    final data = <String, dynamic>{
      'error': error.toString(),
    };
    if (stack != null) data['stack'] = Trace.format(stack);

    return Response.xml(data, statusCode: 500);
  } */
  else {
    return Response(
        body: _write500Html(ctx, error, stack),
        statusCode: 500,
        mimeType: MimeTypes.html);
  }
}