Connexion

catch all js error in the browser

20/03/2023
<script>
    async function fetchPost(url, params) {
        const rawResponse = await fetch(url, {
            method: 'POST',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(params)
        });

        return await rawResponse.json();
    }

    window.onerror = function(msg, url, lineNo, columnNo, error) {
        (async function() {
            try {
                await fetchPost('https://httptest.raphaelpiccolo.com/error', {
                    error: error.stack,
                    html: document.documentElement.outerHTML,
                });
            }
            catch(err) {
                console.log("cant send error to monitoring " + err.message);
            }
        })()

        // return false makes the error show in the console
        return false;
    }
</script>

<h1>salut</h1>
<button onclick="zob()">click me</button>