Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
core
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to JiHu GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Chenhao Ma
core
Commits
2f117a30
Commit
2f117a30
authored
9 years ago
by
Renzo Frigato
Browse files
Options
Downloads
Patches
Plain Diff
return JSON responses for errors
closes #59
parent
e3704b06
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
api/api.py
+7
-5
7 additions, 5 deletions
api/api.py
api/base.py
+14
-0
14 additions, 0 deletions
api/base.py
api/util.py
+10
-0
10 additions, 0 deletions
api/util.py
with
31 additions
and
5 deletions
api/api.py
+
7
−
5
View file @
2f117a30
...
...
@@ -134,11 +134,13 @@ routes = [
def
dispatcher
(
router
,
request
,
response
):
rv
=
router
.
default_dispatcher
(
request
,
response
)
if
rv
is
not
None
:
response
.
write
(
json
.
dumps
(
rv
,
default
=
util
.
custom_json_serializer
))
response
.
headers
[
'
Content-Type
'
]
=
'
application/json; charset=utf-8
'
try
:
rv
=
router
.
default_dispatcher
(
request
,
response
)
if
rv
is
not
None
:
response
.
write
(
json
.
dumps
(
rv
,
default
=
util
.
custom_json_serializer
))
response
.
headers
[
'
Content-Type
'
]
=
'
application/json; charset=utf-8
'
except
(
webapp2
.
exc
.
HTTPNotFound
,
webapp2
.
exc
.
HTTPMethodNotAllowed
)
as
e
:
util
.
send_json_http_exception
(
response
,
str
(
e
),
e
.
code
)
def
app_factory
(
*
_
,
**
__
):
# don't use config.get_item() as we don't want to require the database at startup
...
...
This diff is collapsed.
Click to expand it.
api/base.py
+
14
−
0
View file @
2f117a30
...
...
@@ -7,6 +7,7 @@ import requests
import
urlparse
import
jsonschema
from
.
import
util
from
.
import
config
log
=
config
.
log
...
...
@@ -119,6 +120,18 @@ class RequestHandler(webapp2.RequestHandler):
def
get_param
(
self
,
param
,
default
=
None
):
return
self
.
request
.
GET
.
get
(
param
,
default
)
def
handle_exception
(
self
,
exception
,
debug
):
# Log the error.
log
.
error
(
exception
)
# If the exception is a HTTPException, use its error code.
# Otherwise use a generic 500 error code.
if
isinstance
(
exception
,
webapp2
.
HTTPException
):
code
=
exception
.
code
else
:
code
=
500
util
.
send_json_http_exception
(
self
.
response
,
str
(
exception
),
code
)
def
dispatch
(
self
):
"""
dispatching and request forwarding
"""
site_id
=
config
.
get_item
(
'
site
'
,
'
id
'
)
...
...
@@ -183,3 +196,4 @@ class RequestHandler(webapp2.RequestHandler):
json_schema
=
copy
.
deepcopy
(
self
.
json_schema
)
json_schema
[
'
properties
'
].
update
(
updates
)
return
json_schema
This diff is collapsed.
Click to expand it.
api/util.py
+
10
−
0
View file @
2f117a30
import
os
import
json
import
pytz
import
uuid
import
datetime
...
...
@@ -108,6 +109,15 @@ def custom_json_serializer(obj):
return
pytz
.
timezone
(
'
UTC
'
).
localize
(
obj
).
isoformat
()
raise
TypeError
(
repr
(
obj
)
+
"
is not JSON serializable
"
)
def
send_json_http_exception
(
response
,
message
,
code
):
response
.
set_status
(
code
)
content
=
json
.
dumps
({
'
message
'
:
message
,
'
status_code
'
:
code
})
response
.
headers
[
'
Content-Type
'
]
=
'
application/json; charset=utf-8
'
response
.
write
(
content
)
class
Enum
(
baseEnum
.
Enum
):
# Enum strings are prefixed by their class: "Category.classifier".
# This overrides that behaviour and removes the prefix.
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment