Shoutbox

[split] show error toast instead of in debug window - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: [split] show error toast instead of in debug window (/showthread.php?tid=94760)

RE: [Release] Fake media 1.60 by whiz on 06-09-2010 at 04:29 PM

Just out of interest, how do you make Plus! show a toast with an error code instead of an error in the Debug window?  I had a quick look at your code but couldn't see it...  *-)


RE: [Release] Fake media 1.60 by SmokingCookie on 06-09-2010 at 04:41 PM

What exactly do you mean? (as in: which error message?)


RE: [Release] Fake media 1.60 by whiz on 06-09-2010 at 05:20 PM

Like this.

[Image: attachment.php?pid=995425]
Occurs when opening the settings window for the second time (or fourth, sixth, and so on).


RE: [Release] Fake media 1.60 by SmokingCookie on 06-09-2010 at 05:27 PM

Well, it doesn't happen here *-) Could be that I've got a private version.

Although it definitely means I'll have something to do in the first couple of days of the upcoming vacation.

Fyi, that'll be in 4 to 5 weeks


RE: [Release] Fake media 1.60 by CookieRevised on 06-10-2010 at 11:26 AM

quote:
Originally posted by whiz
Just out of interest, how do you make Plus! show a toast with an error code instead of an error in the Debug window?  I had a quick look at your code but couldn't see it...  *-)
Afaik there isn't any magic or hidden setting to enable this.

You show the toast and error yourself.
Do this by using the try {} catch( e) {} methods. And in the catch show a toast with the error number (e.number).

So this wont work for all errors though, only the stuff you 'catch'.
RE: [Release] Fake media 1.60 by whiz on 06-10-2010 at 11:30 AM

I thought about that, but then I'd have to put one into every function...

Can it go across all functions in a file, like this?

js code:
try
{
    function Test1()
    {
        // invalid function
        foo.bar();
    }
    function Test2()
    {
        Test1();
    }
}
catch (error)
{
    MsgPlus.DisplayToast("Error!", error.description);
}

RE: [Release] Fake media 1.60 by Matti on 06-10-2010 at 12:47 PM

The sample code you defined there won't trigger an exception since you're not calling the functions inside your try block, you are just defining them. Due to the nature of JScript's treatment of global objects, it won't check for the existence of foo on declaration, since foo could still be assigned later. Therefore, there's no real use in wrapping function declarations in try-catch blocks.

Perhaps the best way to implement custom exception handling for all your functions, is to wrap the code inside all your event handlers such as OnEvent_Initialize or OnChatWndEvent_Created.

js code:
// Main event, wrapped in try-catch block
function OnEvent_Initialize() {
    try {
        DoThis();
        DoThat();
    } catch(error) {
        MsgPlus.DisplayToast("Error!", error.description);
    }
}

// Script functions calling each other
function DoThis() {
    HelpMeWithThis();
    CallingUnknownFunction(); // try-catch in main event catches this
}
function HelpMeWithThis() { ... }
function DoThat() { ... }
As you can see, despite being wrapped in other functions, the exception will still reach the try-catch block in the event handler. These are the starting points of your call stacks, so any exception occurring inside a function called from the event will bubble up in the call stack until it hits the try-catch block in your event handler code.
RE: [Release] Fake media 1.60 by whiz on 06-10-2010 at 06:36 PM

quote:
Originally posted by Matti
Perhaps the best way to implement custom exception handling for all your functions, is to wrap the code inside all your event handlers such as OnEvent_Initialize or OnChatWndEvent_Created.

<code>

As you can see, despite being wrapped in other functions, the exception will still reach the try-catch block in the event handler. These are the starting points of your call stacks, so any exception occurring inside a function called from the event will bubble up in the call stack until it hits the try-catch block in your event handler code.

js code:
Debugger.Catch = function(Function, Error)
{
    Debugger.Trace("/////////////////////////////////////////////////////////////////");
    Debugger.Trace("** Error in: " + Function + " **");
    Debugger.Trace("Description: " + Error.description + " (" + Error.code + ")");
    Debugger.Trace("Error time: " + new Date());
    Debugger.Trace("/////////////////////////////////////////////////////////////////");
}
js code:
function OnEvent_Initialize(MessengerStart)
{
    try
    {
        Debugger.Call("Initialize", {"MessengerStart" : MessengerStart});
        // ...
        foo.bar(); // invalid function
    }
    catch (error)
    {
        Debugger.Catch("Initialize", error); // line 72
    }
}
code:
From the Plus! Script Debug...
Error: Object doesn't support this property or method (code: -2146827850)
       File: FnInitialization.js. Line: 72.
Function OnEvent_Initialize returned an error. Code: -2147352318



