0

I am getting an exception which am transferring to a variable. The output of the variable is given below:

schema.SchemaError("Key 'policy' error:\nOr({'name': And(<class 'str'>, <built-in function len>), 'age': And(<class 'int'>, <function <lambda> at 0x000001C964E92DC8>), Optional('gender'): And(<class 'str'>, Use(<method 'lower' of 'str' objects>), <function <lambda> at 0x000001C964E92E58>)}) did not validate {'name': 'Sue', 'age': 28, 'gnder': 'Squid'}\nWrong key 'gnder' in {'name': 'Sue', 'age': 28, 'gnder': 'Squid'}")

Please let me know how I can get the contents of this variable, I could try to convert this into a string but then it becomes very cumbersome to extract the contents. I specifically want the contents after 'did not validate' part.

EDIT - Not looking to resolve the error, error was expected. I transferred the contents of the exception to a variable, just need to figure out how to read the variable.

4
  • 1
    Please provide a reduced version of the code that resulted in that error, and if possible also a minimized version of the input data. Commented Apr 29, 2021 at 10:16
  • Hey, am not looking to resolve the error, error was expected. I transferred the contents of the exception to a variable, just need to figure out how to read the variable. Commented Apr 29, 2021 at 10:19
  • does this help? SchemaError Commented Apr 29, 2021 at 10:30
  • Nope the error is fine, am just unable to read the error, I want the contents of the error, currently its coming as a data type as shown above. I want to get which key was wrong from the error output. Commented Apr 29, 2021 at 10:33

1 Answer 1

2
  1. Store it in variable
  2. Convert to string
  3. Search for did not validate in the converted string
  4. the string lies in 127th position
  5. search for { in the string as it is the starting after the error
  6. takeout the thing after the position
  7. convert it to any form you want
  8. ALSO: print the variable a for confirmation
  9. CODE :
a = schema.SchemaError("Key 'policy' error:\nOr({'name': And(<class 'str'>, <built-in function len>), 'age': And(<class 'int'>, <function <lambda> at 0x000001C964E92DC8>), Optional('gender'): And(<class 'str'>, Use(<method 'lower' of 'str' objects>), <function <lambda> at 0x000001C964E92E58>)}) did not validate {'name': 'Sue', 'age': 28, 'gnder': 'Squid'}\nWrong key 'gnder' in {'name': 'Sue', 'age': 28, 'gnder': 'Squid'}")
a = str(a)
b = 127 #every error starts from here
a = a[b:len(a)]
c = a.find("{")
a = a[c:len(a)]
print(a)
Sign up to request clarification or add additional context in comments.

3 Comments

Almost solved, getting this: "{'name': 'Sue', 'age': 28, 'gnder': 'Squid'}\nWrong key 'gnder' in {'name': 'Sue', 'age': 28, 'gnder': 'Squid'}" Any way I could convert this into a dictionary?
Cannot accept this as an answer as 'did not validate' string won't be there for every error, sometimes there is 'key not found', 'datatype wrong' etc.
@arunmenon Now the problem of other errors are solved ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.