Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question I want to disable all alert errors for webgl

Discussion in 'Web' started by dansav, Oct 22, 2023.

  1. dansav

    dansav

    Joined:
    Sep 22, 2005
    Posts:
    510
    I want to disable the entire alert system that shows up with:
    An error occurred running the Unity content on this page.

    I'm using unity 2022.

    I tried disabling the alert in a unityloader.js file I found in the webgl build folder but that didn't work.
    I also tried this but it didn't work either. I cleared my cache in my testing also.
    Code (CSharp):
    1. UnityLoader.Error.handler = function(e,t){console.log(e);}
    I'm not sure where it's even coming from in 2022.
    Can someone tell me where the alert is coming from so I can change it to console.log?
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,013
    The proper way to do that is to locate and fix these errors. ;)
     
  3. snipshotmedia

    snipshotmedia

    Joined:
    Nov 13, 2023
    Posts:
    9
    Nice one...
     
  4. Hellfim

    Hellfim

    Joined:
    Sep 11, 2014
    Posts:
    123
    While I strongly suggest to follow CodeSmile's advise on locating and fixing errors, there might be some scenerios when it's impossible.
    For example, in my case if I abort WebRequest I recieve a noisy popup with the exception on Safari (MacOS) on Unity 2022.2.16f1. The exception comes from the inside of Unity, is never handled. I'm also unable to catch it anywhere. It's also important to note, that the exception is actually harmless and doesn't affect gameplay in any way with the exception of generating noisy popup.

    So I've written following code which overrides default alert of javascript to suppress popup, but only for my exception. I still want to receive popups with alerts for other types of exceptions. The code itself may be placed anywhere where JavaScript code belongs, for example right above createUnityInstance method.

    Code (csharp):
    1.  
    2. let builtInAlert = alert;
    3. alert = function(message) {
    4.     if (message != null && typeof(message) == "string") {
    5.         if (message.indexOf("AbortError: Fetch is aborted") != -1) {
    6.             console.log("Suppressed exception: \"AbortError: Fetch is aborted\"");
    7.             return;
    8.         }
    9.     }
    10.     builtInAlert(message);
    11. }
    12.  
    If you don't want to receive any exceptions as popups code becomes even simplier, but I advise you not to use it as it leaves you without a good tool to discover exceptions.

    Code (csharp):
    1.  
    2. alert = function(message) {
    3.     console.log(message);
    4. }
    5.  
     
    Last edited: Feb 6, 2024