[Question] Improving enum error messages
See original GitHub issueQuestion
Hello!
I’d like to improve the default error messages for enums to include the allowed values as defined in an enum class instead the of default value is not a valid enumeration member
message without having to override Configs.error_msg_templates[type_error.enum]
on the base model.
Is there a good way to parse the enum values from the Enum class?
Example:
class AllowedAWSInstanceTypesEnum(str, Enum):
t2_medium = "t2.medium"
t2_micro = "t2.micro"
class AWSBase(BaseModel):
instance_size: AllowedAWSInstanceTypesEnum
Example Json
{
"instance_size": "m4.xlarge"
}
This should generate an error similar to
value m4.xlarge is not valid. Must be one of ["t2.medium", "t2.micro"]
Thank you for your help!
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:10 (5 by maintainers)
Top Results From Across the Web
[Question] Improving enum error messages #355 - GitHub
Hello! I'd like to improve the default error messages for enums to include the allowed values as defined in an enum class instead...
Read more >Best way to define error codes/strings in Java? - Stack Overflow
My question is: Is there a better way to do this? I would prefer an solution in code, rather than reading from an...
Read more >Improving explicit error handling in Swift (with enum operations)
Question /Idea: Improving explicit error handling in Swift (with enum operations) ; Merging UserError with AnimalError ; Narrowing AnimalError.
Read more >6 Tips to Improve Your Exception Handling - Data Pipeline
6 Tips to Improve Your Exception Handling · 1. Use a single, system-wide exception class · 2. Use enums for error codes ·...
Read more >Using error codes effectively | Andrzej's C++ blog
In our case, we already said we will be mapping error code values from any sub-system onto the normalized error condition, represented by...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hello.
In Pydantic 1.6.1 the message to Enum ValidationError is in general:
value is not a valid enumeration member; permitted: ...
. I’d like to know, in most cases, what value led to that error (not only in Enum types). The ValidationError ends up overlapping the ValueError message that usually informs such value. Is there any form of adding this in the message template or context field?Thank you!
This is what the
type
attribute of errors is for. I would advise against using themsg
of errors in a lookup as they’re like to change.