03 / 05

What is ExecutionContext in NestJS and why is it central to guards, interceptors, and filters?

ExecutionContext extends ArgumentsHost and provides metadata about the current execution — the transport type (HTTP, WebSocket, RPC) and which handler and class will process it. Its getHandler() and getClass() methods are used with Reflector to read @SetMetadata() values for RBAC and feature flags.

ExecutionContext methods and transport switching
Why ExecutionContext is central:
  1. 1

    getHandler() and getClass() give guards and interceptors access to the matched route metadata.

  2. 2

    switchToHttp(), switchToWs(), switchToRpc() make enhancers transport-agnostic.

  3. 3

    Used by Reflector to read @SetMetadata() values — essential for RBAC, @Public(), feature flags.

  4. 4

    ArgumentsHost (parent interface) is used by exception filters; ExecutionContext extends it with handler/class info.