Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error handler module #7

Open
wants to merge 5 commits into
base: 3.2.0-gnancraft
Choose a base branch
from
Open

Conversation

Gloorf
Copy link

@Gloorf Gloorf commented Aug 2, 2018

Currently, this only works with nashorn

  • Can pretty print an exception :

    • display the message (with color !)
    • display the function name (with color !) See later for explanation
    • Remove unwanted stack lines, by filtering on filename : by default, only display stack line located in scriptcraft/players : each player will only see the stack lines in his files
  • Exports eventWrapper : a decorator intended for events (registered via events.*), catching exceptions and pretty printing them to relevant player(s)

  • Exports functionWrapper : decorator intended for function, catching exceptions and pretty printing them to the function caller

  • Direct integration with scriptcraft __onCommand : catch all exception generated by /js command, and pretty print (instead of simply displaying the exception message)

Finding the function name

Nashorn doesn't make it easy for us : it doesn't give the methodName for JS code, only the filename + line number. So i can't get the function name with only the stack.

Fortunately, __onCommand uses nashorn load function (see doc), which has a neat feature : you can pass a 'name', which will be displayed in the stack trace. Specifically, it's displayed on the stack frame corresponding to the __onCommand call, which is identified by the 'methodName' beeing ''.

We still need to find the function name, when __onCommand is called. To do this, I've opted for inline comments (like shout module), of the form "#f:fnName". The clickable chat module automatically adds them to the command (I recommend using that !).

Some example screenshots

Wrapped event

capture d ecran de 2018-08-02 17-34-18

The code is the following :

var eventWrapper = require("error-handler").eventWrapper;
events.playerAnimation(eventWrapper(function(event) {
    echo(event.player, "playerAnimationEvent");
    event.player.invalidMethod();
}));

java exception

capture d ecran de 2018-08-02 17-34-29

Note : this is a regular function, the exception is catched by the direct integration with __onCommand

javascript exception

capture d ecran de 2018-08-02 17-34-42

Note : this is a regular function, the exception is catched by the direct integration with __onCommand

javascript exception (without function name comment)

capture d ecran de 2018-08-02 17-34-50

Note : this is a regular function, the exception is catched by the direct integration with __onCommand

/js command exception

capture d ecran de 2018-08-02 17-41-49

Can be used to generate clickable links in chat to execute code without
using /js specific call
Main goal is use in classroom, to make the student's life easier
* Pretty print an exception, removing the confusing part
* Can wrap around an event (using eventWrapper) to automatically catch
exception, and send the pretty version to a relevant player
* Can wrap around a function (using functionWrapper) to automatically
catch exception, and send the pretty version to the function caller
* Export the function name (if possible) when calling a function with
/js to allow for easier debugging
* Automatically catch any exception when using /js, and prettyprint them
to the caller
Remove my dev setup specific path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant