notFound method

Future<HttpResponse> notFound()

Renders errors.404 template or plain 404.

Implementation

Future<HttpResponse> notFound() async {
  final engine = App().container.make<TemplateEngine>();

  try {
    final html = await engine.render("errors.404", {});
    response.headers.contentType = ContentType.html;

    final csrfCookie = cookies.firstWhereOrNull((c) => c.name == 'archery_csrf_token');
    final cookie = Cookie('archery_csrf_token', csrfCookie?.value ?? App.generateKey())
      ..httpOnly = true
      ..secure = true
      ..sameSite = SameSite.lax
      ..path = '/';

    return response
      ..statusCode = HttpStatus.notFound
      ..cookies.add(cookie)
      ..write(html)
      ..close();
  } catch (e) {
    return response
      ..statusCode = HttpStatus.notFound
      ..write("404 Not Found")
      ..close();
  }
}