respond method

void respond(
  1. dynamic handlerResult, {
  2. required void onError(
    1. String,
    2. String?,
    3. H4Event?
    )?,
  3. required Middleware? afterResponse,
})

Sends the HTTP response based on the handlerResult and terminates the response stream.

If handlerResult is null, the response is closed without sending any content, and the status code is set to 204 (No Content). After the response is sent, the handled flag is set to true, indicating that the request has been fully processed.

Implementation

void respond(dynamic handlerResult,
    {required void Function(String, String?, H4Event?)? onError,
    required Middleware? afterResponse}) {
  // Handle Async Handler
  if (handlerResult is Future) {
    handlerResult
        .then((value) => _resolveRequest(this, value))
        .onError((error, stackTrace) {
      defineErrorHandler(onError ?? defaultErrorMiddleware,
          params: params,
          error: error.toString(),
          trace: stackTrace)(_request);
    });
    return;
  }

  // Handle non-async handler.
  _resolveRequest(this, handlerResult);

  if (afterResponse != null) {
    afterResponse(this);
  }
}