quote:
Originally posted by SmokingCookie
Pleaaaaase don't go offtopic, or my thread will be closed :'( :P
Can someone split it from my first post, please?

RE: [Release] Fake media 1.60 by SmokingCookie on 06-10-2010 at 06:41 PM

Just a stupid question: does the Debugger object support the Trace() method?


RE: [Release] Fake media 1.60 by whiz on 06-10-2010 at 06:58 PM

Yes.  Here's the full Debugger object:

Spoiler:
js code:
var Debugger = {};

Debugger.Start = new Date();
Debugger.Data = [];

Debugger.Trace = function(Text)
{
    if (Messenger.MyStatus > 1)
    {
        try
        {
            if (Registry.Read("Options\\ChkDebug"))
            {
                Debug.Trace(Text);
                Debugger.Data.push(Text);
            }
        }
        catch (error)
        {
            Debug.Trace(Text);
            Debugger.Data.push(Text);
        }
    }
    else
    {
        Debug.Trace(Text);
        Debugger.Data.push(Text);
    }
}

Debugger.Call = function(Function, Parameters)
{
    var Notif = Function.substr(Function.length - 19, Function.length);
    if (Notif !== "MessageNotification" || (Notif === "MessageNotification" && Registry.Read("Options\\ChkDebugNotif")))
    {
        Debugger.Trace("=================================================================");
        Debugger.Trace("** Event called: " + Function + " **");
        Debugger.Trace("Call time: " + new Date());
        Debugger.Trace("-----------------------------------------------------------------");
        var Count = 0;
        for (var Parameter in Parameters)
        {
            Debugger.Trace("|| " + Parameter + ": " + Parameters[Parameter]);
            Count++;
        }
        if (Count === 0)
        {
            Debugger.Trace("(no attached parameters)");
        }
        Debugger.Trace("=================================================================");
    }
}

Debugger.Catch = function(Function, Error)
{
    Debugger.Trace("/////////////////////////////////////////////////////////////////");
    Debugger.Trace("** Error in: " + Function + " **");
    Debugger.Trace("Description: " + Error.description + " (" + Error.code + ")");
    Debugger.Trace("Error time: " + new Date());
    Debugger.Trace("/////////////////////////////////////////////////////////////////");
}

Debugger.Save = function(Path)
{
    var TextFile = FSO.CreateTextFile(Path, 2);
    TextFile.WriteLine("");
    TextFile.WriteLine("=================================================================");
    TextFile.WriteLine("**************** Interface Writer: Debugging Log ****************");
    TextFile.WriteLine("-----------------------------------------------------------------");
    TextFile.WriteLine("Start time: " + Debugger.Start);
    TextFile.WriteLine("=================================================================");
    TextFile.WriteLine("");
    for (var X in Debugger.Data)
    {
        TextFile.WriteLine(Debugger.Data[X]);
    }
    TextFile.WriteLine("");
    TextFile.WriteLine("=================================================================");
    TextFile.WriteLine("End time: " + new Date());
    TextFile.WriteLine("=================================================================");
    TextFile.Close();
}

Edit: it works if the Debugger::Catch function is specified at the start of the initialization function.
js code:
function OnEvent_Initialize(MessengerStart)
{
    Debugger.Catch = function(Function, Error)
    {
        Debugger.Trace("/////////////////////////////////////////////////////////////////");
        Debugger.Trace("** Error in: " + Function + " **");
        Debugger.Trace("Description: " + Error.description + " (" + Error.number + ")");
        Debugger.Trace("Error time: " + new Date());
        Debugger.Trace("/////////////////////////////////////////////////////////////////");
    }
   
    try
    {
        Debugger.Call("Initialize", {"MessengerStart" : MessengerStart});
        // ...
        foo.bar(); // invalid function
    }
    catch (error)
    {
        Debugger.Catch("Initialize", error);
    }
}
       
code:
From the Plus! Script Debug...
/////////////////////////////////////////////////////////////////
** Error in: Initialize **
Description: 'foo' is undefined (-2146823279)
Error time: Thu Jun 10 20:05:41 UTC+0100 2010
/////////////////////////////////////////////////////////////////

RE: [split] show error toast instead of in debug window by SmokingCookie on 06-11-2010 at 04:35 PM

It might also work as expected if you specify the Debugger object as follows:

JScript code:
var Debugger = {
    "Start":    new Date,
    "Data":    new Array(),
    [...]
}

Note that you must separate each member with a comma, except for the last member:

JScript code:
var MyObject = {
    "SomeProperty":    "Hello world!", // used a comma here; if you omit it, a syntax error will be raised on this line
    "SomeMethod":    function(s) {
        Messenger.DisplayToast("",s); //this sign is within the function itself, so it'll work
    } // not used one here
      // if you put a semicolon (;) here, a syntax error will be raised

RE: [split] show error toast instead of in debug window by whiz on 06-11-2010 at 07:19 PM

Still returns the same error.  New code:

Spoiler:
js code:
var Debugger =
{
    "Start" : new Date(),
    "Data" : [],
    "Trace" : function(Text)
    {
        if (Messenger.MyStatus > 1)
        {
            try
            {
                if (Registry.Read("Options\\ChkDebug"))
                {
                    Debug.Trace(Text);
                    Debugger.Data.push(Text);
                }
            }
            catch (error)
            {
                Debug.Trace(Text);
                Debugger.Data.push(Text);
            }
        }
        else
        {
            Debug.Trace(Text);
            Debugger.Data.push(Text);
        }
    },
    "Call" : function(Function, Parameters)
    {
        var Notif = Function.substr(Function.length - 19, Function.length);
        if (Notif !== "MessageNotification" || (Notif === "MessageNotification" && Registry.Read("Options\\ChkDebugNotif")))
        {
            Debugger.Trace("=================================================================");
            Debugger.Trace("** Event called: " + Function + " **");
            Debugger.Trace("Call time: " + new Date());
            Debugger.Trace("-----------------------------------------------------------------");
            var Count = 0;
            for (var Parameter in Parameters)
            {
                Debugger.Trace("|| " + Parameter + ": " + Parameters[Parameter]);
                Count++;
            }
            if (Count === 0)
            {
                Debugger.Trace("(no attached parameters)");
            }
            Debugger.Trace("=================================================================");
        }
    },
    "Catch" : function(Function, Error)
    {
        Debugger.Trace("/////////////////////////////////////////////////////////////////");
        Debugger.Trace("** Error in: " + Function + " **");
        Debugger.Trace("Description: " + Error.description + " (" + Error.number + ")");
        Debugger.Trace("Error time: " + new Date());
        Debugger.Trace("/////////////////////////////////////////////////////////////////");
    },
    "Save" : function(Path)
    {
        var TextFile = FSO.CreateTextFile(Path, 2);
        TextFile.WriteLine("");
        TextFile.WriteLine("=================================================================");
        TextFile.WriteLine("**************** Interface Writer: Debugging Log ****************");
        TextFile.WriteLine("-----------------------------------------------------------------");
        TextFile.WriteLine("Start time: " + Debugger.Start);
        TextFile.WriteLine("=================================================================");
        TextFile.WriteLine("");
        for (var X in Debugger.Data)
        {
            TextFile.WriteLine(Debugger.Data[X]);
        }
        TextFile.WriteLine("");
        TextFile.WriteLine("=================================================================");
        TextFile.WriteLine("End time: " + new Date());
        TextFile.WriteLine("=================================================================");
        TextFile.Close();
    }
}

RE: [split] show error toast instead of in debug window by Matti on 06-12-2010 at 07:25 AM

It might help if you could point us where line 72 is (or whatever line it was).

Here's something I noticed: you're using the name Function as parameter name, which may conflict with the JScript's built-in Function class. I suggest you change the name of the parameter (and its references).

Also, where are you defining FSO?

js code:
    "Save" : function(Path)
    {
        var TextFile = FSO.CreateTextFile(Path, 2);

RE: [split] show error toast instead of in debug window by whiz on 06-12-2010 at 11:30 AM

Line 72 is the call to Debugger::Catch.

js code:
Debugger.Catch("Initialize", error);

FSO is defined globally, in a file called "VarGlobals.js".
js code:
var FSO = new ActiveXObject("Scripting.FileSystemObject");

I've changed the variable name to "Fn", but it doesn't seem to make a difference.



Edit: fixed it!  I synced all my files with a backup tool of mine, but it overwrote a file which had an old copy of the Debug stuff, meaning the new Debug object was being cleared and filled with the old tools.  :|  Thanks, anyway.  ;)
RE: [split] show error toast instead of in debug window by SmokingCookie on 06-12-2010 at 05:14 PM

Omg you just gave me a great idea :O

[Image: attachment.php?pid=995568]

(click to enlarge)


RE: [split] show error toast instead of in debug window by whiz on 06-13-2010 at 10:14 AM

Yeah, the evaluator can be useful for testing code snippets.  Maybe I'll put a file reader in as well...  ;)


RE: [split] show error toast instead of in debug window by SmokingCookie on 06-13-2010 at 10:28 AM

Well, I was actually talking about the other window :P

About the eval window: just be extremely careful with this, as any code can be evaluated during runtime, and if WLM crashes, you might not be able to figure out what's wrong.


RE: [split] show error toast instead of in debug window by whiz on 06-13-2010 at 01:12 PM

Fair enough.  :)