Search code examples
pythonmaya

Is it possible to edit the error template in Maya using python?


I'm currently learning how to code in python and I was trying to raise exceptions in my code to make my tool more user-friendly. However, I find the template given by Maya to be too simple and I'd like to have more control over it.

Normally, the base template looks a little like this:

raise Exception(errorTitle)

>>> Result
# Error: errorTitle
# Traceback (most recent call last):
#   File "<maya console>", line 1, in <module>
# Exception: errorTitle # 

It works fine, but it doesn't let you give a more detailed description of the error. What I'm trying to achieve would look a little more like this:

raise Exception(errorTitle, errorDescription)

>>> Result
# Error: errorTitle
# errorDescription
# Traceback (most recent call last):
#   File "<maya console>", line 1, in <module>
# Exception: errorTitle # 

I also tried putting the errorDescription into errorTitle by adding a new line to the string, but since errorTitle is repeated twice, it becomes confusing quickly when trying to sort errors.

I know you can't really use multiple arguments in the raise function, so I was wondering if there was an error template in Maya I could reference instead of raising an error.

Alternatively, is there a way to create my own error template? If I can reuse the Traceback function and change the script bar color, it might be easier to just do that.

Thanks in advance!


Solution

  • You can get your desired result just like that:

    >>> raise Exception("Error Title\nError description that explains the problem")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    Exception: Error Title
    Error description that explains the problem
    

    Or you may want to declare your custom exception class:

    class YourException(Exception):
        def __init__(self, title, description):
            super(YourException, self).__init__(title + "\n" + description)
    

    and then you can use your original syntax:

    >>> raise YourException("Error Title", "Error description that explains the problem")
      File "<stdin>", line 1, in <module>
    __main__.YourException: Error Title
    Error description that explains the problem