Skip to content

Handle transient VIES faults gracefully - #363

Open
eason4kim-rocket wants to merge 1 commit into
codingjoe:mainfrom
eason4kim-rocket:fix/vies-transient-faults-314
Open

Handle transient VIES faults gracefully#363
eason4kim-rocket wants to merge 1 commit into
codingjoe:mainfrom
eason4kim-rocket:fix/vies-transient-faults-314

Conversation

@eason4kim-rocket

Copy link
Copy Markdown

Summary

  • convert known transient VIES SOAP faults into a field-level ValidationError with the vies_unavailable code
  • keep unknown SOAP faults visible instead of swallowing them
  • cover both direct validation and the Django model-form path

When 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 passed
  • uvx ruff check vies/types.py tests/test_types.py tests/test_widgets.py
  • uvx ruff format --check vies/types.py tests/test_types.py tests/test_widgets.py

Fixes #314.

@codingjoe codingjoe left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread vies/types.py
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)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use some of Python's internal magic.

Suggested change
logger.warning("Transient VIES fault: %s", e.message)
logger.warning("Transient VIES fault: %r", e, exc_info=True)

Comment thread vies/types.py
"Please try again later."
)
raise ValidationError(msg, code="vies_unavailable") from e
logger.exception(e)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
logger.exception(e)
logger.exception("Unexpected error")

Comment thread vies/types.py
try:
return client.service.checkVat(self.country_code, self.number)
except Fault as e:
if e.message in TRANSIENT_VIES_FAULTS:

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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

Successfully merging this pull request may close these issues.

MS_MAX_CONCURRENT_REQ

2 participants