Fix error responses

This commit is contained in:
Naoki Takezoe
2016-10-03 16:01:57 +09:00
parent 82b102845f
commit 2621de2cde
11 changed files with 80 additions and 81 deletions

View File

@@ -134,7 +134,7 @@ trait AccountControllerBase extends AccountManagementControllerBase {
context.loginAccount.exists(x => members.exists { member => member.userName == x.userName && member.isManager }))
}
}
} getOrElse NotFound
} getOrElse NotFound()
}
get("/:userName.atom") {
@@ -157,7 +157,7 @@ trait AccountControllerBase extends AccountManagementControllerBase {
val userName = params("userName")
getAccountByUserName(userName).map { x =>
html.edit(x, flash.get("info"), flash.get("error"))
} getOrElse NotFound
} getOrElse NotFound()
})
post("/:userName/_edit", editForm)(oneselfOnly { form =>
@@ -173,7 +173,7 @@ trait AccountControllerBase extends AccountManagementControllerBase {
flash += "info" -> "Account information has been updated."
redirect(s"/${userName}/_edit")
} getOrElse NotFound
} getOrElse NotFound()
})
get("/:userName/_delete")(oneselfOnly {
@@ -197,14 +197,14 @@ trait AccountControllerBase extends AccountManagementControllerBase {
session.invalidate
redirect("/")
}
} getOrElse NotFound
} getOrElse NotFound()
})
get("/:userName/_ssh")(oneselfOnly {
val userName = params("userName")
getAccountByUserName(userName).map { x =>
html.ssh(x, getPublicKeys(x.userName))
} getOrElse NotFound
} getOrElse NotFound()
})
post("/:userName/_ssh", sshKeyForm)(oneselfOnly { form =>
@@ -235,7 +235,7 @@ trait AccountControllerBase extends AccountManagementControllerBase {
case _ => None
}
html.application(x, tokens, generatedToken)
} getOrElse NotFound
} getOrElse NotFound()
})
post("/:userName/_personalToken", personalTokenForm)(oneselfOnly { form =>
@@ -261,7 +261,7 @@ trait AccountControllerBase extends AccountManagementControllerBase {
} else {
html.register()
}
} else NotFound
} else NotFound()
}
post("/register", newForm){ form =>
@@ -269,7 +269,7 @@ trait AccountControllerBase extends AccountManagementControllerBase {
createAccount(form.userName, sha1(form.password), form.fullName, form.mailAddress, false, form.url)
updateImage(form.userName, form.fileId, false)
redirect("/signin")
} else NotFound
} else NotFound()
}
get("/groups/new")(usersOnly {
@@ -330,7 +330,7 @@ trait AccountControllerBase extends AccountManagementControllerBase {
updateImage(form.groupName, form.fileId, form.clearImage)
redirect(s"/${form.groupName}")
} getOrElse NotFound
} getOrElse NotFound()
}
})
@@ -372,7 +372,7 @@ trait AccountControllerBase extends AccountManagementControllerBase {
)
case _ => redirect(s"/${loginUserName}")
}
} else BadRequest
} else BadRequest()
})
post("/:owner/:repository/fork", accountForm)(readableUsersOnly { (form, repository) =>
@@ -429,7 +429,7 @@ trait AccountControllerBase extends AccountManagementControllerBase {
redirect(s"/${accountName}/${repository.name}")
}
}
} else BadRequest
} else BadRequest()
})
private def existsAccount: Constraint = new Constraint(){

View File

@@ -66,7 +66,7 @@ trait ApiControllerBase extends ControllerBase {
get("/api/v3/orgs/:groupName") {
getAccountByUserName(params("groupName")).filter(account => account.isGroupAccount).map { account =>
JsonFormat(ApiUser(account))
} getOrElse NotFound
} getOrElse NotFound()
}
/**
@@ -75,7 +75,7 @@ trait ApiControllerBase extends ControllerBase {
get("/api/v3/users/:userName") {
getAccountByUserName(params("userName")).filterNot(account => account.isGroupAccount).map { account =>
JsonFormat(ApiUser(account))
} getOrElse NotFound
} getOrElse NotFound()
}
/**
@@ -145,7 +145,7 @@ trait ApiControllerBase extends ControllerBase {
get("/api/v3/user") {
context.loginAccount.map { account =>
JsonFormat(ApiUser(account))
} getOrElse Unauthorized
} getOrElse Unauthorized()
}
/**
@@ -179,7 +179,7 @@ trait ApiControllerBase extends ControllerBase {
)
}
}
}) getOrElse NotFound
}) getOrElse NotFound()
})
/**
@@ -203,7 +203,7 @@ trait ApiControllerBase extends ControllerBase {
)
}
}
}) getOrElse NotFound
}) getOrElse NotFound()
})
/**
@@ -221,7 +221,7 @@ trait ApiControllerBase extends ControllerBase {
disableBranchProtection(repository.owner, repository.name, branch)
}
JsonFormat(ApiBranch(branch, protection)(RepositoryName(repository)))
}) getOrElse NotFound
}) getOrElse NotFound()
})
/**
@@ -243,7 +243,7 @@ trait ApiControllerBase extends ControllerBase {
comments = getCommentsForApi(repository.owner, repository.name, issueId.toInt)
} yield {
JsonFormat(comments.map{ case (issueComment, user, issue) => ApiComment(issueComment, RepositoryName(repository), issueId, ApiUser(user), issue.isPullRequest) })
}).getOrElse(NotFound)
}) getOrElse NotFound()
})
/**
@@ -259,7 +259,7 @@ trait ApiControllerBase extends ControllerBase {
issueComment <- getComment(repository.owner, repository.name, id.toString())
} yield {
JsonFormat(ApiComment(issueComment, RepositoryName(repository), issueId, ApiUser(context.loginAccount.get), issue.isPullRequest))
}) getOrElse NotFound
}) getOrElse NotFound()
})
/**
@@ -393,7 +393,7 @@ trait ApiControllerBase extends ControllerBase {
ApiRepository(headRepo, ApiUser(headOwner)),
ApiRepository(repository, ApiUser(baseOwner)),
ApiUser(issueUser)))
}).getOrElse(NotFound)
}) getOrElse NotFound()
})
/**
@@ -412,7 +412,7 @@ trait ApiControllerBase extends ControllerBase {
JsonFormat(commits)
}
}
} getOrElse NotFound
} getOrElse NotFound()
})
/**
@@ -437,7 +437,7 @@ trait ApiControllerBase extends ControllerBase {
status <- getCommitStatus(repository.owner, repository.name, statusId)
} yield {
JsonFormat(ApiCommitStatus(status, ApiUser(creator)))
}) getOrElse NotFound
}) getOrElse NotFound()
})
/**
@@ -453,7 +453,7 @@ trait ApiControllerBase extends ControllerBase {
JsonFormat(getCommitStatuesWithCreator(repository.owner, repository.name, sha).map{ case(status, creator) =>
ApiCommitStatus(status, ApiUser(creator))
})
}) getOrElse NotFound
}) getOrElse NotFound()
})
/**
@@ -478,7 +478,7 @@ trait ApiControllerBase extends ControllerBase {
} yield {
val statuses = getCommitStatuesWithCreator(repository.owner, repository.name, sha)
JsonFormat(ApiCombinedCommitStatus(sha, statuses, ApiRepository(repository, owner)))
}) getOrElse NotFound
}) getOrElse NotFound()
})
private def isEditable(owner: String, repository: String, author: String)(implicit context: Context): Boolean =

View File

@@ -248,7 +248,7 @@ trait AccountManagementControllerBase extends ControllerBase {
protected def reservedNames(): Constraint = new Constraint(){
override def validate(name: String, value: String, messages: Messages): Option[String] = if(allReservedNames.contains(value)){
Some(s"${value} is reserved")
}else{
} else {
None
}
}

View File

@@ -48,7 +48,7 @@ class FileUploadController extends ScalatraServlet with FileUploadSupport with R
// Check whether logged-in user is collaborator
collaboratorsOnly(owner, repository, loginAccount){
execute({ (file, fileId) =>
val fileName = file.getName
val fileName = file.getName
LockUtil.lock(s"${owner}/${repository}/wiki") {
using(Git.open(Directory.getWikiRepositoryDir(owner, repository))) { git =>
val builder = DirCache.newInCore.builder()
@@ -75,7 +75,7 @@ class FileUploadController extends ScalatraServlet with FileUploadSupport with R
}
}, FileUtil.isUploadableType)
}
} getOrElse BadRequest
} getOrElse BadRequest()
}
post("/import") {
@@ -93,7 +93,7 @@ class FileUploadController extends ScalatraServlet with FileUploadSupport with R
loginAccount match {
case x if(x.isAdmin) => action
case x if(getCollaborators(owner, repository).contains(x.userName)) => action
case _ => BadRequest
case _ => BadRequest()
}
}
@@ -101,10 +101,9 @@ class FileUploadController extends ScalatraServlet with FileUploadSupport with R
case Some(file) if(mimeTypeChcker(file.name)) =>
defining(FileUtil.generateFileId){ fileId =>
f(file, fileId)
Ok(fileId)
}
case _ => BadRequest
case _ => BadRequest()
}
}

View File

@@ -72,7 +72,7 @@ trait IssuesControllerBase extends ControllerBase {
getLabels(owner, name),
hasWritePermission(owner, name, context.loginAccount),
repository)
} getOrElse NotFound
} getOrElse NotFound()
}
})
@@ -139,8 +139,8 @@ trait IssuesControllerBase extends ControllerBase {
createReferComment(owner, name, issue.copy(title = title), title, context.loginAccount.get)
redirect(s"/${owner}/${name}/issues/_data/${issue.issueId}")
} else Unauthorized
} getOrElse NotFound
} else Unauthorized()
} getOrElse NotFound()
}
})
@@ -154,8 +154,8 @@ trait IssuesControllerBase extends ControllerBase {
createReferComment(owner, name, issue, content.getOrElse(""), context.loginAccount.get)
redirect(s"/${owner}/${name}/issues/_data/${issue.issueId}")
} else Unauthorized
} getOrElse NotFound
} else Unauthorized()
} getOrElse NotFound()
}
})
@@ -166,7 +166,7 @@ trait IssuesControllerBase extends ControllerBase {
redirect(s"/${repository.owner}/${repository.name}/${
if(issue.isPullRequest) "pull" else "issues"}/${form.issueId}#comment-${id}")
}
} getOrElse NotFound
} getOrElse NotFound()
})
post("/:owner/:repository/issue_comments/state", issueStateForm)(readableUsersOnly { (form, repository) =>
@@ -176,7 +176,7 @@ trait IssuesControllerBase extends ControllerBase {
redirect(s"/${repository.owner}/${repository.name}/${
if(issue.isPullRequest) "pull" else "issues"}/${form.issueId}#comment-${id}")
}
} getOrElse NotFound
} getOrElse NotFound()
})
ajaxPost("/:owner/:repository/issue_comments/edit/:id", commentForm)(readableUsersOnly { (form, repository) =>
@@ -185,8 +185,8 @@ trait IssuesControllerBase extends ControllerBase {
if(isEditable(owner, name, comment.commentedUserName)){
updateComment(comment.commentId, form.content)
redirect(s"/${owner}/${name}/issue_comments/_data/${comment.commentId}")
} else Unauthorized
} getOrElse NotFound
} else Unauthorized()
} getOrElse NotFound()
}
})
@@ -195,8 +195,8 @@ trait IssuesControllerBase extends ControllerBase {
getComment(owner, name, params("id")).map { comment =>
if(isEditable(owner, name, comment.commentedUserName)){
Ok(deleteComment(comment.commentId))
} else Unauthorized
} getOrElse NotFound
} else Unauthorized()
} getOrElse NotFound()
}
})
@@ -223,8 +223,8 @@ trait IssuesControllerBase extends ControllerBase {
)
)
}
} else Unauthorized
} getOrElse NotFound
} else Unauthorized()
} getOrElse NotFound()
})
ajaxGet("/:owner/:repository/issue_comments/_data/:id")(readableUsersOnly { repository =>
@@ -249,8 +249,8 @@ trait IssuesControllerBase extends ControllerBase {
)
)
}
} else Unauthorized
} getOrElse NotFound
} else Unauthorized()
} getOrElse NotFound()
})
ajaxPost("/:owner/:repository/issues/new/label")(collaboratorsOnly { repository =>
@@ -284,7 +284,7 @@ trait IssuesControllerBase extends ControllerBase {
getMilestonesWithIssueCount(repository.owner, repository.name)
.find(_._1.milestoneId == milestoneId).map { case (_, openCount, closeCount) =>
gitbucket.core.issues.milestones.html.progress(openCount + closeCount, closeCount)
} getOrElse NotFound
} getOrElse NotFound()
} getOrElse Ok()
})
@@ -313,7 +313,7 @@ trait IssuesControllerBase extends ControllerBase {
registerIssueLabel(repository.owner, repository.name, issueId, labelId)
}
}
} getOrElse NotFound
} getOrElse NotFound()
})
post("/:owner/:repository/issues/batchedit/assign")(collaboratorsOnly { repository =>
@@ -340,7 +340,7 @@ trait IssuesControllerBase extends ControllerBase {
RawData(FileUtil.getMimeType(file.getName), file)
}
case _ => None
}) getOrElse NotFound
}) getOrElse NotFound()
})
val assignedUserName = (key: String) => params.get(key) filter (_.trim != "")

View File

@@ -42,7 +42,7 @@ trait MilestonesControllerBase extends ControllerBase {
get("/:owner/:repository/issues/milestones/:milestoneId/edit")(collaboratorsOnly { repository =>
params("milestoneId").toIntOpt.map{ milestoneId =>
html.edit(getMilestone(repository.owner, repository.name, milestoneId), repository)
} getOrElse NotFound
} getOrElse NotFound()
})
post("/:owner/:repository/issues/milestones/:milestoneId/edit", milestoneForm)(collaboratorsOnly { (form, repository) =>
@@ -51,7 +51,7 @@ trait MilestonesControllerBase extends ControllerBase {
updateMilestone(milestone.copy(title = form.title, description = form.description, dueDate = form.dueDate))
redirect(s"/${repository.owner}/${repository.name}/issues/milestones")
}
} getOrElse NotFound
} getOrElse NotFound()
})
get("/:owner/:repository/issues/milestones/:milestoneId/close")(collaboratorsOnly { repository =>
@@ -60,7 +60,7 @@ trait MilestonesControllerBase extends ControllerBase {
closeMilestone(milestone)
redirect(s"/${repository.owner}/${repository.name}/issues/milestones")
}
} getOrElse NotFound
} getOrElse NotFound()
})
get("/:owner/:repository/issues/milestones/:milestoneId/open")(collaboratorsOnly { repository =>
@@ -69,7 +69,7 @@ trait MilestonesControllerBase extends ControllerBase {
openMilestone(milestone)
redirect(s"/${repository.owner}/${repository.name}/issues/milestones")
}
} getOrElse NotFound
} getOrElse NotFound()
})
get("/:owner/:repository/issues/milestones/:milestoneId/delete")(collaboratorsOnly { repository =>
@@ -78,7 +78,7 @@ trait MilestonesControllerBase extends ControllerBase {
deleteMilestone(repository.owner, repository.name, milestone.milestoneId)
redirect(s"/${repository.owner}/${repository.name}/issues/milestones")
}
} getOrElse NotFound
} getOrElse NotFound()
})
}

View File

@@ -104,7 +104,7 @@ trait PullRequestsControllerBase extends ControllerBase {
flash.toMap.map(f => f._1 -> f._2.toString))
}
}
} getOrElse NotFound
} getOrElse NotFound()
})
ajaxGet("/:owner/:repository/pull/:id/mergeguide")(referrersOnly { repository =>
@@ -138,7 +138,7 @@ trait PullRequestsControllerBase extends ControllerBase {
repository,
getRepository(pullreq.requestUserName, pullreq.requestRepositoryName).get)
}
} getOrElse NotFound
} getOrElse NotFound()
})
get("/:owner/:repository/pull/:id/delete/*")(collaboratorsOnly { repository =>
@@ -153,7 +153,7 @@ trait PullRequestsControllerBase extends ControllerBase {
}
createComment(repository.owner, repository.name, userName, issueId, branchName, "delete_branch")
redirect(s"/${repository.owner}/${repository.name}/pull/${issueId}")
} getOrElse NotFound
} getOrElse NotFound()
})
post("/:owner/:repository/pull/:id/update_branch")(referrersOnly { baseRepository =>
@@ -222,7 +222,7 @@ trait PullRequestsControllerBase extends ControllerBase {
}
redirect(s"/${repository.owner}/${repository.name}/pull/${issueId}")
}
}) getOrElse NotFound
}) getOrElse NotFound()
})
post("/:owner/:repository/pull/:id/merge", mergeForm)(collaboratorsOnly { (form, repository) =>
@@ -273,7 +273,7 @@ trait PullRequestsControllerBase extends ControllerBase {
}
}
}
} getOrElse NotFound
} getOrElse NotFound()
})
get("/:owner/:repository/compare")(referrersOnly { forkedRepository =>
@@ -290,7 +290,7 @@ trait PullRequestsControllerBase extends ControllerBase {
redirect(s"/${forkedRepository.owner}/${forkedRepository.name}/compare/${originUserName}:${oldBranch}...${newBranch}")
}
} getOrElse NotFound
} getOrElse NotFound()
}
case _ => {
using(Git.open(getRepositoryDir(forkedRepository.owner, forkedRepository.name))){ git =>
@@ -386,7 +386,7 @@ trait PullRequestsControllerBase extends ControllerBase {
s"${forkedOwner}:${newId.map(_ => forkedId).getOrElse(forkedRepository.repository.defaultBranch)}")
}
}
}) getOrElse NotFound
}) getOrElse NotFound()
})
ajaxGet("/:owner/:repository/compare/*...*/mergecheck")(collaboratorsOnly { forkedRepository =>
@@ -416,7 +416,7 @@ trait PullRequestsControllerBase extends ControllerBase {
}
html.mergecheck(conflict)
}
}) getOrElse NotFound
}) getOrElse NotFound()
})
post("/:owner/:repository/pulls/new", pullRequestForm)(referrersOnly { (form, repository) =>

View File

@@ -300,7 +300,7 @@ trait RepositorySettingsControllerBase extends ControllerBase {
get("/:owner/:repository/settings/hooks/edit")(ownerOnly { repository =>
getWebHook(repository.owner, repository.name, params("url")).map{ case (webhook, events) =>
html.edithooks(webhook, events, repository, flash.get("info"), false)
} getOrElse NotFound
} getOrElse NotFound()
})
/**

View File

@@ -152,7 +152,7 @@ trait RepositoryViewerControllerBase extends ControllerBase {
logs.splitWith{ (commit1, commit2) =>
view.helpers.date(commit1.commitTime) == view.helpers.date(commit2.commitTime)
}, page, hasNext, hasWritePermission(repository.owner, repository.name, context.loginAccount))
case Left(_) => NotFound
case Left(_) => NotFound()
}
}
})
@@ -177,7 +177,7 @@ trait RepositoryViewerControllerBase extends ControllerBase {
html.editor(branch, repository, paths.take(paths.size - 1).toList, Some(paths.last),
JGitUtil.getContentInfo(git, path, objectId),
protectedBranch)
} getOrElse NotFound
} getOrElse NotFound()
}
})
@@ -190,7 +190,7 @@ trait RepositoryViewerControllerBase extends ControllerBase {
val paths = path.split("/")
html.delete(branch, repository, paths.take(paths.size - 1).toList, paths.last,
JGitUtil.getContentInfo(git, path, objectId))
} getOrElse NotFound
} getOrElse NotFound()
}
})
@@ -250,7 +250,7 @@ trait RepositoryViewerControllerBase extends ControllerBase {
loader.copyTo(response.outputStream)
()
}
} getOrElse NotFound
} getOrElse NotFound()
}
})
@@ -270,7 +270,7 @@ trait RepositoryViewerControllerBase extends ControllerBase {
response.setContentLength(loader.getSize.toInt)
loader.copyTo(response.outputStream)
()
} getOrElse NotFound
} getOrElse NotFound()
} else {
html.blob(id, repository, path.split("/").toList,
JGitUtil.getContentInfo(git, path, objectId),
@@ -278,7 +278,7 @@ trait RepositoryViewerControllerBase extends ControllerBase {
hasWritePermission(repository.owner, repository.name, context.loginAccount),
request.paths(2) == "blame")
}
} getOrElse NotFound
} getOrElse NotFound()
}
})
@@ -334,7 +334,7 @@ trait RepositoryViewerControllerBase extends ControllerBase {
}
}
} catch {
case e:MissingObjectException => NotFound
case e:MissingObjectException => NotFound()
}
})
@@ -397,8 +397,8 @@ trait RepositoryViewerControllerBase extends ControllerBase {
)
))
}
} else Unauthorized
} getOrElse NotFound
} else Unauthorized()
} getOrElse NotFound()
})
ajaxPost("/:owner/:repository/commit_comments/edit/:id", commentForm)(readableUsersOnly { (form, repository) =>
@@ -407,8 +407,8 @@ trait RepositoryViewerControllerBase extends ControllerBase {
if(isEditable(owner, name, comment.commentedUserName)){
updateCommitComment(comment.commentId, form.content)
redirect(s"/${owner}/${name}/commit_comments/_data/${comment.commentId}")
} else Unauthorized
} getOrElse NotFound
} else Unauthorized()
} getOrElse NotFound()
}
})
@@ -417,8 +417,8 @@ trait RepositoryViewerControllerBase extends ControllerBase {
getCommitComment(owner, name, params("id")).map { comment =>
if(isEditable(owner, name, comment.commentedUserName)){
Ok(deleteCommitComment(comment.commentId))
} else Unauthorized
} getOrElse NotFound
} else Unauthorized()
} getOrElse NotFound()
}
})
@@ -489,7 +489,7 @@ trait RepositoryViewerControllerBase extends ControllerBase {
archiveRepository(name, ".zip", repository)
case name if name.endsWith(".tar.gz") =>
archiveRepository(name, ".tar.gz", repository)
case _ => BadRequest
case _ => BadRequest()
}
})
@@ -518,7 +518,7 @@ trait RepositoryViewerControllerBase extends ControllerBase {
val ref = multiParams("splat").head
JGitUtil.getTreeId(git, ref).map{ treeId =>
html.find(ref, treeId, repository)
} getOrElse NotFound
} getOrElse NotFound()
}
})
@@ -573,7 +573,7 @@ trait RepositoryViewerControllerBase extends ControllerBase {
getPullRequestFromBranch(repository.owner, repository.name, revstr, repository.repository.defaultBranch),
flash.get("info"), flash.get("error"))
}
} getOrElse NotFound
} getOrElse NotFound()
}
}
}

View File

@@ -233,7 +233,7 @@ trait SystemSettingsControllerBase extends AccountManagementControllerBase {
updateImage(userName, form.fileId, form.clearImage)
redirect("/admin/users")
}
} getOrElse NotFound
} getOrElse NotFound()
})
get("/admin/users/_newgroup")(adminOnly {
@@ -291,7 +291,7 @@ trait SystemSettingsControllerBase extends AccountManagementControllerBase {
updateImage(form.groupName, form.fileId, form.clearImage)
redirect("/admin/users")
} getOrElse NotFound
} getOrElse NotFound()
}
})

View File

@@ -201,7 +201,7 @@ trait WikiControllerBase extends ControllerBase {
getFileContent(repository.owner, repository.name, path).map { bytes =>
RawData(FileUtil.getContentType(path, bytes), bytes)
} getOrElse NotFound
} getOrElse NotFound()
})
private def unique: Constraint = new Constraint(){