Hide Javascript error message from browser

12,422

Solution 1

When the function returns true, this prevents the firing of the default event handler.

It's just the way the browser behaves if this function returns true.

See more

Solution 2

use: try...catch

function message()
{
    try
    {
        adddlert("Welcome guest!");
    }
    catch (err)
    {
        txt = "There was an error on this page.\n\n";
        txt += "Error description: " + err.description + "\n\n";
        txt += "Click OK to continue.\n\n";
        alert(txt);
    }
}

more: https://developer.mozilla.org/en/JavaScript/Reference/Statements/try...catch

Solution 3

Adding window.onerror function to the head section and returning true would stop logging the errors events to the browser console.

As @Decko mentioned returning true prevents the firing of the default event handler.

Stackblitz - https://stackblitz.com/edit/web-platform-mfattr?file=index.html

Share:
12,422
tugberk
Author by

tugberk

Senior Software Engineer and Tech Lead, with a growth mindset belief and 10+ years of practical software engineering experience including technical leadership and distributed systems. I have a passion to create impactful software products, and I care about usability, reliability, observability and scalability of the software systems that I work on, as much as caring about day-to-day effectiveness, productivity and happiness of the team that I work with. I occasionally speak at international conferences (tugberkugurlu.com/speaking), and write technical posts on my blog (tugberkugurlu.com). I currently work at Facebook as a Software Engineer. I used to work at Deliveroo as a Staff Software Engineer in the Consumer division, working on distributed backend systems which have high throughput, low latency and high availability needs. Before that, I used to work at Redgate as a Technical Lead for 4 years, where I led and line-managed a team of 5 Software Engineers. I was responsible for all aspects of the products delivered by the team from technical architecture to product direction. I was also a Microsoft MVP for 7 years between 2012-2019 on Microsoft development technologies.

Updated on June 07, 2022

Comments

  • tugberk
    tugberk almost 2 years

    I found this code sample on one of the questions;

    window.onerror = function (msg, url, linenumber) {  
    
     //make ajax call with all error details and log error directly into the elmah database
    
     //show friendly error msg here to user
    
     //hide error from browser
     return true; 
    }
    

    What does 'hide error from browser' means and how can I do that?

    I didn't know such a thing exists and I give it a try with try-catch block. After an error occurred, I realized that browser still shows the error. How can I hide error from browser?

    UPDATE

    I tried following code and put that inside the head section of my all pages but noting happened;

    <script type="text/javascript">
    
    (function () {
    
        window.onerror = function myErrorHandler(errorMsg, url, lineNumber) {
    
            alert(errorMsg);
    
            // Just let default handler run.
            return true;
        }
    
    })();
    
    </script>