Skip to content

Instantly share code, notes, and snippets.

View guysbryant's full-sized avatar

Guy Bryant guysbryant

View GitHub Profile
@guysbryant
guysbryant / error-handling-with-fetch.md
Created May 14, 2021 03:17 — forked from odewahn/error-handling-with-fetch.md
Processing errors with Fetch API

I really liked @tjvantoll article Handling Failed HTTP Responses With fetch(). The one thing I found annoying with it, though, is that response.statusText always returns the generic error message associated with the error code. Most APIs, however, will generally return some kind of useful, more human friendly message in the body.

Here's a modification that will capture this message. The key is that rather than throwing an error, you just throw the response and then process it in the catch block to extract the message in the body:

fetch("/api/foo")
  .then( response => {
    if (!response.ok) { throw response }
    return response.json()  //we only get here if there is no error
 })

[admonition] (https://github.com/libeanim/inkdrop-admonition) Add 10 types of block styled side content You can use any markdown content inside of any admonition, including admonition blocks

  1. note [[note | Note]] | ctrl alt a | adds a note admonition block
@guysbryant
guysbryant / .crostini-setup
Created January 9, 2019 20:43 — forked from tjpalanca/.crostini-setup
Crostini Setup
These scripts set up Crostini on my Pixelbook