processHandlersWithMiddleware method

Future<void> processHandlersWithMiddleware(
  1. Future<Response> initialHandler(
    1. Context context
    ),
  2. List<Function> allMiddlewares,
  3. Context context,
  4. HttpRequest request,
)

Implementation

Future<void> processHandlersWithMiddleware(
    Future<Response> Function(Context context) initialHandler,
    List<Function> allMiddlewares,
    Context context,
    HttpRequest request) async {
  try {
    var handler = initialHandler;
    allMiddlewares.forEach((element) async {
      handler = element(handler);
    });

    final response = handler(context);
    await writeResponse(request, response);
  } catch (err, stacktrace) {
    await writeErrorResponse(request, err, stacktrace);
  }
}