Skip to content

Commit

Permalink
Serializa JSON no caso de erro 500 na API
Browse files Browse the repository at this point in the history
  • Loading branch information
berinhard committed Nov 9, 2020
1 parent 25a192d commit 893f405
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions brasilio/api_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@

handler403 = "traffic_control.handlers.handler_403"
handler404 = "traffic_control.handlers.handler_404"
handler500 = "traffic_control.handlers.handler_500"
1 change: 1 addition & 0 deletions brasilio/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@

handler403 = "traffic_control.handlers.handler_403"
handler404 = "traffic_control.handlers.handler_404"
handler500 = "traffic_control.handlers.handler_500"
9 changes: 8 additions & 1 deletion traffic_control/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.http import JsonResponse
from django.shortcuts import render
from django.urls import reverse
from django.views.defaults import page_not_found
from django.views.defaults import page_not_found, server_error
from ratelimit.exceptions import Ratelimited
from rest_framework.exceptions import Throttled
from rest_framework.views import exception_handler
Expand Down Expand Up @@ -46,6 +46,13 @@ def handler_404(request, exception):
return page_not_found(request, exception)


def handler_500(request, *args, **kwargs):
if request.get_host() == settings.BRASILIO_API_HOST:
data = {"message": "Ocorreu algum erro em nossos servidores."}
return JsonResponse(data=data, status=500)
return server_error(request, *args, **kwargs)


def api_exception_handler(exc, context):
response = exception_handler(exc, context)
status_code = getattr(response, "status_code", None)
Expand Down

0 comments on commit 893f405

Please sign in to comment.