Wednesday 30 May 2012

Undocumented feature - error stack

This is something which has previously never been documented by Compuware.  In the Uniface 9.5 manuals it is used in a code example, but isn't explained at all.   We first found it in 2008, when we were on Uniface 9.2.  I've called it an "error stack", because that's the context we use it in, but really it's accessing the process stack, so you can determine where you are within the code at runtime.  And it's as simple as this...

stack = $proccontext("STACK")

I could just leave it there, but that seems a little mean, so I'll continue :)

At this point the "stack" variable is populated with a Uniface list.  Each item in this list contains another list, which contains the following items...

  • TYPE - The type of item on the stack ("TRG" for trigger, for example)
  • INSNAM - The instance name of the form running (or application shell)
  • TRIG - The trigger of the form running ("execute", for example)
  • ENTITY - The entity name of the item (if you're in an entity or field trigger)
  • FIELD - The field name of the item (if you're in a field trigger)
  • MODEL - The model name of the item (if you're in an entity or field trigger)
  • MODNAM - The name of the item (the entry or global procedure name, for example)
  • LNR - The line number within the item
  • LIN - The line of code itself

So you can loop through this list (forwards if you want it like a stack, or backwards if you want it to be more readable) to determine how you managed to end up where you currently are, down to the line number.  This is really useful for outputting when errors occur, especially in global procedures, as you can see what form was running at the time, right back to the application shell.

Thanks to Mark R for telling me about this one!

Summary: It is possible to determine exactly where you are within code at runtime, which is really useful for error messaging.

No comments:

Post a Comment