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

How to catch Validator error #224

Closed
kirill-ratkin opened this issue Feb 18, 2022 · 7 comments
Closed

How to catch Validator error #224

kirill-ratkin opened this issue Feb 18, 2022 · 7 comments

Comments

@kirill-ratkin
Copy link

kirill-ratkin commented Feb 18, 2022

In case of validation error a log message appears in console.
But example says no about how to catch the exception.
Could you add such logic to example or say couple words here how to do it?

I try to override method validated_message in my custom validator class inherited from Validator.
It doesn't work :(.

@kirill-ratkin
Copy link
Author

kirill-ratkin commented Feb 18, 2022

How I see function: wrap_function_by_validator(self, function, validator) intercepts ValidationError exception itself and print message.
So ... there is no way to detect validation error on application level, right?

@kirill-ratkin
Copy link
Author

kirill-ratkin commented Feb 18, 2022

I just did minimal changes in pull request - #225 .
It's very simple soluton. Maybe it doesn't work for all cases. But it solves my problem with validation quite complicated messages come thru NATS and maybe helps you.

@artas728
Copy link
Collaborator

Hello @kirill-ratkin
Indeed, catching a validator exception looks like a useful feature.
I'll check your PR soon

@artas728
Copy link
Collaborator

artas728 commented Feb 21, 2022

@kirill-ratkin just checked PR #225
The thing is such implementation breaks all listener functions that don’t have an argument validation_report.
I would consider adding validation_error_cb as an argument for the listener decorator. For example:

async def custom_validation_error_cb(msg):
    # custom msg handling
    log.error(f"something wrong")

@app.listen("some.subject", validator=SomeValidator, validation_error_cb=custom_validation_error_cb)
async def foo(msg):
    # msg handling if everything allright
    log.info(f"got message {msg.data}")

What do you think? Would it be convenient for your case?

@kirill-ratkin
Copy link
Author

Hi.
Yes. Good idea to add decorator parameter. It's better than mine solution.
What do you think to do with wrapped function call in case of validation error happened?

For my perpective wrapped functoin should not be called in this case and custom_validation_error_cb should be responsible for error handling. In my case I'll inform sender in reply about validation error.

Good idea!

@artas728
Copy link
Collaborator

We will think about a wrapped function.

I can't guarantee but i think we will release the new version by the end of next week, the new version will include the validation error callback feature.

@artas728
Copy link
Collaborator

artas728 commented Apr 1, 2022

#227 Update in new panini release - v0.7.1

@artas728 artas728 closed this as completed Apr 1, 2022
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