summaryrefslogtreecommitdiff
path: root/lib/api/helpers.rb
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2012-09-10 06:41:01 -0700
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2012-09-10 06:41:01 -0700
commit8674fba173e520a67d60e6b5289dcd1bd648d537 (patch)
treee3ff99a0865caaa182cca2a51342e3f2978cb844 /lib/api/helpers.rb
parent263b8af1c49d54b3475758abda5ac50c0aff42bf (diff)
parente68d0841be6e252baf98a045fc5b4bed431f31b2 (diff)
downloadgitlab-ce-8674fba173e520a67d60e6b5289dcd1bd648d537.tar.gz
Merge pull request #1422 from AlexDenisov/api_errors_refactoring
API errors refactoring
Diffstat (limited to 'lib/api/helpers.rb')
-rw-r--r--lib/api/helpers.rb31
1 files changed, 28 insertions, 3 deletions
diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb
index c0ba874790a..054eb2d3f70 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,40 @@ 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!
+ render_api_error!('403 Forbidden', 403)
+ end
+
+ def not_found!(resource = nil)
+ message = ["404"]
+ message << resource if resource
+ message << "Not Found"
+ render_api_error!(message.join(' '), 404)
+ end
+
+ def unauthorized!
+ render_api_error!('401 Unauthorized', 401)
+ end
+
+ def not_allowed!
+ render_api_error!('Method Not Allowed', 405)
+ end
+
+ def render_api_error!(message, status)
+ error!({'message' => message}, status)
+ end
+
private
def abilities