Commit 915dac0055cd801c080ebcd37749f4fc6d2d12c4
1 parent
a839cb42
Exists in
master
and in
4 other branches
Error throwing moved to api_helper
Showing
4 changed files
with
34 additions
and
13 deletions
Show diff stats
lib/api/helpers.rb
| @@ -8,7 +8,7 @@ module Gitlab | @@ -8,7 +8,7 @@ module Gitlab | ||
| 8 | if @project ||= current_user.projects.find_by_id(params[:id]) || | 8 | if @project ||= current_user.projects.find_by_id(params[:id]) || |
| 9 | current_user.projects.find_by_code(params[:id]) | 9 | current_user.projects.find_by_code(params[:id]) |
| 10 | else | 10 | else |
| 11 | - error!({'message' => '404 Not found'}, 404) | 11 | + not_found! |
| 12 | end | 12 | end |
| 13 | 13 | ||
| 14 | @project | 14 | @project |
| @@ -19,15 +19,36 @@ module Gitlab | @@ -19,15 +19,36 @@ module Gitlab | ||
| 19 | end | 19 | end |
| 20 | 20 | ||
| 21 | def authenticate! | 21 | def authenticate! |
| 22 | - error!({'message' => '401 Unauthorized'}, 401) unless current_user | 22 | + unauthorized! unless current_user |
| 23 | end | 23 | end |
| 24 | 24 | ||
| 25 | def authorize! action, subject | 25 | def authorize! action, subject |
| 26 | unless abilities.allowed?(current_user, action, subject) | 26 | unless abilities.allowed?(current_user, action, subject) |
| 27 | - error!({'message' => '403 Forbidden'}, 403) | 27 | + forbidden! |
| 28 | end | 28 | end |
| 29 | end | 29 | end |
| 30 | 30 | ||
| 31 | + # error helpers | ||
| 32 | + | ||
| 33 | + def forbidden! | ||
| 34 | + error!({'message' => '403 Forbidden'}, 403) | ||
| 35 | + end | ||
| 36 | + | ||
| 37 | + def not_found!(resource = nil) | ||
| 38 | + message = ["404"] | ||
| 39 | + message << resource if resource | ||
| 40 | + message << "Not Found" | ||
| 41 | + error!({'message' => message.join(' ')}, 404) | ||
| 42 | + end | ||
| 43 | + | ||
| 44 | + def unauthorized! | ||
| 45 | + error!({'message' => '401 Unauthorized'}, 401) | ||
| 46 | + end | ||
| 47 | + | ||
| 48 | + def not_allowed! | ||
| 49 | + error!({'message' => 'method not allowed'}, 405) | ||
| 50 | + end | ||
| 51 | + | ||
| 31 | private | 52 | private |
| 32 | 53 | ||
| 33 | def abilities | 54 | def abilities |
lib/api/issues.rb
| @@ -60,7 +60,7 @@ module Gitlab | @@ -60,7 +60,7 @@ module Gitlab | ||
| 60 | if @issue.save | 60 | if @issue.save |
| 61 | present @issue, with: Entities::Issue | 61 | present @issue, with: Entities::Issue |
| 62 | else | 62 | else |
| 63 | - error!({'message' => '404 Not found'}, 404) | 63 | + not_found! |
| 64 | end | 64 | end |
| 65 | end | 65 | end |
| 66 | 66 | ||
| @@ -93,7 +93,7 @@ module Gitlab | @@ -93,7 +93,7 @@ module Gitlab | ||
| 93 | if @issue.update_attributes(parameters) | 93 | if @issue.update_attributes(parameters) |
| 94 | present @issue, with: Entities::Issue | 94 | present @issue, with: Entities::Issue |
| 95 | else | 95 | else |
| 96 | - error!({'message' => '404 Not found'}, 404) | 96 | + not_found! |
| 97 | end | 97 | end |
| 98 | end | 98 | end |
| 99 | 99 | ||
| @@ -105,7 +105,7 @@ module Gitlab | @@ -105,7 +105,7 @@ module Gitlab | ||
| 105 | # Example Request: | 105 | # Example Request: |
| 106 | # DELETE /projects/:id/issues/:issue_id | 106 | # DELETE /projects/:id/issues/:issue_id |
| 107 | delete ":id/issues/:issue_id" do | 107 | delete ":id/issues/:issue_id" do |
| 108 | - error!({'message' => 'method not allowed'}, 405) | 108 | + not_allowed! |
| 109 | end | 109 | end |
| 110 | end | 110 | end |
| 111 | end | 111 | end |
lib/api/milestones.rb
| @@ -45,7 +45,7 @@ module Gitlab | @@ -45,7 +45,7 @@ module Gitlab | ||
| 45 | if @milestone.save | 45 | if @milestone.save |
| 46 | present @milestone, with: Entities::Milestone | 46 | present @milestone, with: Entities::Milestone |
| 47 | else | 47 | else |
| 48 | - error!({'message' => '404 Not found'}, 404) | 48 | + not_found! |
| 49 | end | 49 | end |
| 50 | end | 50 | end |
| 51 | 51 | ||
| @@ -74,7 +74,7 @@ module Gitlab | @@ -74,7 +74,7 @@ module Gitlab | ||
| 74 | if @milestone.update_attributes(parameters) | 74 | if @milestone.update_attributes(parameters) |
| 75 | present @milestone, with: Entities::Milestone | 75 | present @milestone, with: Entities::Milestone |
| 76 | else | 76 | else |
| 77 | - error!({'message' => '404 Not found'}, 404) | 77 | + not_found! |
| 78 | end | 78 | end |
| 79 | end | 79 | end |
| 80 | end | 80 | end |
lib/api/projects.rb
| @@ -50,7 +50,7 @@ module Gitlab | @@ -50,7 +50,7 @@ module Gitlab | ||
| 50 | if @project.saved? | 50 | if @project.saved? |
| 51 | present @project, with: Entities::Project | 51 | present @project, with: Entities::Project |
| 52 | else | 52 | else |
| 53 | - error!({'message' => '404 Not found'}, 404) | 53 | + not_found! |
| 54 | end | 54 | end |
| 55 | end | 55 | end |
| 56 | 56 | ||
| @@ -172,7 +172,7 @@ module Gitlab | @@ -172,7 +172,7 @@ module Gitlab | ||
| 172 | if @snippet.save | 172 | if @snippet.save |
| 173 | present @snippet, with: Entities::ProjectSnippet | 173 | present @snippet, with: Entities::ProjectSnippet |
| 174 | else | 174 | else |
| 175 | - error!({'message' => '404 Not found'}, 404) | 175 | + not_found! |
| 176 | end | 176 | end |
| 177 | end | 177 | end |
| 178 | 178 | ||
| @@ -201,7 +201,7 @@ module Gitlab | @@ -201,7 +201,7 @@ module Gitlab | ||
| 201 | if @snippet.update_attributes(parameters) | 201 | if @snippet.update_attributes(parameters) |
| 202 | present @snippet, with: Entities::ProjectSnippet | 202 | present @snippet, with: Entities::ProjectSnippet |
| 203 | else | 203 | else |
| 204 | - error!({'message' => '404 Not found'}, 404) | 204 | + not_found! |
| 205 | end | 205 | end |
| 206 | end | 206 | end |
| 207 | 207 | ||
| @@ -244,10 +244,10 @@ module Gitlab | @@ -244,10 +244,10 @@ module Gitlab | ||
| 244 | ref = params[:sha] | 244 | ref = params[:sha] |
| 245 | 245 | ||
| 246 | commit = user_project.commit ref | 246 | commit = user_project.commit ref |
| 247 | - error!('404 Commit Not Found', 404) unless commit | 247 | + not_found! "Commit" unless commit |
| 248 | 248 | ||
| 249 | tree = Tree.new commit.tree, user_project, ref, params[:filepath] | 249 | tree = Tree.new commit.tree, user_project, ref, params[:filepath] |
| 250 | - error!('404 File Not Found', 404) unless tree.try(:tree) | 250 | + not_found! "File" unless tree.try(:tree) |
| 251 | 251 | ||
| 252 | if tree.text? | 252 | if tree.text? |
| 253 | encoding = Gitlab::Encode.detect_encoding(tree.data) | 253 | encoding = Gitlab::Encode.detect_encoding(tree.data) |