Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Object of type ValidationError is not JSON serializable #23

Open
adityapatil123 opened this issue May 1, 2022 · 2 comments

Comments

@adityapatil123
Copy link

adityapatil123 commented May 1, 2022

This library has been great help to me. Although I'm facing one issue.

I'm using flask-restx library

from flask_expects_json import expects_json
from flask_restx import Namespace, Resource, reqparse
import json

search_namespace = Namespace('search', description='Search Namespace')

@search_namespace.route("/v1/on_search")
class AddSearchCatalogues(Resource):

    @expects_json(some_schema)
    def post(self):
        return add_search_catalogues(g.data)

I'm getting this error:

jsonschema.exceptions.ValidationError: 'fulfillment_id' is a required property

Failed validating 'required' in schema['properties']['message']['properties']['catalog']['properties']['bpp/providers']['items']['properties']['items']['items']:
.
.
.
During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/Aditya/PycharmProjects/venvs/sandbox-protocol-py/lib/python3.7/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/Aditya/PycharmProjects/venvs/sandbox-protocol-py/lib/python3.7/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/Aditya/PycharmProjects/venvs/sandbox-protocol-py/lib/python3.7/site-packages/flask_cors/extension.py", line 165, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/Users/Aditya/PycharmProjects/venvs/sandbox-protocol-py/lib/python3.7/site-packages/flask_restx/api.py", line 671, in error_router
    return original_handler(f)
  File "/Users/Aditya/PycharmProjects/venvs/sandbox-protocol-py/lib/python3.7/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/Aditya/PycharmProjects/venvs/sandbox-protocol-py/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/Users/Aditya/PycharmProjects/venvs/sandbox-protocol-py/lib/python3.7/site-packages/flask_restx/api.py", line 669, in error_router
    return self.handle_error(e)
  File "/Users/Aditya/PycharmProjects/venvs/sandbox-protocol-py/lib/python3.7/site-packages/flask_restx/api.py", line 766, in handle_error
    data, code, headers, fallback_mediatype=fallback_mediatype
  File "/Users/Aditya/PycharmProjects/venvs/sandbox-protocol-py/lib/python3.7/site-packages/flask_restx/api.py", line 430, in make_response
    resp = self.representations[mediatype](data, *args, **kwargs)
  File "/Users/Aditya/PycharmProjects/venvs/sandbox-protocol-py/lib/python3.7/site-packages/flask_restx/representations.py", line 25, in output_json
    dumped = dumps(data, **settings) + "\n"
  File "/Users/Aditya/.pyenv/versions/3.7.2/lib/python3.7/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/Users/Aditya/.pyenv/versions/3.7.2/lib/python3.7/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/Users/Aditya/.pyenv/versions/3.7.2/lib/python3.7/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/Users/Aditya/.pyenv/versions/3.7.2/lib/python3.7/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type ValidationError is not JSON serializable

I think stringifying error object should do the work
at

                return abort(400, e)

Is there any way we can handle this?

Thank you

@Fischerfredl
Copy link
Owner

Hi, great to hear that this library is useful for you.

Unfortunately I have no experience with flask-restx. The error object is passed intentionally to abort so users can do their own error handling or error formating.

The Readme has a paragraph about error handling. https://github.com/Fischerfredl/flask-expects-json/blob/master/README.md#error-handling

Does this help you?

@adityapatil123
Copy link
Author

Oh ok I got it, so one has to implement error-handler for error-handling.
Just a suggestion, we can default error-handler to avoid json-serialisation error.

Thanks a lot @Fischerfredl 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants