django-ninja icon indicating copy to clipboard operation
django-ninja copied to clipboard

[BUG] Custom Error Handler conflicts with Django Admin

Open riseryan89 opened this issue 2 years ago • 1 comments

Describe the bug Please see how to use Custom Error Handling part here

I have 5 files.

urls folder

  • init.py
  • urls.py
  • api_object.py

exception folder

  • basic_error.py

api_object.py

from ninja import NinjaAPI

apis = NinjaAPI()

basic_error.py

from enum import Enum

from project.urls.api_object import apis


class ErrorType(str, Enum):
    SILENCE = "SILENCE"
    NOTIFY = "NOTIFY"
    ALERT = "ALERT"


class ServiceUnavailableException(Exception):
    def __init__(self, msg: str, error_type: ErrorType = ErrorType.SILENCE):
        self.msg = msg
        self.status = 503
        self.error_type = error_type


class BadRequestException(Exception):
    def __init__(self, msg: str, error_type: ErrorType = ErrorType.SILENCE):
        self.msg = msg
        self.status = 400
        self.error_type = error_type


class UnAuthorizedException(Exception):
    def __init__(self, msg: str, error_type: ErrorType = ErrorType.SILENCE):
        self.msg = msg
        self.status = 401
        self.error_type = error_type


class ForbiddenException(Exception):
    def __init__(self, msg: str, error_type: ErrorType = ErrorType.SILENCE):
        self.msg = msg
        self.status = 403
        self.error_type = error_type


class NotFoundException(Exception):
    def __init__(self, msg: str, error_type: ErrorType = ErrorType.SILENCE):
        self.msg = msg
        self.status = 404
        self.error_type = error_type


class ConflictException(Exception):
    def __init__(self, msg: str, error_type: ErrorType = ErrorType.SILENCE):
        self.msg = msg
        self.status = 409
        self.error_type = error_type


class InternalServerError(Exception):
    def __init__(self, msg: str, error_type: ErrorType = ErrorType.SILENCE):
        self.msg = msg
        self.status = 500
        self.error_type = error_type


# initializing handler


@apis.exception_handler(ServiceUnavailableException)
@apis.exception_handler(BadRequestException)
@apis.exception_handler(UnAuthorizedException)
@apis.exception_handler(ForbiddenException)
@apis.exception_handler(NotFoundException)
@apis.exception_handler(ConflictException)
@apis.exception_handler(InternalServerError)
def base_error_exception(request, exc):
    return apis.create_response(
        request,
        {"msg": exc.msg, "errorType": exc.error_type},
        status=exc.status,
    )

when i access /admin, the msg is shown like image

if I comment out basic_error.py, works fine.

Any Suggestions?

Versions (please complete the following information):

  • Python version: [e.g. 3.6] 3.10
  • Django version: [e.g. 4.0] 4.0.3
  • Django-Ninja version: [e.g. 0.16.2] 0.17

riseryan89 avatar Mar 19 '22 15:03 riseryan89

@riseryan89

how do you import basic_error ? maybe you have some circular import...

vitalik avatar Jul 01 '22 14:07 vitalik