Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    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:
    6,659
    The proper way to do that is to locate and fix these errors. ;)
     
  3. snipshotmedia

    snipshotmedia

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

    Hellfim

    Joined:
    Sep 11, 2014
    Posts:
    124
    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
  5. zeinhatduong

    zeinhatduong

    Joined:
    Sep 18, 2021
    Posts:
    17
    Sorry, I'm not used to javascript so where can I apply this code? Is there a file in the depository or a script hidden somewhere?
     
  6. Hellfim

    Hellfim

    Joined:
    Sep 11, 2014
    Posts:
    124