Search Unity

Is there a way to unhook the Javascript error popup?

Discussion in 'Web' started by mattbrandmm, Sep 10, 2018.

  1. mattbrandmm

    mattbrandmm

    Joined:
    Jun 5, 2018
    Posts:
    90
    Currently UnityLoader.js handles errors, so that if an error occurs during loading, it pops up a window.

    I want to be able to catch and log loading errors. Is there a graceful way to unhook the catching from UnityLoader?

    My one strategy is to unminify UnityLoader.js, unhook it in there, and reminify it. But I would like to know if there is a better way to do it.
     
  2. JJJohan

    JJJohan

    Joined:
    Mar 18, 2016
    Posts:
    214
    Yep, quick way to do it is to override the error handler - you don't have to do this in the UnityLoader itself, you can do this in your own separate JS script block.

    Code (JavaScript):
    1. UnityLoader.Error.handler = function(e, t)
    2. {
    3.     console.log(e);
    4. }
    5.  
    e is the error (e.message is the raw string if you want to compare it against certain types of errors)
    Unfortunately I can't remember of the top of my head what 't' is.
     
    ravishnair likes this.
  3. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    the second parameter is the Unity Module object.
     
  4. mattbrandmm

    mattbrandmm

    Joined:
    Jun 5, 2018
    Posts:
    90
    awesome, thank you so much guys!
     
  5. ravishnair

    ravishnair

    Joined:
    Aug 8, 2016
    Posts:
    20
    When you say your own separate JS block, do you mean in a different .jslib file? Would that work?

    Edit: OHMYGOLYGOSH you are right. It does work. <3 Thanks!
     
    Last edited: Sep 19, 2018
  6. asimonar

    asimonar

    Joined:
    Jan 25, 2018
    Posts:
    2
    Hey there, trying to implement this same fix! A bit new to using jslib files; I created a jslib file with below contents, and dropped that in the Plugins folder, but it is failing to compiler on build without giving much feedback, just an obscure stracktrace.

    What am I doing wrong here? Any help would be greatly appreciated!

    Code (JavaScript):
    1. mergeInto(LibraryManager.library, {
    2.     UnityLoader.Error.handler = function(e,t)
    3.     {
    4.         console.log(e);
    5.     },
    6. });
     
    Last edited: Feb 12, 2019
  7. ravishnair

    ravishnair

    Joined:
    Aug 8, 2016
    Posts:
    20
    Code (CSharp):
    1. mergeInto(LibraryManager.library, {
    2.      UnityLoader.Error.handler = function(e,t)
    3.      {
    4.          console.log(e);
    5.      },
    6.  };);
    maybe that?

    or just write it separately so it is more readable. like

    Code (CSharp):
    1. var dodgeFn = {
    2.     UnityLoader.Error.handler = function(e,t)
    3.     {
    4.        console.log(e);
    5.     },
    6. };
    7. mergeInto(LibraryManager.library, dodgeFn);
     
  8. asimonar

    asimonar

    Joined:
    Jan 25, 2018
    Posts:
    2
    Thanks for the help @ravishnair , unfortunately it's still failing on build. There's not much feedback, other than a long console output starting like this:

    Code (CSharp):
    1. stdout:
    2. stderr:WARNING:root:--separate-asm works best when compiling to HTML. Otherwise, you must yourself load the '.asm.js' file that is emitted separately, and must do so before loading the main '.js' file. [-Wseparate-asm]
    3. error: failure to execute js library
    Seems like it doesn't like the syntax and is getting a compiler error when converting, but what's strange is that when I upload a .js file including just:

    Code (JavaScript):
    1. UnityLoader.Error.handler = function(e,t)
    2. {
    3.     console.log(e);
    4. }
    And manually reference this script when I instantiate the gameinstance, it works fine and overrides the Error Handler as intended. Seems to be an issue with the mergeInto() portion... time to dig further into that.
     
  9. De-Panther

    De-Panther

    Joined:
    Dec 27, 2009
    Posts:
    589
    .jslib is for cases when you want to have reference from C# to JS using DLL import.
    In your case, you can use .jspre file, there you can write JS as usual...
    You can also have a reference from .jslib file to a method in a .jspre file.
     
    xeniaeo likes this.
  10. StevenT-Han

    StevenT-Han

    Joined:
    Oct 10, 2020
    Posts:
    2
    Hello forum, I'm new to Unity WebGL. I've been searching to replace Javascript error popup like "An error occurred running the Unity content on this page. See your java script console for more info", I know this is getting by object reference since I removed ref in instantiate. So, in Unity 2021.3.11f1, there is no UnityLoader since there is only unityInstance in createUnityInstance() in index.html. Please help me to the right direction. Sorry to post this on old thread.
     

    Attached Files:

  11. StevenT-Han

    StevenT-Han

    Joined:
    Oct 10, 2020
    Posts:
    2
    Just found out in loader.js, an "alert(e)" which is generating the error messages.