Commit 915dac00 authored by Alex Denisov's avatar Alex Denisov

Error throwing moved to api_helper

parent a839cb42
...@@ -8,7 +8,7 @@ module Gitlab ...@@ -8,7 +8,7 @@ module Gitlab
if @project ||= current_user.projects.find_by_id(params[:id]) || if @project ||= current_user.projects.find_by_id(params[:id]) ||
current_user.projects.find_by_code(params[:id]) current_user.projects.find_by_code(params[:id])
else else
error!({'message' => '404 Not found'}, 404) not_found!
end end
@project @project
...@@ -19,13 +19,34 @@ module Gitlab ...@@ -19,13 +19,34 @@ module Gitlab
end end
def authenticate! def authenticate!
error!({'message' => '401 Unauthorized'}, 401) unless current_user unauthorized! unless current_user
end end
def authorize! action, subject def authorize! action, subject
unless abilities.allowed?(current_user, action, subject) unless abilities.allowed?(current_user, action, subject)
forbidden!
end
end
# error helpers
def forbidden!
error!({'message' => '403 Forbidden'}, 403) error!({'message' => '403 Forbidden'}, 403)
end 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 end
private private
......
...@@ -60,7 +60,7 @@ module Gitlab ...@@ -60,7 +60,7 @@ module Gitlab
if @issue.save if @issue.save
present @issue, with: Entities::Issue present @issue, with: Entities::Issue
else else
error!({'message' => '404 Not found'}, 404) not_found!
end end
end end
...@@ -93,7 +93,7 @@ module Gitlab ...@@ -93,7 +93,7 @@ module Gitlab
if @issue.update_attributes(parameters) if @issue.update_attributes(parameters)
present @issue, with: Entities::Issue present @issue, with: Entities::Issue
else else
error!({'message' => '404 Not found'}, 404) not_found!
end end
end end
...@@ -105,7 +105,7 @@ module Gitlab ...@@ -105,7 +105,7 @@ module Gitlab
# Example Request: # Example Request:
# DELETE /projects/:id/issues/:issue_id # DELETE /projects/:id/issues/:issue_id
delete ":id/issues/:issue_id" do delete ":id/issues/:issue_id" do
error!({'message' => 'method not allowed'}, 405) not_allowed!
end end
end end
end end
......
...@@ -45,7 +45,7 @@ module Gitlab ...@@ -45,7 +45,7 @@ module Gitlab
if @milestone.save if @milestone.save
present @milestone, with: Entities::Milestone present @milestone, with: Entities::Milestone
else else
error!({'message' => '404 Not found'}, 404) not_found!
end end
end end
...@@ -74,7 +74,7 @@ module Gitlab ...@@ -74,7 +74,7 @@ module Gitlab
if @milestone.update_attributes(parameters) if @milestone.update_attributes(parameters)
present @milestone, with: Entities::Milestone present @milestone, with: Entities::Milestone
else else
error!({'message' => '404 Not found'}, 404) not_found!
end end
end end
end end
......
...@@ -50,7 +50,7 @@ module Gitlab ...@@ -50,7 +50,7 @@ module Gitlab
if @project.saved? if @project.saved?
present @project, with: Entities::Project present @project, with: Entities::Project
else else
error!({'message' => '404 Not found'}, 404) not_found!
end end
end end
...@@ -172,7 +172,7 @@ module Gitlab ...@@ -172,7 +172,7 @@ module Gitlab
if @snippet.save if @snippet.save
present @snippet, with: Entities::ProjectSnippet present @snippet, with: Entities::ProjectSnippet
else else
error!({'message' => '404 Not found'}, 404) not_found!
end end
end end
...@@ -201,7 +201,7 @@ module Gitlab ...@@ -201,7 +201,7 @@ module Gitlab
if @snippet.update_attributes(parameters) if @snippet.update_attributes(parameters)
present @snippet, with: Entities::ProjectSnippet present @snippet, with: Entities::ProjectSnippet
else else
error!({'message' => '404 Not found'}, 404) not_found!
end end
end end
...@@ -244,10 +244,10 @@ module Gitlab ...@@ -244,10 +244,10 @@ module Gitlab
ref = params[:sha] ref = params[:sha]
commit = user_project.commit ref 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] 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? if tree.text?
encoding = Gitlab::Encode.detect_encoding(tree.data) encoding = Gitlab::Encode.detect_encoding(tree.data)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment