receive method Null safety

Future? receive(
  1. Request req
)

Delivers req to this instance to be processed.

This method is the entry point of a Request into this Controller. By default, it invokes this controller's handle method within a try-catch block that guarantees an HTTP response will be sent for Request.

Implementation

Future? receive(Request req) async {
  if (req.isPreflightRequest) {
    return _handlePreflightRequest(req);
  }

  Request? next;
  try {
    try {
      final result = await handle(req);
      if (result is Response) {
        await _sendResponse(req, result, includeCORSHeaders: true);
        logger.info(req.toDebugString());
        return null;
      } else if (result is Request) {
        next = result;
      }
    } on Response catch (response) {
      await _sendResponse(req, response, includeCORSHeaders: true);
      logger.info(req.toDebugString());
      return null;
    } on HandlerException catch (e) {
      await _sendResponse(req, e.response, includeCORSHeaders: true);
      logger.info(req.toDebugString());
      return null;
    }
  } catch (any, stacktrace) {
    print(stacktrace);

    // ignore: unawaited_futures
    handleError(req, any, stacktrace);

    if (letUncaughtExceptionsEscape) {
      rethrow;
    }

    return null;
  }

  if (next == null) {
    return null;
  }

  return nextController?.receive(next);
}