mirror of
https://github.com/gitbucket/gitbucket.git
synced 2026-07-05 06:59:47 +02:00
show error as json
This commit is contained in:
5
src/main/scala/api/ApiError.scala
Normal file
5
src/main/scala/api/ApiError.scala
Normal file
@@ -0,0 +1,5 @@
|
||||
package api
|
||||
|
||||
case class ApiError(
|
||||
message: String,
|
||||
documentation_url: Option[String] = None)
|
||||
@@ -167,7 +167,7 @@ trait AccountControllerBase extends AccountManagementControllerBase {
|
||||
get("/api/v3/user") {
|
||||
context.loginAccount.map { account =>
|
||||
JsonFormat(ApiUser(account))
|
||||
} getOrElse NotFound
|
||||
} getOrElse Unauthorized
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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("/"))
|
||||
|
||||
@@ -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.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user