Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
95df4898
Commit
95df4898
authored
Apr 07, 2017
by
Tom Christie
Committed by
GitHub
Apr 07, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #5055 from sigmavirus24/bug/5054
Use overridden settings exception handler
parents
5e6b2339
c2ee1b30
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
2 deletions
+26
-2
rest_framework/views.py
+1
-1
tests/test_views.py
+25
-1
No files found.
rest_framework/views.py
View file @
95df4898
...
...
@@ -290,7 +290,7 @@ class APIView(View):
"""
Returns the exception handler that this view uses.
"""
return
api_
settings
.
EXCEPTION_HANDLER
return
self
.
settings
.
EXCEPTION_HANDLER
# API policy implementation methods
...
...
tests/test_views.py
View file @
95df4898
...
...
@@ -8,7 +8,7 @@ from django.test import TestCase
from
rest_framework
import
status
from
rest_framework.decorators
import
api_view
from
rest_framework.response
import
Response
from
rest_framework.settings
import
api_settings
from
rest_framework.settings
import
APISettings
,
api_settings
from
rest_framework.test
import
APIRequestFactory
from
rest_framework.views
import
APIView
...
...
@@ -45,6 +45,19 @@ class ErrorView(APIView):
raise
Exception
def
custom_handler
(
exc
,
context
):
if
isinstance
(
exc
,
SyntaxError
):
return
Response
({
'error'
:
'SyntaxError'
},
status
=
400
)
return
Response
({
'error'
:
'UnknownError'
},
status
=
500
)
class
OverridenSettingsView
(
APIView
):
settings
=
APISettings
({
'EXCEPTION_HANDLER'
:
custom_handler
})
def
get
(
self
,
request
,
*
args
,
**
kwargs
):
raise
SyntaxError
(
'request is invalid syntax'
)
@api_view
([
'GET'
])
def
error_view
(
request
):
raise
Exception
...
...
@@ -118,3 +131,14 @@ class TestCustomExceptionHandler(TestCase):
expected
=
'Error!'
assert
response
.
status_code
==
status
.
HTTP_400_BAD_REQUEST
assert
response
.
data
==
expected
class
TestCustomSettings
(
TestCase
):
def
setUp
(
self
):
self
.
view
=
OverridenSettingsView
.
as_view
()
def
test_get_exception_handler
(
self
):
request
=
factory
.
get
(
'/'
,
content_type
=
'application/json'
)
response
=
self
.
view
(
request
)
assert
response
.
status_code
==
400
assert
response
.
data
==
{
'error'
:
'SyntaxError'
}
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