-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactors the controls module, adding custom errors (#17)
* Changes "controlsClass" to factory function "set_controls". * Removes "baseControls" class and fixes bug when initialising procedure field * Adds error check to "set_controls" * Adds formatted error for extra fields to "set_controls" * Introduces logging for error reporting * Substitutes logging for raising errors directly * Adds routine "custom_pydantic_validation_error" to introduce custom error messages when raising a ValidationError
- Loading branch information
1 parent
101365b
commit 7f2ad53
Showing
7 changed files
with
380 additions
and
340 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from RAT.classlist import ClassList | ||
from RAT.controls import Controls | ||
from RAT.project import Project | ||
import RAT.controls | ||
import RAT.models |
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 |
---|---|---|
@@ -1,26 +1,36 @@ | ||
"""Defines routines for custom error handling in RAT.""" | ||
import pydantic_core | ||
|
||
from pydantic import ValidationError | ||
|
||
def custom_pydantic_validation_error(error_list: list[pydantic_core.ErrorDetails], custom_errors: dict[str, str] = None | ||
) -> list[pydantic_core.ErrorDetails]: | ||
"""Run through the list of errors generated from a pydantic ValidationError, substituting the standard error for a | ||
PydanticCustomError for a given set of error types. | ||
def formatted_pydantic_error(error: ValidationError) -> str: | ||
"""Write a custom string format for pydantic validation errors. | ||
For errors that do not have a custom error message defined, we redefine them using a PydanticCustomError to remove | ||
the url from the error message. | ||
Parameters | ||
---------- | ||
error : pydantic.ValidationError | ||
A ValidationError produced by a pydantic model | ||
error_list : list[pydantic_core.ErrorDetails] | ||
A list of errors produced by pydantic.ValidationError.errors(). | ||
custom_errors: dict[str, str], optional | ||
A dict of custom error messages for given error types. | ||
Returns | ||
------- | ||
error_str : str | ||
A string giving details of the ValidationError in a custom format. | ||
new_error : list[pydantic_core.ErrorDetails] | ||
A list of errors including PydanticCustomErrors in place of the error types in custom_errors. | ||
""" | ||
num_errors = error.error_count() | ||
error_str = f'{num_errors} validation error{"s"[:num_errors!=1]} for {error.title}' | ||
for this_error in error.errors(): | ||
error_str += '\n' | ||
if this_error['loc']: | ||
error_str += ' '.join(this_error['loc']) + '\n' | ||
error_str += ' ' + this_error['msg'] | ||
return error_str | ||
if custom_errors is None: | ||
custom_errors = {} | ||
custom_error_list = [] | ||
for error in error_list: | ||
if error['type'] in custom_errors: | ||
RAT_custom_error = pydantic_core.PydanticCustomError(error['type'], custom_errors[error['type']]) | ||
else: | ||
RAT_custom_error = pydantic_core.PydanticCustomError(error['type'], error['msg']) | ||
error['type'] = RAT_custom_error | ||
custom_error_list.append(error) | ||
|
||
return custom_error_list |
Oops, something went wrong.