You've already forked Promocode-API
mirror of
https://github.com/devitq/Promocode-API.git
synced 2026-05-22 20:57:11 +00:00
107 lines
2.4 KiB
Python
107 lines
2.4 KiB
Python
import logging
|
|
from http import HTTPStatus as status
|
|
|
|
import django.core.exceptions
|
|
import django.http
|
|
import ninja.errors
|
|
from django.http import HttpRequest, HttpResponse
|
|
from ninja import NinjaAPI
|
|
|
|
from config.errors import ConflictError
|
|
|
|
logger = logging.getLogger("django")
|
|
|
|
|
|
def handle_conflict_error(
|
|
request: HttpRequest,
|
|
exc: ConflictError,
|
|
router: NinjaAPI,
|
|
) -> HttpResponse:
|
|
detail = list(exc.validation_error)
|
|
|
|
if hasattr(exc, "error_dict"):
|
|
detail = dict(exc.validation_error)
|
|
|
|
return router.create_response(
|
|
request,
|
|
{"detail": detail},
|
|
status=status.CONFLICT,
|
|
)
|
|
|
|
|
|
def handle_django_validation_error(
|
|
request: HttpRequest,
|
|
exc: django.core.exceptions.ValidationError,
|
|
router: NinjaAPI,
|
|
) -> HttpResponse:
|
|
detail = list(exc)
|
|
|
|
if hasattr(exc, "error_dict"):
|
|
detail = dict(exc)
|
|
|
|
return router.create_response(
|
|
request,
|
|
{"detail": detail},
|
|
status=status.BAD_REQUEST,
|
|
)
|
|
|
|
|
|
def handle_authentication_error(
|
|
request: HttpRequest,
|
|
exc: ninja.errors.AuthenticationError,
|
|
router: NinjaAPI,
|
|
) -> HttpResponse:
|
|
return router.create_response(
|
|
request,
|
|
{"detail": status.UNAUTHORIZED.phrase},
|
|
status=status.UNAUTHORIZED,
|
|
)
|
|
|
|
|
|
def handle_validation_error(
|
|
request: HttpRequest,
|
|
exc: ninja.errors.ValidationError,
|
|
router: NinjaAPI,
|
|
) -> HttpResponse:
|
|
return router.create_response(
|
|
request,
|
|
{"detail": exc.errors},
|
|
status=status.BAD_REQUEST,
|
|
)
|
|
|
|
|
|
def handle_not_found_error(
|
|
request: HttpRequest,
|
|
exc: Exception,
|
|
router: NinjaAPI,
|
|
) -> HttpResponse:
|
|
return router.create_response(
|
|
request,
|
|
{"detail": status.NOT_FOUND.phrase},
|
|
status=status.NOT_FOUND,
|
|
)
|
|
|
|
|
|
def handle_unknown_exception(
|
|
request: HttpRequest,
|
|
exc: Exception,
|
|
router: NinjaAPI,
|
|
) -> HttpResponse:
|
|
logger.exception(exc)
|
|
|
|
return router.create_response(
|
|
request,
|
|
{"detail": status.INTERNAL_SERVER_ERROR.phrase},
|
|
status=status.INTERNAL_SERVER_ERROR,
|
|
)
|
|
|
|
|
|
exception_handlers = [
|
|
(ConflictError, handle_conflict_error),
|
|
(django.core.exceptions.ValidationError, handle_django_validation_error),
|
|
(ninja.errors.AuthenticationError, handle_authentication_error),
|
|
(ninja.errors.ValidationError, handle_validation_error),
|
|
(django.http.Http404, handle_not_found_error),
|
|
(Exception, handle_unknown_exception),
|
|
]
|