Code Monkey home page Code Monkey logo

Comments (1)

danyadanch avatar danyadanch commented on September 23, 2024

sup! i have very strange workaround for this, but got it working

# apps.common.api.errors
from django.core.exceptions import ValidationError
from ninja import NinjaAPI


def get_validation_error_handler(api: NinjaAPI):
    def handle(request, exc: ValidationError):
        error = "Validation Error"

        error_data = {"message": error, "errors": exc.error_list}

        return api.create_response(request, error_data, status=400)

    return (handle, ValidationError)


def get_exception_handler(api: NinjaAPI):
    def handle(request, exc: Exception):
        error_data = {"message": str(exc)}
        return api.create_response(request, error_data, status=500)

    return (handle, Exception)


EXCEPTION_HANDLERS = [get_validation_error_handler, get_exception_handler]


def add_exception_handlers(api: NinjaAPI):
    for handler in EXCEPTION_HANDLERS:
        handler_func, exc = handler(api)
        api.add_exception_handler(handler=handler_func, exc_class=exc)
# apps.common.test_client
'''
   This is mixin file for testing
''' 
from django.conf import settings
from ninja import NinjaAPI, Router
from ninja.testing import TestClient
from apps.common.api.errors import add_exception_handlers


class BaseNinjaTestCase(object):
    @classmethod
    def setup_client(cls, router: Router):
        settings.DEBUG = True
        settings.IN_TEST = True

        cls.api_client = TestClient(router)

       ''' 
          https://github.com/vitalik/django-ninja/blob/c6d44b62a180fcf8ddfd73d67e0274a77b9d30ae/ninja/testing/client.py#L94-L95
          when we in test we not having urls, so it recreates api instance without attached handlers
       '''
        _ = cls.api_client.urls
       
        # add handlers
        add_exception_handlers(cls.api_client.router_or_app.api)
# example test file
from apps.common.test_client import BaseNinjaTestCase
from apps.sheets.operations.api import router

class TestOperationsApi(TestCase, BaseNinjaTestCase):
    @classmethod
    def setUpTestData(cls):
        cls.setup_client(router)

from django-ninja.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.