-
Notifications
You must be signed in to change notification settings - Fork 516
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Handle NotFound and UnprocessableEntity errors in middleware (#3327)
* ✨ Handle NotFound and UnprocessableEntity exceptions at info log level Signed-off-by: ff137 <ff137@proton.me> * 🎨 replace traceback print with exception log Signed-off-by: ff137 <ff137@proton.me> * 🎨 replace deprecated .warn with .warning Signed-off-by: ff137 <ff137@proton.me> * 🎨 setting ledger to read-only should not print error log Signed-off-by: ff137 <ff137@proton.me> * ✨ extract the marshmallow validation error message from the nested exception Signed-off-by: ff137 <ff137@proton.me> * 🎨 modify import for consistency Signed-off-by: ff137 <ff137@proton.me> * ✅ test coverage for new method Signed-off-by: ff137 <ff137@proton.me> * ✅ test coverage for exception handling changes Signed-off-by: ff137 <ff137@proton.me> * 🎨 refactor for reduced complexity Signed-off-by: ff137 <ff137@proton.me> * 🎨 reorder exceptions by severity Signed-off-by: ff137 <ff137@proton.me> * 🎨 update log level Signed-off-by: ff137 <ff137@proton.me> * 🎨 Signed-off-by: ff137 <ff137@proton.me> --------- Signed-off-by: ff137 <ff137@proton.me> Co-authored-by: jamshale <31809382+jamshale@users.noreply.github.com>
- Loading branch information
Showing
7 changed files
with
308 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
"""Extract validation error messages from nested exceptions.""" | ||
|
||
from aiohttp.web import HTTPUnprocessableEntity | ||
from marshmallow.exceptions import ValidationError | ||
|
||
|
||
def extract_validation_error_message(exc: HTTPUnprocessableEntity) -> str: | ||
"""Extract marshmallow error message from a nested UnprocessableEntity exception.""" | ||
visited = set() | ||
current_exc = exc | ||
while current_exc and current_exc not in visited: | ||
visited.add(current_exc) | ||
if isinstance(current_exc, ValidationError): | ||
return current_exc.messages | ||
current_exc = current_exc.__cause__ or current_exc.__context__ | ||
return exc.reason |
Oops, something went wrong.