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
e8c07665
Commit
e8c07665
authored
Dec 13, 2014
by
José Padilla
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Support handlers with and without context
parent
0d109c90
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
28 additions
and
7 deletions
+28
-7
rest_framework/views.py
+7
-1
tests/test_views.py
+21
-6
No files found.
rest_framework/views.py
View file @
e8c07665
...
@@ -2,6 +2,7 @@
...
@@ -2,6 +2,7 @@
Provides an APIView class that is the base of all views in REST framework.
Provides an APIView class that is the base of all views in REST framework.
"""
"""
from
__future__
import
unicode_literals
from
__future__
import
unicode_literals
import
inspect
from
django.core.exceptions
import
PermissionDenied
from
django.core.exceptions
import
PermissionDenied
from
django.http
import
Http404
from
django.http
import
Http404
...
@@ -369,8 +370,13 @@ class APIView(View):
...
@@ -369,8 +370,13 @@ class APIView(View):
else
:
else
:
exc
.
status_code
=
status
.
HTTP_403_FORBIDDEN
exc
.
status_code
=
status
.
HTTP_403_FORBIDDEN
exception_handler
=
self
.
settings
.
EXCEPTION_HANDLER
if
'context'
in
inspect
.
getargspec
(
exception_handler
)
.
args
:
context
=
self
.
get_renderer_context
()
context
=
self
.
get_renderer_context
()
response
=
self
.
settings
.
EXCEPTION_HANDLER
(
exc
,
context
)
response
=
exception_handler
(
exc
,
context
)
else
:
response
=
exception_handler
(
exc
)
if
response
is
None
:
if
response
is
None
:
raise
raise
...
...
tests/test_views.py
View file @
e8c07665
...
@@ -6,6 +6,7 @@ from django.test import TestCase
...
@@ -6,6 +6,7 @@ from django.test import TestCase
from
rest_framework
import
status
from
rest_framework
import
status
from
rest_framework.decorators
import
api_view
from
rest_framework.decorators
import
api_view
from
rest_framework.response
import
Response
from
rest_framework.response
import
Response
from
rest_framework.request
import
Request
from
rest_framework.settings
import
api_settings
from
rest_framework.settings
import
api_settings
from
rest_framework.test
import
APIRequestFactory
from
rest_framework.test
import
APIRequestFactory
from
rest_framework.views
import
APIView
from
rest_framework.views
import
APIView
...
@@ -121,12 +122,7 @@ class TestCustomExceptionHandler(TestCase):
...
@@ -121,12 +122,7 @@ class TestCustomExceptionHandler(TestCase):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
DEFAULT_HANDLER
=
api_settings
.
EXCEPTION_HANDLER
self
.
DEFAULT_HANDLER
=
api_settings
.
EXCEPTION_HANDLER
def
exception_handler
(
exc
,
context
=
None
):
def
exception_handler
(
exc
):
self
.
assertTrue
(
'args'
in
context
)
self
.
assertTrue
(
'kwargs'
in
context
)
self
.
assertTrue
(
'request'
in
context
)
self
.
assertTrue
(
'view'
in
context
)
return
Response
(
'Error!'
,
status
=
status
.
HTTP_400_BAD_REQUEST
)
return
Response
(
'Error!'
,
status
=
status
.
HTTP_400_BAD_REQUEST
)
api_settings
.
EXCEPTION_HANDLER
=
exception_handler
api_settings
.
EXCEPTION_HANDLER
=
exception_handler
...
@@ -151,3 +147,22 @@ class TestCustomExceptionHandler(TestCase):
...
@@ -151,3 +147,22 @@ class TestCustomExceptionHandler(TestCase):
expected
=
'Error!'
expected
=
'Error!'
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_400_BAD_REQUEST
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_400_BAD_REQUEST
)
self
.
assertEqual
(
response
.
data
,
expected
)
self
.
assertEqual
(
response
.
data
,
expected
)
def
test_context_exception_handler
(
self
):
def
exception_handler
(
exc
,
context
=
None
):
self
.
assertEqual
(
context
[
'args'
],
())
self
.
assertEqual
(
context
[
'kwargs'
],
{})
self
.
assertTrue
(
isinstance
(
context
[
'request'
],
Request
))
self
.
assertTrue
(
isinstance
(
context
[
'view'
],
ErrorView
))
return
Response
(
'Error!'
,
status
=
status
.
HTTP_400_BAD_REQUEST
)
api_settings
.
EXCEPTION_HANDLER
=
exception_handler
view
=
ErrorView
.
as_view
()
request
=
factory
.
get
(
'/'
,
content_type
=
'application/json'
)
response
=
view
(
request
)
expected
=
'Error!'
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_400_BAD_REQUEST
)
self
.
assertEqual
(
response
.
data
,
expected
)
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