File tree Expand file tree Collapse file tree 4 files changed +53
-0
lines changed Expand file tree Collapse file tree 4 files changed +53
-0
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,12 @@ class IdentitySerializer(serializers.ModelSerializer):
66 """
77 Identity Serializer
88 """
9+ def validate_first_name (self , data ):
10+ if len (data ) > 10 :
11+ raise serializers .ValidationError (
12+ 'There\' s a problem with first name' )
13+ return data
14+
915 class Meta :
1016 model = auth_models .User
1117 fields = (
Original file line number Diff line number Diff line change 3838 'PAGINATE_BY' : 1 ,
3939 'PAGINATE_BY_PARAM' : 'page_size' ,
4040 'MAX_PAGINATE_BY' : 100 ,
41+ 'EXCEPTION_HANDLER' : 'rest_framework_ember.exceptions.exception_handler' ,
4142 # DRF v3.1+
4243 'DEFAULT_PAGINATION_CLASS' :
4344 'rest_framework_json_api.pagination.PageNumberPagination' ,
Original file line number Diff line number Diff line change @@ -48,4 +48,30 @@ def test_ember_expected_renderer(self):
4848 }
4949 )
5050
51+ def test_custom_exceptions (self ):
52+ """
53+ Exceptions should conform to json api spec
54+ """
55+ response = self .client .post ('/identities' , {
56+ 'email' : 'bar' , 'first_name' : 'alajflajaljalajlfjafljalj' })
57+ self .assertEqual (
58+ json .loads (response .content .decode ('utf8' )),
59+ {
60+ 'errors' : [
61+ {
62+ 'source' : {
63+ 'parameter' : 'email'
64+ },
65+ 'detail' : 'Enter a valid email address.'
66+ },
67+ {
68+ 'source' : {
69+ 'parameter' : 'first_name'
70+ },
71+ 'detail' : 'There\' s a problem with first name'
72+ },
73+ ]
74+ }
75+ )
76+
5177
Original file line number Diff line number Diff line change 1+ from rest_framework .views import exception_handler as drf_exception_handler
2+
3+
4+ def exception_handler (exc , context ):
5+ response = drf_exception_handler (exc , context )
6+
7+ errors = []
8+ for field , error in response .data .items ():
9+ for message in error :
10+ errors .append ({
11+ 'detail' : message ,
12+ 'source' : {
13+ 'parameter' : field ,
14+ },
15+ })
16+ context ['view' ].resource_name = 'errors'
17+ response .data = errors
18+ return response
19+
20+
You can’t perform that action at this time.
0 commit comments