summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAlex Denisov <1101.debian@gmail.com>2012-09-10 10:41:46 +0300
committerAlex Denisov <1101.debian@gmail.com>2012-09-10 10:41:46 +0300
commit915dac0055cd801c080ebcd37749f4fc6d2d12c4 (patch)
tree1de457b9c3d95d98051432d4686f89f981429ccb /lib
parenta839cb427cc158330297fd89fbf40321d41349a4 (diff)
downloadgitlab-ce-915dac0055cd801c080ebcd37749f4fc6d2d12c4.tar.gz
Error throwing moved to api_helper
Diffstat (limited to 'lib')
-rw-r--r--lib/api/helpers.rb27
-rw-r--r--lib/api/issues.rb6
-rw-r--r--lib/api/milestones.rb4
-rw-r--r--lib/api/projects.rb10
4 files changed, 34 insertions, 13 deletions
diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb
index c0ba874790a..3a385f1582a 100644
--- a/lib/api/helpers.rb
+++ b/lib/api/helpers.rb
@@ -8,7 +8,7 @@ module Gitlab
if @project ||= current_user.projects.find_by_id(params[:id]) ||
current_user.projects.find_by_code(params[:id])
else
- error!({'message' => '404 Not found'}, 404)
+ not_found!
end
@project
@@ -19,15 +19,36 @@ module Gitlab
end
def authenticate!
- error!({'message' => '401 Unauthorized'}, 401) unless current_user
+ unauthorized! unless current_user
end
def authorize! action, subject
unless abilities.allowed?(current_user, action, subject)
- error!({'message' => '403 Forbidden'}, 403)
+ forbidden!
end
end
+ # error helpers
+
+ def forbidden!
+ error!({'message' => '403 Forbidden'}, 403)
+ end
+
+ def not_found!(resource = nil)
+ message = ["404"]
+ message << resource if resource
+ message << "Not Found"
+ error!({'message' => message.join(' ')}, 404)
+ end
+
+ def unauthorized!
+ error!({'message' => '401 Unauthorized'}, 401)
+ end
+
+ def not_allowed!
+ error!({'message' => 'method not allowed'}, 405)
+ end
+
private
def abilities
diff --git a/lib/api/issues.rb b/lib/api/issues.rb
index 4cfa7500e33..659f065e390 100644
--- a/lib/api/issues.rb
+++ b/lib/api/issues.rb
@@ -60,7 +60,7 @@ module Gitlab
if @issue.save
present @issue, with: Entities::Issue
else
- error!({'message' => '404 Not found'}, 404)
+ not_found!
end
end
@@ -93,7 +93,7 @@ module Gitlab
if @issue.update_attributes(parameters)
present @issue, with: Entities::Issue
else
- error!({'message' => '404 Not found'}, 404)
+ not_found!
end
end
@@ -105,7 +105,7 @@ module Gitlab
# Example Request:
# DELETE /projects/:id/issues/:issue_id
delete ":id/issues/:issue_id" do
- error!({'message' => 'method not allowed'}, 405)
+ not_allowed!
end
end
end
diff --git a/lib/api/milestones.rb b/lib/api/milestones.rb
index 7c68466760f..4b0424ba444 100644
--- a/lib/api/milestones.rb
+++ b/lib/api/milestones.rb
@@ -45,7 +45,7 @@ module Gitlab
if @milestone.save
present @milestone, with: Entities::Milestone
else
- error!({'message' => '404 Not found'}, 404)
+ not_found!
end
end
@@ -74,7 +74,7 @@ module Gitlab
if @milestone.update_attributes(parameters)
present @milestone, with: Entities::Milestone
else
- error!({'message' => '404 Not found'}, 404)
+ not_found!
end
end
end
diff --git a/lib/api/projects.rb b/lib/api/projects.rb
index 05b07e8def4..9d33323e5fb 100644
--- a/lib/api/projects.rb
+++ b/lib/api/projects.rb
@@ -50,7 +50,7 @@ module Gitlab
if @project.saved?
present @project, with: Entities::Project
else
- error!({'message' => '404 Not found'}, 404)
+ not_found!
end
end
@@ -172,7 +172,7 @@ module Gitlab
if @snippet.save
present @snippet, with: Entities::ProjectSnippet
else
- error!({'message' => '404 Not found'}, 404)
+ not_found!
end
end
@@ -201,7 +201,7 @@ module Gitlab
if @snippet.update_attributes(parameters)
present @snippet, with: Entities::ProjectSnippet
else
- error!({'message' => '404 Not found'}, 404)
+ not_found!
end
end
@@ -244,10 +244,10 @@ module Gitlab
ref = params[:sha]
commit = user_project.commit ref
- error!('404 Commit Not Found', 404) unless commit
+ not_found! "Commit" unless commit
tree = Tree.new commit.tree, user_project, ref, params[:filepath]
- error!('404 File Not Found', 404) unless tree.try(:tree)
+ not_found! "File" unless tree.try(:tree)
if tree.text?
encoding = Gitlab::Encode.detect_encoding(tree.data)