Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
gitlab-ce
Commits
8674fba1
Commit
8674fba1
authored
Sep 10, 2012
by
Dmitriy Zaporozhets
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1422 from AlexDenisov/api_errors_refactoring
API errors refactoring
parents
263b8af1
e68d0841
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
39 additions
and
14 deletions
+39
-14
lib/api/helpers.rb
lib/api/helpers.rb
+28
-3
lib/api/issues.rb
lib/api/issues.rb
+3
-3
lib/api/milestones.rb
lib/api/milestones.rb
+2
-2
lib/api/projects.rb
lib/api/projects.rb
+5
-5
spec/requests/api/projects_spec.rb
spec/requests/api/projects_spec.rb
+1
-1
No files found.
lib/api/helpers.rb
View file @
8674fba1
...
@@ -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,15 +19,40 @@ module Gitlab
...
@@ -19,15 +19,40 @@ 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
)
error!
({
'message'
=>
'403 Forbidden'
},
403
)
forbidden!
end
end
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
private
def
abilities
def
abilities
...
...
lib/api/issues.rb
View file @
8674fba1
...
@@ -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
...
...
lib/api/milestones.rb
View file @
8674fba1
...
@@ -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
...
...
lib/api/projects.rb
View file @
8674fba1
...
@@ -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
)
...
...
spec/requests/api/projects_spec.rb
View file @
8674fba1
...
@@ -85,7 +85,7 @@ describe Gitlab::API do
...
@@ -85,7 +85,7 @@ describe Gitlab::API do
it
"should return a 404 error if not found"
do
it
"should return a 404 error if not found"
do
get
api
(
"/projects/42"
,
user
)
get
api
(
"/projects/42"
,
user
)
response
.
status
.
should
==
404
response
.
status
.
should
==
404
json_response
[
'message'
].
should
==
'404 Not
f
ound'
json_response
[
'message'
].
should
==
'404 Not
F
ound'
end
end
end
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment