script tool: Exception traceback

Discussion and information relevant to creating special missions, new ships, skins etc.

Moderators: winston, another_commander

Post Reply
cag
Deadly
Deadly
Posts: 202
Joined: Fri Mar 17, 2017 1:49 am

script tool: Exception traceback

Post by cag »

Using JavaScript's try ... catch statement, a primitive form of call stack traceback is possible.

Code: Select all

this._initOptions = function initOptions( oxp ) {
  try {
    registeredOxps[ oxp ] = ...
    ...
  } catch( err ) {
    log(this.name, ws._reportError( err, initOptions, [oxp, registeredOxps, decodedChoice], 1 ) );
    if( debug ) throw err;      // NB: not _initOptions
  }
}
The err argument from catch is an Error object with properties:
message, fileName, lineNumber, stack & name

[I'm calling it primitive because the stack property is just one long string, so the function parameters cannot be evaluated. <sigh> We work with what we have.]

I've witten a script that parses & formats this object into something readable (default depth = 1):

Code: Select all

function initOptions()   caught:    TypeError: condition.prev is undefined
    parameters: [ "telescope_",
                  { telescope_: <object of 20> },
                  { key: null, type: null, defawlt: null, min: null, max: null, numBits: null, selection: null, error: null } ]
    file: ../AddOns/station_options.oxp/Scripts/station_options.js
        line: 1119, conditionNotMet( "MassLockViewDirn" )
        line: 1216, formatOption( "MassLockViewDirn", [object Array] )
        line: 1057, buildOptions( 22, "WW_next_pg" )
        line:  954, displayOptions()
        line:  900, initOptions( "telescope_" )
vs the usual from the debug console

Code: Select all

Exception: TypeError: condition.prev is undefined
    Active script: station_options 1.0
    station_options.js, line 1119:
    var goof = condition.prev.next;
or the Latest.log file

Code: Select all

[script.javaScript.exception.unexpectedType]: ***** JavaScript exception (station_options 1.0): TypeError: condition.prev is undefined
[script.javaScript.exception.unexpectedType]:       ../AddOns/station_options.oxp/Scripts/station_options.js, line 1119.
and that last is only with the developers release! :cry:

Of the 5 parameters to _reportError, only the first 3 are required:

err
  • the Error object the catch statement provides
func
  • the function you're in (or a string with the function's name)
parms
  • the parameter of that function; if more than one, put them in an array. Dispite the name, you can pass anything in the function's scope you want to be displayed.

The two optional ones deal with how much of the items in parms you want displayed.

depth
  • number of levels to recurse
goDeep
  • boolean toggling object inheritance

The same example, now with depth = 2:

Code: Select all

function initOptions()   caught:    TypeError: condition.prev is undefined
    parameters: [ "telescope_",
                  { telescope_: { hostOxp: [Script "telescope" version 2.0], keyPrefix: "telescope_", optionsAllowedCallback: function _stnOptionsAllowed(), callPWSG: true, notifyCallback: function _reloadFromStn(), suppressSummary: false, hostVarPrefix: "$", lowestValue: "0", highestValue: null, maxPrecision: "7", maxVectorLen: "2", allow_reset: true, formatting: true, optionPages: <object of 4>, pageLabels: [ "config", "inflight", "UI_and_docs", "experimental" ], optionTabStops: <array of 4>, interface_title: "Telescope Options", interface_summary: "Edit all hardware and in-flight options; includes quick start & readme", interface_category: "Ship Systems", callback: function () } },
                  { key: null, type: null, defawlt: null, min: null, max: null, numBits: null, selection: null, error: null } ]
    file: ../AddOns/station_options.oxp/Scripts/station_options.js
        line: 1119, conditionNotMet( "MassLockViewDirn" )
        line: 1216, formatOption( "MassLockViewDirn", [object Array] )
        line: 1057, buildOptions( 22, "WW_next_pg" )
        line:  954, displayOptions()
        line:  900, initOptions( "telescope_" )
and depth = 3:

Code: Select all

function initOptions()   caught:    TypeError: condition.prev is undefined
    parameters: [ "telescope_",
                  { telescope_: { hostOxp: [Script "telescope" version 2.0],
                                  keyPrefix: "telescope_",
                                  optionsAllowedCallback: function _stnOptionsAllowed(),
                                  callPWSG: true,
                                  notifyCallback: function _reloadFromStn(),
                                  suppressSummary: false,
                                  hostVarPrefix: "$",
                                  lowestValue: "0",
                                  highestValue: null,
                                  maxPrecision: "7",
                                  maxVectorLen: "2",
                                  allow_reset: true,
                                  formatting: true,
                                  optionPages: { config: [ "Brief", "AutoScan", "AutoScanMaxRange", "FarStatus", "AutoLock", "GravLock", "IdentLock", "IdentDelay", "LightBallMinDist", "LightBallShipMinDist", "ShowMassLock", "MassLockViewDirn", "RedAlertDist", "SniperRingSize", "SniperRingColor", "ModelRingColor", "VTarget_HUD_shift" ], inflight: [ "Keys", "RemoveInFlight", "LightBalls", "ShipLightBalls", "LargeLightBalls", "MassLockBorders", "BrightMassLockBorders", "SniperMinRange", "SniperRange", "Steering", "MaxTargets", "ShowVisualTarget", "VisualTargetNormalSize", "VisualTargetCombatSize", "VisualTargetRing", "ShowVisualStation", "ShowVisualQuestionMark" ], UI_and_docs: [ "ConsoleMsgDurn", "GravScanMsgFreq", "IdentMessages", "DebugMessages", "ShowSummary", "FrameRate", "EffectData", "QuickStart", "Equipment", "Extender", "Support", "ReleaseNotes" ], experimental: [ "MFDFiltering", "MFDPrimaryStatic", "MFDPrimaryDynamic", "SeparateMFDs", "MFDAuxStatic", "MFDAuxDynamic", "Thargoids" ] },
                                  pageLabels: [ "config", "inflight", "UI_and_docs", "experimental" ],
                                  optionTabStops: [ [ 8.4, 14.6 ], [ 10.4, 14 ], [ 8, 10.5 ], [ 8.6, 12 ] ],
                                  interface_title: "Telescope Options",
                                  interface_summary: "Edit all hardware and in-flight options; includes quick start & readme",
                                  interface_category: "Ship Systems",
                                  callback: function () } },
                  { key: null, type: null, defawlt: null, min: null, max: null, numBits: null, selection: null, error: null } ]
    file: ../AddOns/station_options.oxp/Scripts/station_options.js
        line: 1119, conditionNotMet( "MassLockViewDirn" )
        line: 1216, formatOption( "MassLockViewDirn", [object Array] )
        line: 1057, buildOptions( 22, "WW_next_pg" )
        line:  954, displayOptions()
        line:  900, initOptions( "telescope_" )
I won't bore you with examples of goDeep; it's just more of the same. If the default output is too large, there's always depth = 0:

Code: Select all

function initOptions()   caught:    TypeError: condition.prev is undefined
    parameters: [ "telescope_", <object of 1>, { key: null, type: null, defawlt: null, min: null, max: null, numBits: null, selection: null, error: null } ]
    file: ../AddOns/station_options.oxp/Scripts/station_options.js
        line: 1119, conditionNotMet( "MassLockViewDirn" )
        line: 1216, formatOption( "MassLockViewDirn", [object Array] )
        line: 1057, buildOptions( 22, "WW_next_pg" )
        line:  954, displayOptions()
        line:  900, initOptions( "telescope_" )
If your function has properties, they are displayed exactly like parameters, but you can specify a different depth (pass depth as an array, eg. [ 2, 1 ]).

There's a more complete explanation in the file itself. Just paste it into your script and you're good to go.

https://www.dropbox.com/s/cjldriko9aaq ... r.js?dl=0
"Better to be thought a fool, boy, than to open your trap and remove all doubt." - Grandma [over time, just "Shut your trap... fool"]
"The only stupid questions are the ones you fail to ask." - Dad
How do I...? Nevermind.
User avatar
Cholmondely
Archivist
Archivist
Posts: 5366
Joined: Tue Jul 07, 2020 11:00 am
Location: The Delightful Domains of His Most Britannic Majesty (industrial? agricultural? mainly anything?)
Contact:

Re: script tool: Exception traceback

Post by Cholmondely »

cag wrote: Wed Apr 15, 2020 6:13 am
Using JavaScript's try ... catch statement, a primitive form of call stack traceback is possible.
...
I rather think that this should be linked to the wiki.

Does anybody have any idea as to where that should be?

Debug Console? say, or Oolite utilities?
Comments wanted:
Missing OXPs? What do you think is missing?
Lore: The economics of ship building How many built for Aronar?
Lore: The Space Traders Flight Training Manual: Cowell & MgRath Do you agree with Redspear?
cag
Deadly
Deadly
Posts: 202
Joined: Fri Mar 17, 2017 1:49 am

Re: script tool: Exception traceback

Post by cag »

I don't think it belongs with Debug Console, as it's separate and will work just as well without a developer's release (while Debug Console requires one).
(BTW, in Links section at the bottom of
http://wiki.alioth.net/index.php/Debug_OXP
the 2nd & 3rd both refer to the same page and should be merged into one item.)

It doesn't really qualify for Oolite utilities, as it's not an oxp, just a function some oxp developers may find useful. I don't know if the wiki has a place for developers to copy/paste useful bits & bobs. If not, perhaps we need one similar (but separate) to
http://wiki.alioth.net/index.php/Scripting_Tasks
"Better to be thought a fool, boy, than to open your trap and remove all doubt." - Grandma [over time, just "Shut your trap... fool"]
"The only stupid questions are the ones you fail to ask." - Dad
How do I...? Nevermind.
User avatar
Cholmondely
Archivist
Archivist
Posts: 5366
Joined: Tue Jul 07, 2020 11:00 am
Location: The Delightful Domains of His Most Britannic Majesty (industrial? agricultural? mainly anything?)
Contact:

Re: script tool: Exception traceback

Post by Cholmondely »

cag wrote: Thu Nov 25, 2021 10:42 pm
I don't think it belongs with Debug Console, as it's separate and will work just as well without a developer's release (while Debug Console requires one).
(BTW, in Links section at the bottom of
http://wiki.alioth.net/index.php/Debug_OXP
the 2nd & 3rd both refer to the same page and should be merged into one item.)

It doesn't really qualify for Oolite utilities, as it's not an oxp, just a function some oxp developers may find useful. I don't know if the wiki has a place for developers to copy/paste useful bits & bobs. If not, perhaps we need one similar (but separate) to
http://wiki.alioth.net/index.php/Scripting_Tasks
What about just bunging it in a new Links section on the bottom of the Scripting Tasks page?

Oh! Just curious:

"Better to be thought a fool, boy, than to open your trap and remove all doubt." - Grandma [over time, just "Shut your trap... fool"]
"The only stupid questions are the ones you fail to ask." - Dad
How do I...? Nevermind.


Grandma is Dad's mother?

And: What about your new Telescope 2.0? Does it need testing?
Comments wanted:
Missing OXPs? What do you think is missing?
Lore: The economics of ship building How many built for Aronar?
Lore: The Space Traders Flight Training Manual: Cowell & MgRath Do you agree with Redspear?
cag
Deadly
Deadly
Posts: 202
Joined: Fri Mar 17, 2017 1:49 am

Re: script tool: Exception traceback

Post by cag »

Cholmondely wrote: Thu Nov 25, 2021 11:23 pm
What about just bunging it in a new Links section on the bottom of the Scripting Tasks page?
That would work. Unless we get a whole bunch, there's no need for a separate page.
Cholmondely wrote: Thu Nov 25, 2021 11:23 pm
Grandma is Dad's mother?
Nope. Mum grew up on a farm, Dad, the inner city. The gods laugh, men weep.
Cholmondely wrote: Thu Nov 25, 2021 11:23 pm
And: What about your new Telescope 2.0? Does it need testing?
Since you offered:
https://www.dropbox.com/s/swfwx5klha3dn ... e.oxz?dl=0
[/url]
https://www.dropbox.com/s/dzz9ayq1e0jhv ... s.oxz?dl=0
[/url]
Edit Nov.26/21: links now match Telescope 2.0 1st post
I'm just putting on finishing touches and plan to release soon. Now I just got to learn the expansion manager and file the paperwork for credentials.
"Better to be thought a fool, boy, than to open your trap and remove all doubt." - Grandma [over time, just "Shut your trap... fool"]
"The only stupid questions are the ones you fail to ask." - Dad
How do I...? Nevermind.
Post Reply