Search Unity

Make javascript errors not alert in 5.6?

Discussion in 'Web' started by Fizzer, Apr 18, 2017.

  1. Fizzer

    Fizzer

    Joined:
    Nov 16, 2016
    Posts:
    40
    Hello,

    When I embed my Unity webgl app into a page, I notice that any javascript errors that occur on the page pop up this message in an alert:

    An error occured running the Unity content on this page. See your browser JavaScript console for more info. The error was: blah blah blah
    I have defined an errorhandler function in my module like this:

    UnityLoader.instantiate("gameContainer", "Unity.json", { Module: { errorhandler: function() { } } });

    This errorhandler function is getting called fine for Unity-related errors, such as Unity running out of memory. However, if there's a random javascript error on the page unrelated to Unity, I still see it appear as an alert and my errorhandler function never gets called.

    In my case, Unity is running alongside all the normal javascript of my website. So I don't want it handling errors of javascript that's completely unrelated to it. I already have javascript error handling code that I want to handle errors normally as if Unity wasn't there.

    I can see this happening in UnityLoader.js. It calls window.addEventListener("error", ) and then attempts to match that to a Unity module. If it can't, it just alert's the error with seemingly no exposed hooks.

    Having all errors happen through this alert is a bad experience for customers for a few reasons. For one, it looks unprofessional, especially since there's a typo (occured -> occurred). Second, I can't give the user any assistance on how to fix it, or tell them contact me for support. Third, I can't log it to investigate like I do with all my other errors. Finally, it instructs the customer to use their JavaScript console which isn't something non-technical users should really be doing or will understand how to use/interpret. This is just setting them up for failure.

    I know can I assign my own function to UnityLoader.Error.handler to handle it my own way, but there's no good place to assign this. It won't work if I assign it before Unity loads, and if I try to assign it after Unity loads I can't guarantee that an error won't occur between when Unity loads and when I assign it.

    I also know I can make changes to UnityLoader.js, but it's minifed so I don't think this is the intended solution, plus I want a solution that will be compatible in future version of Unity.

    What's the intended solution here?

    I'm running Unity 5.6.0f3 and seeing this behavior in both FireFox and Chrome (latest release versions).
     
  2. TheRoccoB

    TheRoccoB

    Joined:
    Jun 29, 2017
    Posts:
    54
    I just put mine right before the gameInstance is instantiated. Works like a charm:

    //any js errors are handled like regular errors at this point

    UnityLoader.Error.handler = function(e, t){
    //do something with the error here if you wish.
    //i personally just leave this an empty function, and it seems to default to a regular JS error.
    }

    var gameInstance = UnityLoader.instantiate("gameContainer" //unityloader uses the handler above from this point on.
     
  3. dansav

    dansav

    Joined:
    Sep 22, 2005
    Posts:
    510
    Thanks very much for this solution.