handleEdgeFetch<E extends Variables> function

Future<Response> handleEdgeFetch<E extends Variables>(
  1. Aim<E> app,
  2. EdgeRaw raw
)

Runs app against raw and produces a web.Response.

For use by adapter packages implementing a runtime's fetch entry point. Two fallbacks keep a broken request or response from crashing the isolate:

  1. If Aim.handle throws, logs to console.error and answers 400.
  2. If toWebResponse throws, logs to console.error and answers 500.

Unhandled errors inside a handler (with no Aim.onError registered) are caught by Aim.handle's onUnhandledError, logged, and answered as 500.

Implementation

Future<web.Response> handleEdgeFetch<E extends Variables>(
  Aim<E> app,
  EdgeRaw raw,
) async {
  Response response;
  try {
    response = await app.handle(
      await toAimRequest(raw),
      onUnhandledError: _logAndRespond,
    );
  } catch (e, st) {
    web.console.error('Failed to process request: $e\n$st'.toJS);
    response = Response.text('Bad Request', statusCode: 400);
  }
  try {
    return toWebResponse(response);
  } catch (e, st) {
    web.console.error('Failed to send response: $e\n$st'.toJS);
    return web.Response(
      'Internal Server Error'.toJS,
      web.ResponseInit(status: 500),
    );
  }
}