Commit 915dac0055cd801c080ebcd37749f4fc6d2d12c4

Authored by Alex Denisov
1 parent a839cb42

Error throwing moved to api_helper

lib/api/helpers.rb
... ... @@ -8,7 +8,7 @@ module Gitlab
8 8 if @project ||= current_user.projects.find_by_id(params[:id]) ||
9 9 current_user.projects.find_by_code(params[:id])
10 10 else
11   - error!({'message' => '404 Not found'}, 404)
  11 + not_found!
12 12 end
13 13  
14 14 @project
... ... @@ -19,15 +19,36 @@ module Gitlab
19 19 end
20 20  
21 21 def authenticate!
22   - error!({'message' => '401 Unauthorized'}, 401) unless current_user
  22 + unauthorized! unless current_user
23 23 end
24 24  
25 25 def authorize! action, subject
26 26 unless abilities.allowed?(current_user, action, subject)
27   - error!({'message' => '403 Forbidden'}, 403)
  27 + forbidden!
28 28 end
29 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 52 private
32 53  
33 54 def abilities
... ...
lib/api/issues.rb
... ... @@ -60,7 +60,7 @@ module Gitlab
60 60 if @issue.save
61 61 present @issue, with: Entities::Issue
62 62 else
63   - error!({'message' => '404 Not found'}, 404)
  63 + not_found!
64 64 end
65 65 end
66 66  
... ... @@ -93,7 +93,7 @@ module Gitlab
93 93 if @issue.update_attributes(parameters)
94 94 present @issue, with: Entities::Issue
95 95 else
96   - error!({'message' => '404 Not found'}, 404)
  96 + not_found!
97 97 end
98 98 end
99 99  
... ... @@ -105,7 +105,7 @@ module Gitlab
105 105 # Example Request:
106 106 # DELETE /projects/:id/issues/:issue_id
107 107 delete ":id/issues/:issue_id" do
108   - error!({'message' => 'method not allowed'}, 405)
  108 + not_allowed!
109 109 end
110 110 end
111 111 end
... ...
lib/api/milestones.rb
... ... @@ -45,7 +45,7 @@ module Gitlab
45 45 if @milestone.save
46 46 present @milestone, with: Entities::Milestone
47 47 else
48   - error!({'message' => '404 Not found'}, 404)
  48 + not_found!
49 49 end
50 50 end
51 51  
... ... @@ -74,7 +74,7 @@ module Gitlab
74 74 if @milestone.update_attributes(parameters)
75 75 present @milestone, with: Entities::Milestone
76 76 else
77   - error!({'message' => '404 Not found'}, 404)
  77 + not_found!
78 78 end
79 79 end
80 80 end
... ...
lib/api/projects.rb
... ... @@ -50,7 +50,7 @@ module Gitlab
50 50 if @project.saved?
51 51 present @project, with: Entities::Project
52 52 else
53   - error!({'message' => '404 Not found'}, 404)
  53 + not_found!
54 54 end
55 55 end
56 56  
... ... @@ -172,7 +172,7 @@ module Gitlab
172 172 if @snippet.save
173 173 present @snippet, with: Entities::ProjectSnippet
174 174 else
175   - error!({'message' => '404 Not found'}, 404)
  175 + not_found!
176 176 end
177 177 end
178 178  
... ... @@ -201,7 +201,7 @@ module Gitlab
201 201 if @snippet.update_attributes(parameters)
202 202 present @snippet, with: Entities::ProjectSnippet
203 203 else
204   - error!({'message' => '404 Not found'}, 404)
  204 + not_found!
205 205 end
206 206 end
207 207  
... ... @@ -244,10 +244,10 @@ module Gitlab
244 244 ref = params[:sha]
245 245  
246 246 commit = user_project.commit ref
247   - error!('404 Commit Not Found', 404) unless commit
  247 + not_found! "Commit" unless commit
248 248  
249 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 252 if tree.text?
253 253 encoding = Gitlab::Encode.detect_encoding(tree.data)
... ...