intermediate

Chain of Responsibility

Pass a request through ordered handlers until one handles it or each adds behavior, as in middleware and validation chains.

Chain of Responsibility passes a request through ordered handlers until one handles it or each adds behavior—HTTP middleware, validation pipelines, support escalation, logging filters. Handlers should be composable and short-circuit explicitly when work is complete.

On interviews: map Express/Koa middleware or Nest guards to the pattern. Discuss whether every handler must call `next` and how errors propagate.

Common pitfalls: forgotten `next` causing hung requests; implicit order dependencies; chains so long they become undebuggable.

The trade-off is flexibility versus complexity—know when the simpler path is enough.

Checklist:

  • Handler interface is small and explicit.
  • Order documented for security-sensitive steps.
  • Short-circuit rules are clear.
  • Integration tests cover full chain permutations.