Comments (1)
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)
- [BUG] Converter `uuid` is already registered - deprecated in django 6.0 HOT 1
- Make 'exclude_none' and other options easier to enable per router
- [BUG] Field serialized when using repr=False HOT 1
- How to create an alias for a field created via annotation HOT 2
- [BUG] model_dump defaults to `mode=python`
- How can I use resolve_<field> for async api?
- Mypy complaining about PatchDict HOT 1
- [Question] Unable to cache paginated queryset `PicklingError` HOT 4
- Recommend approach to expose generic error handling to the generated OpenAPI specification
- [DOC] guides/input/file-params/ should say "Django's Upload_ed_File..."
- [BUG] Openapi generated docs won't submit arrays correctly in forms. HOT 1
- [BUG] JWTAuth() is inconsistent with django authentication? HOT 1
- [BUG] computed_field is not present in openapi spec
- [BUG] Throttle should be an instance of BaseThrottle HOT 1
- [BUG] use put method can not parser form-data
- [BUG] Performance issue when using pagination HOT 4
- Django model id field is not recognized for ModelSchema HOT 1
- ModelSchema inheritance
- Could you tell me how to achieve add_router, the management of permissions, such as the request, the user. The groups. The filter (name = 'admin'). The exists () to True to this route
- Schema export only supports mounting the api under `/api/`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-ninja.