Skip to content

Fix exception handler parameter call#253

Merged
wu-clan merged 7 commits into
fastapi-practices:masterfrom
wu-clan:fix-exceptions-handler
Dec 19, 2023
Merged

Fix exception handler parameter call#253
wu-clan merged 7 commits into
fastapi-practices:masterfrom
wu-clan:fix-exceptions-handler

Conversation

@wu-clan

@wu-clan wu-clan commented Dec 17, 2023

Copy link
Copy Markdown
Member

WIP.

@wu-clan
wu-clan requested a review from downdawn December 18, 2023 17:14
@wu-clan

wu-clan commented Dec 18, 2023

Copy link
Copy Markdown
Member Author

PLR.

@wu-clan wu-clan mentioned this pull request Dec 18, 2023
message += f'{data.get(field, field) if field != "__root__" else ""} {msg}' + '.'
elif isinstance(raw_error.exc, json.JSONDecodeError):
message += 'json解析失败'
raw_error = exc.raw_errors[0]

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The loop is no longer used because we end up returning only one error message, the loop outputs the last error message, and using slices outputs the first error message.

This is done to be compatible with the logic in v2, making it less confusing during the migration.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

raw_error = exc.raw_errors[0] if exc.raw_errors else None

Would this way be better?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

In fact, if raw_errors is empty, raw_errors[0] will raise an error. The same error will occur for for raw_error in exc.raw_errors.

If add if exc.raw_errors..., an exception will be thrown only when raw_exc = raw_error.exc is encountered. Clearly, this is not compatible.

message += f'{data.get(field, field) if field != "__root__" else ""} {msg}' + '.'
elif isinstance(raw_error.exc, json.JSONDecodeError):
message += 'json解析失败'
raw_error = exc.raw_errors[0]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

raw_error = exc.raw_errors[0] if exc.raw_errors else None

Would this way be better?

@wu-clan
wu-clan merged commit 1574d54 into fastapi-practices:master Dec 19, 2023
@wu-clan
wu-clan deleted the fix-exceptions-handler branch December 19, 2023 13:17
chuxijin pushed a commit to chuxijin/fba that referenced this pull request Feb 8, 2026
* Fix exception handler parameter call

* update the validation exception handler

* Update the content returned by the validation exception

* update import of JSONDecodeError

* fix validation exception handler typing

* Update the content returned by the validation exception handler
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.

2 participants