handleRequest method

Future<HttpResponse> handleRequest(
  1. HttpRequest httpRequest
)

Implementation

Future<HttpResponse> handleRequest(HttpRequest httpRequest) async {
  return await runZoned(
    () async {
      try {
        final handler = _findRequestHandler(httpRequest.path);
        final path = httpRequest.path.startsWith(basePath)
            ? httpRequest.path.substring(basePath.length)
            : '';

        final route = (handler is ApiEndpoint)
            ? handler.routePattern.decode(path)
            : Route(RoutePattern.any, path, {}, path);

        //TODO cookies

        final request = ApiRequest(
          httpRequest.method,
          route,
          httpRequest.headers,
          httpRequest.queryParams,
          httpRequest.bodyData,
          null,
        );

        final response = await (middleware?.call(handler) ?? handler)
            .handleRequest(request);

        return response.toHttpResponse(httpRequest.requestUri);
      } on ApiRequestException catch (e) {
        // Exceptions should have been handled by ApiEndpoint, this is just
        // to make sure
        return e.toResponse().toHttpResponse(httpRequest.requestUri);
      } catch (e, stack) {
        // Exceptions should have been handled by ApiEndpoint, this is just
        // to make sure
        if (resolve<ConfigService>().environment == Environment.dev) {
          return DebugResponse(e, stack, 500)
              .toHttpResponse(httpRequest.requestUri);
        } else {
          return ApiRequestException.internalError('Internal Server Error')
              .toResponse()
              .toHttpResponse(httpRequest.requestUri);
        }
      }
    },
    zoneValues: {
      #apiRequestId: _generateRequestId(),
    },
  );
}