execute method

Future<void> execute()

Executes the route with this Context.

Takes responsibility of executing after and before interceptors. Tries to automatically construct response from returned result if the response is not explicitly set in route handler.

Implementation

Future<void> execute() async {
  dynamic maybeFuture;
  for (int i = 0; i < before.length; i++) {
    maybeFuture = before[i](this);
    if (maybeFuture is Future) await maybeFuture;
  }

  {
    final info = route!.info;
    dynamic res = route!.handler(this);
    if (res is Future) res = await res;

    if (res is Response) {
      response = res;
    } else {
      if (response.body == null) {
        if (info.responseProcessor != null) {
          maybeFuture = info.responseProcessor!(this, res);
          if (maybeFuture is Future) await maybeFuture;
        } else if (res != null) {
          response = StringResponse.cloneFrom(response,
              body: res,
              statusCode: info.statusCode,
              mimeType: info.mimeType,
              charset: info.charset);
        }
      }
    }
  }

  for (int i = after.length - 1; i >= 0; i--) {
    maybeFuture = after[i](this);
    if (maybeFuture is Future) await maybeFuture;
  }
}