Console methods

Whenever I create a fetch event inside a service worker, my code roughly follows the same pattern. There’s a then clause which gets executed if the fetch is successful, and a catch clause in case anything goes wrong:

fetch( request)
.then( fetchResponse => {
    // Yay! It worked.
})
.catch( fetchError => {
    // Boo! It failed.
});

In my book—Going Offline—I’m at pains to point out that those arguments being passed into each clause are yours to name. In this example I’ve called them fetchResponse and fetchError but you can call them anything you want.

I always do something with the fetchResponse inside the then clause—either I want to return the response or put it in a cache.

But I rarely do anything with fetchError. Because of that, I’ve sometimes made the mistake of leaving it out completely:

fetch( request)
.then( fetchResponse => {
    // Yay! It worked.
})
.catch( () => {
    // Boo! It failed.
});

Don’t do that. I think there’s some talk of making the error argument optional, but for now, some browsers will get upset if it’s not there.

So always include that argument, whether you call it fetchError or anything else. And seeing as it’s an error, this might be a legitimate case for outputing it to the browser’s console, even in production code.

And yes, you can output to the console from a service worker. Even though a service worker can’t access anything relating to the document object, you can still make use of window.console, known to its friends as console for short.

My muscle memory when it comes to sending something to the console is to use console.log:

fetch( request)
.then( fetchResponse => {
    return fetchResponse;
})
.catch( fetchError => {
    console.log(fetchError);
});

But in this case, the console.error method is more appropriate:

fetch( request)
.then( fetchResponse => {
    return fetchResponse;
})
.catch( fetchError => {
    console.error(fetchError);
});

Now when there’s a connectivity problem, anyone with a console window open will see the error displayed bold and red.

If that seems a bit strident to you, there’s always console.warn which will still make the output stand out, but without being quite so alarmist:

fetch( request)
.then( fetchResponse => {
    return fetchResponse;
})
.catch( fetchError => {
    console.warn(fetchError);
});

That said, in this case, console.error feels like the right choice. After all, it is technically an error.

Have you published a response to this? :

Responses

Šime Vidas

adactio.com/journal/14241 says that in some browsers fetch() fails silently if the catch() handler has no arguments, e.g., fetch( ‘/foo’ ).then(res => { }).catch(() => { }); but Chrome and Firefox still call the catch handler normally. In which browsers does it fail silently?

2 Likes

# Liked by Gunnar Bittersmann on Tuesday, August 7th, 2018 at 4:40pm

# Liked by Eric Rivas on Thursday, September 6th, 2018 at 6:40pm

Previously on this day

6 years ago I wrote August in America, day four

Alexandria, Virginia.

15 years ago I wrote Lust

This collection of 1920s erotic photos features one flapper with a mandolin and another with a bouzouki.

15 years ago I wrote Anger

If you’d like to make a difference in the ever-worsening situation in Darfur, please, please, please make a donation (via the secure WorldPay service) to the World Food Program. I’ve made a modest contribution which, by itself, won’t amo

15 years ago I wrote Envy

File this under "Now, why didn’t I think of that?".

15 years ago I wrote Greed

I finally caved in and succumbed to the temptation of owning the style gadget de-siècle. I’ve ordered an iPod.

15 years ago I wrote Gluttony

Richard points out that Jamie Oliver’s website is now written in XHTML and CSS.

15 years ago I wrote Sloth

When I really should be working, the last thing I need to read is this excellent article by Tom Hodgkinson in The Guardian entitled The Virtue Of Idleness, taken from his forthcoming book How To Be Idle:

15 years ago I wrote Pride

It’s that time of year again. Brighton is party-central this weekend. Brighton Pride is an annual event celebrating the town—, sorry, city’s gay and lesbian community. It’s Fun with a capital F and it’s make me proud to live

16 years ago I wrote Wireless Mall

I’ve just made a nifty little discovery: the mall in Sierra Vista has Wi-Fi.

16 years ago I wrote Budd Blog

Brighton web designer, pal and all-round good guy Andy Budd has joined the blogosphere.

17 years ago I wrote Plustech Walking Technology

That is one cool looking machine. Eco friendly, too.

17 years ago I wrote Why I Hate Star Wars

"I thought it would be funny to go dressed up as Spock".