Chaining rejection handlers

September 4, 2015

To serve some resources via Spray, it’s as easy as using the ‘getFromFile’ directive. If you want to fall back to an alternative because the file is not available, you can define a RejectionHandler to serve an alternative file.

In a web application I wanted to try some alternative names before falling back to a default image. The default solution requires you to nest all RejectionHandlers.

 def alternativesHandler = RejectionHandler {    
  case rejection =>    
   handleRejection(RejectionHandler {    
    case rejection => getFromFile("second-alternative")    
   }) {    
    getFromFile("first-alternative")    
   }    
 }    

This becomes quite messy very fast and is not very easy to read.
Chaining instead of nesting would improve it quite a bit.

 def alternativesHandler = chaining(RejectionHandler {    
  case rejection => getFromFile("first-alternative")    
 } >> RejectionHandler {    
  case rejection => getFromFile("second-alternative")    
 } >> RejectionHandler {    
  case rejection => getFromFile("third-alternative")    
 })

Here is the code which makes this possible. It only needs to be imported where the route is being defined. The ‘chaining’ method just tries each handler in the List. The implicit classes extends the classes with a ‘»’ method which allows the handlers to be chained in a list.

 trait RejectionHandlingChain {    
  type RejectionHandlerList = List[RejectionHandler]    

  implicit class RejectionHandlerListExt(handler: RejectionHandlerList) {    
   def >>(other: RejectionHandler): RejectionHandlerList = handler :+ other    
  }    

  implicit class RejectionHandlerExt(handler: RejectionHandler) {    
   def >>(other: RejectionHandler): RejectionHandlerList = List(handler, other)    
  }    

  import spray.routing.directives.RouteDirectives.reject    
  import spray.routing.directives.ExecutionDirectives.handleRejections    

  final def chaining(handlers: RejectionHandlerList): RejectionHandler = RejectionHandler {    
   case rejection => handlers match {    
    case Nil     =>    
     reject(rejection: _*)    
    case head :: tail =>    
     handleRejections(chaining(tail)) {    
      head(rejection)    
     }    
   }    
  }    
 }    

 object RejectionHandlingChain extends RejectionHandlingChain

Interesting cases & blogs

November 17, 2026

Van schuur tot stroom: mijn thuisbatterij van 128 kWh in productie

Sinds 30 oktober 2026 draait ons hele huis achter drie omvormers en 128 kWh aan zelfgebouwde batterijen. In de vorige […]

September 4, 2026

Building my own energy storage system

The spark Since I was young I’ve been playing with wires, batteries, and small light bulbs — model electric trains, […]

Scroll to Top