Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

more exception handling examples

  • Loading branch information...
commit e75f0c0de4c27438f6fdbfe1b89181af2533ded4 1 parent ac63db2
@twoism twoism authored
Showing with 19 additions and 1 deletion.
  1. +19 −1 src/test/scala/com/twitter/finatra/ExampleSpec.scala
View
20 src/test/scala/com/twitter/finatra/ExampleSpec.scala
@@ -115,12 +115,24 @@ class ExampleSpec extends SpecHelper {
render.plain("we never make it here").toFuture
}
+ /**
+ * Custom Error Handling with custom Exception
+ *
+ * curl http://localhost:7070/unautorized
+ */
+ class Unauthorized extends Exception
+
+ get("/unauthorized") { request =>
+ throw new Unauthorized
+ }
+
error { request =>
request.error match {
case Some(e:ArithmeticException) =>
render.status(500).plain("whoops, divide by zero!").toFuture
+ case Some(e:Unauthorized) =>
+ render.status(401).plain("Not Authorized!").toFuture
case _ =>
- println(request.error)
render.status(500).plain("Something went wrong!").toFuture
}
}
@@ -155,6 +167,12 @@ class ExampleSpec extends SpecHelper {
response.code should equal (500)
}
+ "GET /unauthorized" should "respond 401" in {
+ get("/unauthorized")
+ response.body should equal ("Not Authorized!")
+ response.code should equal (401)
+ }
+
"GET /hello" should "respond with hello world" in {
get("/")
response.body should equal ("hello world")
Please sign in to comment.
Something went wrong with that request. Please try again.