Mishaps - Spicery/Nutmeg GitHub Wiki

Inside the compiler, if you wish to raise an error for the user, you should use the Mishap class imported from the mishap module. For example:

from mishap import Mishap
raise Mishap( "Trying to declare a top-level variable as assignable", name=name_of_variable, hint="Top level variables must be val or const" )

The Mishap will propagate to the top-level of the Nutmeg compiler where it is turned into a nice error message for the user. The keyword arguments are printed out together with the message to provide extra context.

As the Mishap propagates out you may want to add extra contextual details. For this use the following idiom:

try:
    STATEMENTS
except Mishap as m:
    raise m.addDetails( in_procedure=getProcedureName() )

You do not have to worry about name clashes between added details and the original details as each set of keyword-arguments is retained separately.