show error as json

This commit is contained in:
nazoking
2015-02-27 17:08:25 +09:00
parent 597f86dc7b
commit 415519716e
4 changed files with 20 additions and 1 deletions

View File

@@ -0,0 +1,5 @@
package api
case class ApiError(
message: String,
documentation_url: Option[String] = None)

View File

@@ -167,7 +167,7 @@ trait AccountControllerBase extends AccountManagementControllerBase {
get("/api/v3/user") {
context.loginAccount.map { account =>
JsonFormat(ApiUser(account))
} getOrElse NotFound
} getOrElse Unauthorized
}

View File

@@ -51,6 +51,9 @@ abstract class ControllerBase extends ScalatraFilter
// Git repository
chain.doFilter(request, response)
} else {
if(path.startsWith("/api/v3/")){
httpRequest.setAttribute(Keys.Request.APIv3, true)
}
// Scalatra actions
super.doFilter(request, response, chain)
}
@@ -103,6 +106,9 @@ abstract class ControllerBase extends ScalatraFilter
protected def NotFound() =
if(request.hasAttribute(Keys.Request.Ajax)){
org.scalatra.NotFound()
} else if(request.hasAttribute(Keys.Request.APIv3)){
contentType = formats("json")
org.scalatra.NotFound(api.ApiError("Not Found"))
} else {
org.scalatra.NotFound(html.error("Not Found"))
}
@@ -110,6 +116,9 @@ abstract class ControllerBase extends ScalatraFilter
protected def Unauthorized()(implicit context: app.Context) =
if(request.hasAttribute(Keys.Request.Ajax)){
org.scalatra.Unauthorized()
} else if(request.hasAttribute(Keys.Request.APIv3)){
contentType = formats("json")
org.scalatra.Unauthorized(api.ApiError("Requires authentication"))
} else {
if(context.loginAccount.isDefined){
org.scalatra.Unauthorized(redirect("/"))

View File

@@ -71,6 +71,11 @@ object Keys {
*/
val Ajax = "AJAX"
/**
* Request key for the /api/v3 request flag.
*/
val APIv3 = "APIv3"
/**
* Request key for the username which is used during Git repository access.
*/