Search code examples
mps

How to throw an error (at generation-time) in the default branch of a template switch?


I want to make sure that my template switch is only called with nodes it can handle. If it is used with an unrecognized concept, I would like code generation to fail with an error. Is there a way to throw generation-time errors from templates?


Note: The workaround I have been using is to generate invalid output that will cause an error down the line, for example:

default: <T  "unknown command: " + $COPY_SRC$[null]  T>

which will then generate an error like

textgen error: 'No textgen for Draw.structure.Fill' in [rightExpression] Fill null[847086916112855242] in Draw.sandbox@0

when Fill is a concept not recognized by the switch.


Solution

  • Instead of default that has a consequence you can define a "DISMISS TOP RULE" and then report the error there:

    template switch operationsSwitch extends <none>                              
    
    parameters                                                                   
    << ... >>                                                                    
    
      null-input message: <none>                                                 
    
      cases:                                                                     
    
            ...                                                     
    
    
      default: DISMISS TOP RULE error : operation  not supported by generator
    

    This will then present an error in the messages window and the user/developer can click the message to get to the input node and the switch that failed to match from there.