Hi there,
Read the documentation about exception handlers in Akka http. That should
get you what you want

-- 
Konrad Malawski

On April 25, 2017 at 8:13:24, Andras Szerdahelyi (
[email protected]) wrote:

> ( I apologize for the re-post if you've seen this under the akka /
> akka-http tag at stackoverflow already )
> Hello list,
>
> I have a need to encode exception messages in to a specific JSON format in
> my http responses. I was wondering how I could catch a
> DeserializationException in my route or outside of it or encode the
> DeserializationException.
>
> I tried the following:
>
> 1) the below exception handler around my route:
>
> val exceptionHandler = ExceptionHandler {
>     case e: DeserializationException => complete(StatusCodes.BadRequest,
> ServiceBrokerError(e.getMessage))
> }
> 2) JSON format in implicit scope for DeserializationException
>
> implicit object DeserializationExceptionFormat extends DefaultJsonProtocol
> with RootJsonFormat[DeserializationException] {
>
>   def write(e: DeserializationException) = JsObject("message" ->
> JsString(e.getMessage))
>   def read(v: JsValue) = throw new NotImplementedError()
>
> }
> Neither of these made any difference and DeserializationExceptions are
> still encoded in to the http response body per below:
>
> HTTP/1.1 400 Bad Request
> Content-Length: 74
> Content-Type: text/plain; charset=UTF-8
> Date: Thu, 20 Apr 2017 21:23:11 GMT
> Server: akka-http/10.0.1
>
> The request content was malformed:
> Node count may not be a floating number
>
> Some additional context- my routes rely on the spray-json integration to
> map request entities to objects, such as:
>
> // service instance management related routes
> put {
>   entity(as[CreateInstance]) { createInstance =>
> handleCreateInstance(s"cluster-$clusterId", createInstance) }
> }
>
> it is the DeserializationException that I throw from the Format
> implementation of CreateInstance that I would like to capture somewhere in
> or around my routes..
>
> Any suggestions are greatly appreciated.
>
>
> --
> >>>>>>>>>> Read the docs: http://akka.io/docs/
> >>>>>>>>>> Check the FAQ:
> http://doc.akka.io/docs/akka/current/additional/faq.html
> >>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
> ---
> You received this message because you are subscribed to the Google Groups
> "Akka User List" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/akka-user.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to