Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch/report browser errors (both JavaScript and network) #315

Open
docuracy opened this issue Jul 23, 2024 · 0 comments
Open

Catch/report browser errors (both JavaScript and network) #315

docuracy opened this issue Jul 23, 2024 · 0 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@docuracy
Copy link
Member

docuracy commented Jul 23, 2024

Report all JavaScript errors to a new server API (POST with csrf).

Something like this, perhaps, but with modal alerts:

// service-worker.js
self.addEventListener('fetch', event => {
    const url = new URL(event.request.url);
    if (url.hostname === 'whgazetteer.example.com') {
        event.respondWith(
            fetch(event.request).then(response => {
                if (!response.ok) {
                    handleError(response);
                }
                return response;
            }).catch(error => {
                console.error('Fetch failed:', error);
                throw error;
            })
        );
    } else {
        // Allow all other requests to proceed without custom handling
        return;
    }
});

function handleError(response) {
    switch (response.status) {
        case 404:
            console.error('Error 404: Resource not found -', response.url);
            break;
        case 500:
            console.error('Error 500: Internal server error -', response.url);
            break;
        default:
            console.error('Error', response.status, 'on', response.url);
    }
}

and:

if ('serviceWorker' in navigator) {
    navigator.serviceWorker.register('/service-worker.js')
    .then(function(registration) {
        console.log('ServiceWorker registration successful with scope: ', registration.scope);
    }, function(error) {
        console.log('ServiceWorker registration failed: ', error);
    });
}

This should catch errors on all network requests, including those initiated by jQuery's $.ajax, $.get, $.post, and also Fetch API calls.

@docuracy docuracy added enhancement New feature or request high priority can break key site elements SG#08 labels Jul 23, 2024
@docuracy docuracy added this to the v3 beta+ milestone Jul 23, 2024
@docuracy docuracy self-assigned this Jul 23, 2024
@docuracy docuracy changed the title Catch Ajax errors Catch network errors Jul 23, 2024
@docuracy docuracy changed the title Catch network errors Catch browser network errors Jul 23, 2024
@docuracy docuracy changed the title Catch browser network errors Catch/report browser errors (both JavaScript and network) Jul 24, 2024
@docuracy docuracy removed the high priority can break key site elements label Jul 30, 2024
@docuracy docuracy removed the SG#08 label Oct 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant