Distinguish rejections from exceptions in Akka HTTP: handle routing mismatches and runtime failures
In Akka HTTP’s Routing DSL, a rejection signals that the current route cannot handle a request and leaves later alternatives a chance to do so. An exception is a failure during route execution that bubbles to an enclosing exception handler. Keeping them distinct lets an API respond differently to a wrong path or method and to an internal server failure. This guide follows the current official Akka HTTP routing documentation checked on 2026-11-20; the pages did not identify a specific library release.
In Akka HTTP’s Routing DSL, a rejection signals that the current route cannot handle a request and leaves later alternatives a chance to do so. An exception is a failure during route execution that bubbles to an enclosing exception handler. Keeping them distinct lets an API respond differently to a wrong path or method and to an internal server failure. This guide follows the current official Akka HTTP routing documentation checked on 2026-11-20; the pages did not identify a specific library release.
Rejections leave room for alternative routes
A filtering directive such as get does not immediately finish with a 4xx response when the method does not match. It produces a MethodRejection, because a later route in concat may handle the same request. If no route completes it, an outer handleRejections or Route.seal converts the accumulated rejections into an HTTP response.
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route
val orders: Route =
path("orders") {
concat(
get { complete("GET /orders") },
post { complete("POST /orders") }
)
}GET /orders completes in the GET branch, and POST /orders in the POST branch. If the top-level route is sealed, the default handler normally turns an unsupported method into 405 Method Not Allowed. A path directive’s empty rejection set means the resource was not found and maps to the default 404 Not Found. Check exact response bodies and customized statuses against your library version and handler settings.
A custom RejectionHandler can handle a rejection type with handle, inspect several of one type with handleAll, and handle an empty set with handleNotFound. Let route branches try the request first; customize only the final responses needed by your API contract.
Exceptions are execution failures
Exceptions thrown during route execution bubble to an enclosing handleExceptions. Route.seal also applies exception handling. The official docs say the default handler handles unhandled NonFatal exceptions with a 500 response, logs a stack trace, and may include the exception message in the response body. Review production response and logging policies so detailed messages or internal implementation information are not exposed to clients.
Ordinary input validation failures are part of expected request processing. Akka HTTP advises against using the exception handler for these; turn validation results into explicit responses. Use exception handling for unexpected execution failures, and define a stable public error format that hides internal exception details.
| Situation | Akka HTTP flow | Design direction |
|---|---|---|
| Path does not match | Empty rejection set | Default 404 or custom not-found response |
| Method misses one route, another may match | MethodRejection, try another branch | Offer alternatives with concat |
| No branch completes the request | Rejection list is handled | Return contract-appropriate response in a RejectionHandler |
| Unexpected exception during execution | Exception bubbles outward | Fail safely and log via ExceptionHandler |
| User input violates a rule |
Verification steps
Split path and method branches into small routes; use `concat` for alternatives.
Confirm that supported methods receive successful responses from their branches.
Send an unsupported method to the same path; verify it tries other branches and then gets an appropriate 405 from top-level rejection handling.
Confirm a nonexistent path ends in the default or custom 404.
In a test environment only, throw a controlled exception and inspect the handler’s status, body, and logs. Check that the response contains no stack trace or sensitive internals.
Test validation failures separately to ensure they return the intended public response without throwing.
Akka HTTP Route TestKit can test routes, but the example here was not run. Check version-specific APIs and default handlers against your project’s dependency version.
References
[Akka HTTP rejections](https://doc.akka.io/libraries/akka-http/current/routing-dsl/rejections.html)
[Akka HTTP exception handling](https://doc.akka.io/libraries/akka-http/current/routing-dsl/exception-handling.html)
[Akka HTTP method directive](https://doc.akka.io/libraries/akka-http/current/routing-dsl/directives/method-directives/method.html)
[Akka HTTP Routing DSL and Route TestKit](https://doc.akka.io/libraries/akka-http/current/routing-dsl/index.html)