question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Improving enum error messages

See original GitHub issue

Question

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:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

12reactions
rodrigocauscommented, Oct 21, 2020

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!

3reactions
samuelcolvincommented, Jan 20, 2019

having a fixed error value, like value is not a valid enumeration member allows you to use it as the key of a dictionary, do str comparison, etc.

This is what the type attribute of errors is for. I would advise against using the msg of errors in a lookup as they’re like to change.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found