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

Related posts

Workaround

Browsers and bugs.

Bugblogging

Also, tipblogging.

Jake Archibald is speaking at Web Day Out

The sixth speaker is revealed—only two more to go!

Speculation rules and fears

Browser are user agents, not developer agents.

Trust

I’m trying to understand why developers would trust third-party code more than a native browser feature.

Related links

Bugs I’ve filed on browsers | Read the Tea Leaves

I think filing bugs on browsers is one of the most useful things a web developer can do.

Agreed!

Tagged with

In a Land Before Dev Tools | Amber’s Website

A great little history lesson from Amber—ah, Firebug!

Tagged with

A debugging thought process

Remy walks us through his performance debugging routine …and now Una must write him a song.

Tagged with

The Art of Debugging

I was just helping out with some debugging at work and it reminded me of this great talk/post by Remy:

  1. Replicate: see the bug
  2. Isolate: understand the bug
  3. Eliminate: fix the bug

Tagged with

Taking Chrome DevTools outside of the browser. — Kenneth Auchenberg

Kenneth has isolated Chrome’s dev tools into its own app. This is a big step towards this goal:

Why are DevTools still bundled with the browsers? What if clicking “inspect element” simply started an external DevTools app?

With DevTools separated from one specific browser, a natural next step would be making the DevTools app work with other browsers.

Tagged with

Previously on this day

13 years ago I wrote August in America, day four

Alexandria, Virginia.

22 years ago I wrote Lust

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

22 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

22 years ago I wrote Envy

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

22 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.

22 years ago I wrote Gluttony

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

22 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:

22 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

23 years ago I wrote Wireless Mall

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

23 years ago I wrote Budd Blog

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

24 years ago I wrote Plustech Walking Technology

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

24 years ago I wrote Why I Hate Star Wars

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