embedShelf function

RequestHandler embedShelf(
  1. Handler handler, {
  2. String? handlerPath,
  3. Map<String, Object>? context,
  4. bool throwOnNullResponse = false,
})

Simply passes an incoming request to a shelf handler.

If the handler does not return a shelf.Response, then the result will be passed down the Angel middleware pipeline, like with any other request handler.

If throwOnNullResponse is true (default: false), then a 500 error will be thrown if the handler returns null.

Implementation

RequestHandler embedShelf(shelf.Handler handler,
    {String? handlerPath,
    Map<String, Object>? context,
    bool throwOnNullResponse = false}) {
  return (RequestContext req, ResponseContext res) async {
    var shelfRequest = await convertRequest(req, res,
        handlerPath: handlerPath, context: context);
    try {
      var result = await handler(shelfRequest);
      if (throwOnNullResponse == true) {
        throw AngelHttpException(message: 'Internal Server Error');
      }
      await mergeShelfResponse(result, res);
      return false;
    } on shelf.HijackException {
      // On hijack, do nothing, because the hijack handlers already call res.detach();
      return null;
    } catch (e) {
      rethrow;
    }
  };
}