Handle transient VIES faults gracefully - #363
Conversation
There was a problem hiding this comment.
Wonderful start!
The current code is a little old, and I am ashamed of it. While you are at it, can you improve the logging messages? Python has a native way to include exceptions (exc_info=True). Log messages should provide additional context and don't need to include the exception.
I'd be much simpler if we'd get explicit exceptions per failure, not just Fault, but hey 🤷
Thanks
-joe
| return client.service.checkVat(self.country_code, self.number) | ||
| except Fault as e: | ||
| if e.message in TRANSIENT_VIES_FAULTS: | ||
| logger.warning("Transient VIES fault: %s", e.message) |
There was a problem hiding this comment.
Let's use some of Python's internal magic.
| logger.warning("Transient VIES fault: %s", e.message) | |
| logger.warning("Transient VIES fault: %r", e, exc_info=True) |
| "Please try again later." | ||
| ) | ||
| raise ValidationError(msg, code="vies_unavailable") from e | ||
| logger.exception(e) |
There was a problem hiding this comment.
| logger.exception(e) | |
| logger.exception("Unexpected error") |
| try: | ||
| return client.service.checkVat(self.country_code, self.number) | ||
| except Fault as e: | ||
| if e.message in TRANSIENT_VIES_FAULTS: |
There was a problem hiding this comment.
It might be nice to use match..case here and provide proper messages (or at least log messages) per expected failure.
E.G. a timeout should be logged as an error, not a warning, since its the cause could be inside your own infrastructure.
Summary
ValidationErrorwith thevies_unavailablecodeWhen VIES returns
MS_MAX_CONCURRENT_REQ(or another documented temporary availability fault), the form now reports that VIES is temporarily unavailable instead of allowing the Zeep fault to become a server error. The VAT number is not described as invalid.Tests
uv run pytest— 39 passeduvx ruff check vies/types.py tests/test_types.py tests/test_widgets.pyuvx ruff format --check vies/types.py tests/test_types.py tests/test_widgets.pyFixes #314.