Gracefully handle 'Callback failed: the server did not respond.'

Hi All,

I’m using dash in a scenario where, on occasion, the internet drops out (for a matter of 5-10 seconds) and a callback may fail.

Is there any way to gracefully catch these callback failures, and inform the user? A typical use case is a button call back doing an action, and the user needs to know if that button action failed, and they can respond accordingly (e.g. a ‘save’ failed, and they need to click save again).

Hello @Inci,

Welcome to the community!

You can do this on the clientside via a javascript listener to the fetch events.

window.fetch = new Proxy(window.fetch, {
    apply(fetch, that, args) {
        // Forward function call to the original fetch
        const result = fetch.apply(that, args);

        // Do whatever you want with the resulting Promise
        result.then((response) => {
            if (args[0] == '/_dash-update-component' && !['2','3'].includes(response.status.toString()[0])) {
                console.log(response.status)
         }
         });

        return result;
    }
});

This will log to the console any time a status comes through other than 2 or 3 when looking for updates. Just change the console.log to be something where you want to display a notification to the user.

Be it an alert that just shows up like dbc.Alert, etc.

3 Likes

Ah legend, thanks! Client-side callbacks are just a godsend for doing a whole host of things.

1 Like

Now, this isn’t a clientside callback, but placed into a JavaScript file under the assets folder.

You could use it to trigger a clientside or server side callback with a hidden button that displays the alert.