Rejection in Akka HTTP: Handle Errors Properly
Learn more about rejection in Akka HTTP and how it helps in handling error scenarios in your application.
Join the DZone community and get the full member experience.Join For Free
In this blog, we will demonstrate rejection in the Akka HTTP and how it helps in handling error scenarios in our application. Let's get started.
The concept of rejection in Akka HTTP helps to properly deal with error scenarios. For example, with a filtering directive, like the get directive, it does not allow the request to pass through its inner route. As the filter condition is not satisfied, a rejection concept is invoked.
~ (Tilde) operator is also called the chaining operator. As it is used to connect two or more paths, the Request allows flowing through the routing structure. And try to find another route that can complete it. If a request does not find any route, it will generate a rejection.
You may also like: Rejection Handling in Akka HTTP
By default, a response for rejected requests was generated by the “handle not found” method of
We will understand this in detail through the following example:
If we access the path with “hello,” then we will get the response “Hello world.” At the same time, we will get a response “PONG!” for the route “ping.”
You may have questions like: "What happens if we hit any other route?" We’ll get the following response: “The requested resource could not be found.”
If we want this response in a more descriptive way, we can customize the
RejectionHandler. The easiest way to construct a
RejectionHandler is with
RejectionHandler.newBuilder() that Akka HTTP provides.
Refer to following code snippet:
We’ll get the response as follows:
ErrorResponse is the case class:
case class ErrorResponse(code: Int, message: String, reason: String)
There are three helper methods in
As described in the above example, “Resource Not Found” is special. As it is represented with an empty rejection set, the handle not found helper lets you specify the “recovery route” for this case.
Handles provided the type of rejection with given partial function. This partial function produces a route, which is run when certain rejection occurs.
handleAll[T <: Rejection: ClassTag](f: immutable.Seq[T] => Route):
Handles all rejections of a certain type at the same time. This is useful for cases where you need access to more than the first rejection of a certain type, e. g. for producing the error message to an unsupported request method.
There are many predefined rejections that are provided by Akka HTTP. They are
We invoked these handle calls through implicit definition using “
newBuilder()” to build a new
RejectionHandler. We can tell this handler how to handle a particular rejection.
This rejection occurs when a user uses an unsupported method to access request. In the following example, a supported method is
get. This will generate a rejection when we try to access using
put, or any other method.
For unsupported method, the output will be:
MissingQueryParamRejection arises when the user doesn't mention the required parameters in
In the given example, if we hit URL, localhost:8080/paint?color = red.
In this scenario, the Response would be:
Request has missing required query parameter for = ‘bgColor’.
And if we hit URL, we get “localhost:8080/paint?bgColor = red." Then, Response will be: Request has missing required query parameter for = ‘color’.
When we mention both parameters in the URL localhost:8080/paint?bgColor=red&color=blue, then we get the proper result:
This Rejection is created by the “authorize” directive. This signals that the request was rejected because the user is not authorized.
Let’s see an example:
If we hit the path with the right credential values, such as “localhost:8080/login?username = knoldus&password = pune,” we will get a response as:
When we try to provide access with the wrong values, like “ localhost:8080/login?username = software&password = abcd, ” we will get a response as:
In conclusion, this is how
RejectionHandler can be customized to handle errors in code. You can view the entire code on GitHub.
Thanks for reading! Happy coding!
This article was originally published on the Knoldus blog.
Published at DZone with permission of Praful Bangar. See the original article here.
Opinions expressed by DZone contributors are their own.