M
M
Maxim Tarabrin2017-07-13 10:14:50
Django
Maxim Tarabrin, 2017-07-13 10:14:50

How to write custom handler for Django REST Framework?

How to actually write your own custom handler? I need to write an api, I first encountered this because I am writing a SPA. The default exception handler is not suitable, it writes in English there, but I need it specifically for my application. I read off the documentation ( click ), but I did not understand anything. Please explain in Russian knowledgeable people.
There is some code:

def custom_exception_handler(exc, context):
    response = exception_handler(exc, context)
    elif response is not None:
        response.data['status_code'] = response.status_code
        response.data['error'] = response.data['detail']
        del response.data['detail']
    return response

But it is necessary to somehow understand what happened and to russify errors for different cases. How it's done? And what information can I get from exc and context? What are these variables responsible for?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artyom Innokentiev, 2017-07-13
@artinnok

for example:

if isinstance(exc, PermissionDenied):
    # some handling

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question