0

I have the following error message:

type object 'SomeClass' has no attribute 'some_class_field'

The exception is of type 'AtrributeError'.

Is there any way to change the error message to:

Provider class 'SomeClass' has no field 'some_class_field'

I guess those parameters are saved in the python exception class and I think there could be a solution of the following type:

try:
    # some code
except AtrributeError as ae:
    print("Provider class '{0}' has no field '{1}'").format(ae.type_object, ae.attribute))
    exit(1)
3
  • 1
    you can always extract the values from the exception object. Why do you need to customize the message like this, though? Commented May 25, 2017 at 11:50
  • @Jim Fasarakis Hilliard I was wondering if it could happen whithout defining my own exceptions, that is why I asked. Also I thought it would be better more understandable for people that don't write in python. Commented May 25, 2017 at 11:56
  • 1
    Don't define your own exceptions; get the exception message from ae.args[0] and extract the parts you need. Then print them out (using .format or whatever else you need). Commented May 25, 2017 at 11:58

1 Answer 1

1

simply You cant do that.

if you do like that again you will exception like this

    print("Provider class '{0}' has no field '{1}'").format(ae.type_object, ae.attribute)
AttributeError: 'exceptions.AttributeError' object has no attribute 'type_object'

from Jim Fasarakis Hilliard comment:

we can't access like that because ae has only message key. it doesn't have any other key for what you are trying to access. Don't define your own exceptions; get the exception message from ae.args[0] and extract the parts you need. Then print them out (using .format or whatever else you need). – Jim Fasarakis Hilliard

Sign up to request clarification or add additional context in comments.

2 Comments

Please add the idea of the Jim Fasarakis Hilliard comments as a short explanatory text and I will accept your answer. This is for more comprehensive closure of the topic, thank you in advance.
@stefan.stt thank you. i update my answer. please check

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.