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

feat: add pydantic's error dict to ValidationException #3467

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Anu-cool-007
Copy link

@Anu-cool-007 Anu-cool-007 commented May 4, 2024

Description

  • Updated SignatureModel's _build_error_message method to accept an optional error dict and add it to the message.
  • This can be used to propagate the original error object to the error handlers.

Closes #3466

@Anu-cool-007 Anu-cool-007 requested review from a team as code owners May 4, 2024 05:45
@github-actions github-actions bot added area/private-api This PR involves changes to the privatized API size: small type/feat area/signature pr/external Triage Required 🏥 This requires triage labels May 4, 2024
Copy link

codecov bot commented May 4, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.29%. Comparing base (d5196ed) to head (ce02e80).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3467   +/-   ##
=======================================
  Coverage   98.29%   98.29%           
=======================================
  Files         328      328           
  Lines       14865    14868    +3     
  Branches     2358     2359    +1     
=======================================
+ Hits        14611    14614    +3     
  Misses        116      116           
  Partials      138      138           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

github-actions bot commented May 4, 2024

Documentation preview will be available shortly at https://litestar-org.github.io/litestar-docs-preview/3467

@@ -61,6 +61,7 @@ class ErrorMessage(TypedDict):
key: NotRequired[str]
message: str
source: NotRequired[Literal["body"] | ParamType]
exc: NotRequired[Dict[str, Any]]
Copy link
Member

Choose a reason for hiding this comment

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

I think a different name like errors or something may be better since exc is usually used to mean an exception. Wdyt?

Copy link
Author

Choose a reason for hiding this comment

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

Good point. This will contain data of a single error and will be accessed with validation_exception["extra"][0]["exc"].
How about raw, detail or error_detail?

@peterschutt
Copy link
Contributor

I'm not enthusiastic about adding more pydantic specific handling in our core, and wonder if there's not a way we can better manage this type of thing with our plugin architecture.

We have this ExtendedMsgSpecValidationError raised by the pydantic type decoders to communicate to our core error handling that the exception has extra detail that can be included in the error response. I wonder if it would be more generic to have the ExtendedMsgSpecValidationError type accept an original_exception parameter instead of the errors mapping, and let the pydantic plugin register an exception handler that can access the original pydantic exception and add the extra detail to the exception response. This way, other serialization plugins could leverage the same behavior.

@Anu-cool-007
Copy link
Author

I'm not enthusiastic about adding more pydantic specific handling in our core, and wonder if there's not a way we can better manage this type of thing with our plugin architecture.

We have this ExtendedMsgSpecValidationError raised by the pydantic type decoders to communicate to our core error handling that the exception has extra detail that can be included in the error response. I wonder if it would be more generic to have the ExtendedMsgSpecValidationError type accept an original_exception parameter instead of the errors mapping, and let the pydantic plugin register an exception handler that can access the original pydantic exception and add the extra detail to the exception response. This way, other serialization plugins could leverage the same behavior.

This approach feels more accessible. I'll try to implement this.

@peterschutt
Copy link
Contributor

I'm not enthusiastic about adding more pydantic specific handling in our core, and wonder if there's not a way we can better manage this type of thing with our plugin architecture.

We have this ExtendedMsgSpecValidationError raised by the pydantic type decoders to communicate to our core error handling that the exception has extra detail that can be included in the error response. I wonder if it would be more generic to have the ExtendedMsgSpecValidationError type accept an original_exception parameter instead of the errors mapping, and let the pydantic plugin register an exception handler that can access the original pydantic exception and add the extra detail to the exception response. This way, other serialization plugins could leverage the same behavior.

This approach feels more accessible. I'll try to implement this.

Cool, feel free to share early thoughts and if you have any questions I'll do my best to try and answer them. Perhaps best to do this in another branch in case we hit a dead end and end up back here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/private-api This PR involves changes to the privatized API area/signature pr/external size: small Triage Required 🏥 This requires triage type/feat
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enhancement: Add Pydantic's error dictionary to ValidationException's extra dict
3 participants