akka-http non-standard status codes

216 views
Skip to first unread message

Patrick O'Reilly

unread,
Jan 4, 2017, 12:46:09 PM1/4/17
to Akka User List
Hi,

We're using Akka HTTP client to consume a remote API. This API however is returning non-standard status codes in the 600's which cannot be parsed by Akka. akka.http.ParserSettings.withCustomStatusCodes only allow valid 100-599 codes. Does anyone have a solution to this problem? The exception thrown is an illegal response status but the exception has no details on the actual status code.

Thanks,
Patrick

Konrad Malawski

unread,
Jan 4, 2017, 12:49:21 PM1/4/17
to akka...@googlegroups.com, Patrick O'Reilly
Everything is configurable :-)

I see we missed to document that setting. We documented how to do it for custom media types here: http://doc.akka.io/docs/akka-http/current/scala/http/routing-dsl/directives/method-directives/extractMethod.html#custom-http-method

Which is basically the same process you'll need to do for the status code:
val parserSettings = ParserSettings(system).withCustomStatusCodes(StatusCodes.custom(666, "BadStatusCode", "The number of the beast", false, false))
val serverSettings = ServerSettings(system).withParserSettings(parserSettings)
val binding = Http().bindAndHandle(routes, host, port, settings = serverSettings)

I'll add that to the docs.

Happy hakking

-- 
Konrad `ktoso` Malawski
Akka @ Lightbend
--
>>>>>>>>>> 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 akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Patrick O'Reilly

unread,
Jan 4, 2017, 1:13:34 PM1/4/17
to Akka User List, padjo....@gmail.com
Hi Konrad,

Thanks for the prompt reply. I've tried the StatusCodes.custom call with a non-standard status code but it throws the following exception:

Exception in thread "main" java.lang.IllegalArgumentException: Can't register status code in non-standard region
at akka.http.scaladsl.model.StatusCodes$.custom(StatusCode.scala:92)
at AkkaHttp2$.delayedEndpoint$AkkaHttp2$1(AkkaHttp2.scala:47)
at AkkaHttp2$delayedInit$body.apply(AkkaHttp2.scala:16)
at scala.Function0$class.apply$mcV$sp(Function0.scala:34)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App$$anonfun$main$1.apply(App.scala:76)
at scala.App$$anonfun$main$1.apply(App.scala:76)
at scala.collection.immutable.List.foreach(List.scala:381)
at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
at scala.App$class.main(App.scala:76)
at AkkaHttp2$.main(AkkaHttp2.scala:16)
at AkkaHttp2.main(AkkaHttp2.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

It seems StatusCodes.custom specifically doesn't allow codes outside the normal range:

def custom(intValue: Int, reason: String, defaultMessage: String = ""): StatusCode =
    if (100 to 199 contains intValue) Informational(intValue)(reason, defaultMessage)
    else if (200 to 299 contains intValue) Success(intValue)(reason, defaultMessage)
    else if (300 to 399 contains intValue) Redirection(intValue)(reason, defaultMessage, defaultMessage)
    else if (400 to 499 contains intValue) ClientError(intValue)(reason, defaultMessage)
    else if (500 to 599 contains intValue) ServerError(intValue)(reason, defaultMessage)
    else throw new IllegalArgumentException("Can't register status code in non-standard region")

Best,
Patrick

Konrad Malawski

unread,
Jan 4, 2017, 1:15:03 PM1/4/17
to akka...@googlegroups.com, Patrick O'Reilly
Please read my response in detail: 
Use the 4 parameter version: StatusCodes.custom(666, "BadStatusCode", "The number of the beast", false, false)

Since it's not a standard range you must provide the additional information since we can't guess it.

-- 
Konrad `ktoso` Malawski
Akka @ Lightbend

Konrad Malawski

unread,
Jan 4, 2017, 1:15:41 PM1/4/17
to akka...@googlegroups.com, Patrick O'Reilly
5* parameter :)

-- 
Konrad `ktoso` Malawski
Akka @ Lightbend

Viktor Klang

unread,
Jan 4, 2017, 1:17:22 PM1/4/17
to Akka User List, Patrick O'Reilly
How about having that exception message suggest using the other factory method?

--
Cheers,

To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+unsubscribe@googlegroups.com.

To post to this group, send email to akka...@googlegroups.com.
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 akka-user+unsubscribe@googlegroups.com.

Konrad Malawski

unread,
Jan 4, 2017, 1:17:55 PM1/4/17
to akka...@googlegroups.com, Viktor Klang, Patrick O'Reilly
Sure, why not.

-- 
Konrad `ktoso` Malawski
Akka @ Lightbend

Patrick O'Reilly

unread,
Jan 4, 2017, 1:21:34 PM1/4/17
to Akka User List, padjo....@gmail.com
Aha. Did not realize there was a separate method. Thanks a lot for your help!

Best,
Patrick

Konrad Malawski

unread,
Jan 4, 2017, 1:38:53 PM1/4/17
to akka...@googlegroups.com, Patrick O'Reilly
PR with docs and improved exception: https://github.com/akka/akka-http/pull/714/

Happy hakking!

-- 
Konrad `ktoso` Malawski
Akka @ Lightbend

Reply all
Reply to author
Forward
0 new messages