使用した技術:
http://www.django-rest-framework.org
例外: http://www.django-rest-framework.org/api-guide/exceptions/
カスタム exception.py ファイルに rest_framework デフォルトの例を含めました:
from rest_framework.views import exception_handler
import sys
def custom_exception_handler(exc, context=None):
# Call REST framework's default exception handler first,
# to get the standard error response.
response = exception_handler(exc)
# Now add the HTTP status code to the response and rename detail to error
if response is not None:
response.data['status_code'] = response.status_code
response.data['request'] = request
response.data['error'] = response.data.get('detail')
del response.data['detail']
return response
これにより、「Http404」などの基本的なエラー情報が送信されますが、IP アドレスなどのリクエスト データは送信されません。
リクエストをレスポンスに追加する最良の方法は? 前もって感謝します。
更新(および解決済み):
そのため、最初は DjangoRestFramework 2.4.x を使用してこれを解決しようとしていましたが、そのバージョンにはカスタム例外ハンドラーの要求またはコンテキスト データ オプションがありません。3.1.3 にアップグレードすると、データを応答に簡単に追加できるようになりました。新しいコードは次のようになります (バージョン 3.1.3 を使用):
def custom_exception_handler(exc, request):
# Call REST framework's default exception handler first,
# to get the standard error response.
response = exception_handler(exc, request)
# Send error to rollbar
rollbar.report_exc_info(sys.exc_info(), request)
# Now add the HTTP status code to the response and rename detail to error
if response is not None:
response.data['status_code'] = response.status_code
response.data['error'] = response.data.get('detail')
del response.data['detail']
return response