Skip to content

Commit 28b857c

Browse files
authored
adopt curl -f to make the data loader fail if curl fails to download (#1667)
1 parent 312f4a9 commit 28b857c

File tree

8 files changed

+10
-10
lines changed

8 files changed

+10
-10
lines changed

docs/data-loaders.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Data loaders are polyglot: they can be written in any programming language. They
1616
A data loader can be as simple as a shell script that invokes [curl](https://curl.se/) to fetch recent earthquakes from the [USGS](https://earthquake.usgs.gov/earthquakes/feed/v1.0/geojson.php):
1717

1818
```sh
19-
curl https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson
19+
curl -f https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson
2020
```
2121

2222
Data loaders use [file-based routing](#routing), so assuming this shell script is named `quakes.json.sh`, a `quakes.json` file is then generated at build time. You can access this file from the client using [`FileAttachment`](./files):
@@ -230,7 +230,7 @@ If multiple requests are made concurrently for the same data loader, the data lo
230230
231231
## Output
232232
233-
Data loaders must output to [standard output](<https://en.wikipedia.org/wiki/Standard_streams#Standard_output_(stdout)>). The first extension (such as `.csv`) does not affect the generated snapshot; the data loader is solely responsible for producing the expected output (such as CSV). If you wish to log additional information from within a data loader, be sure to log to standard error, say by using [`console.warn`](https://developer.mozilla.org/en-US/docs/Web/API/console/warn) or `process.stderr`; otherwise the logs will be included in the output file and sent to the client.
233+
Data loaders must output to [standard output](<https://en.wikipedia.org/wiki/Standard_streams#Standard_output_(stdout)>). The first extension (such as `.csv`) does not affect the generated snapshot; the data loader is solely responsible for producing the expected output (such as CSV). If you wish to log additional information from within a data loader, be sure to log to standard error, say by using [`console.warn`](https://developer.mozilla.org/en-US/docs/Web/API/console/warn) or `process.stderr`; otherwise the logs will be included in the output file and sent to the client. If you use `curl` as above, we recommend the `-f` flag (equivalently, the `--fail` option) to make the data loader return an error when the download fails.
234234
235235
## Building
236236
@@ -247,7 +247,7 @@ Data loaders generate files at build time that live alongside other [static file
247247
Where `quakes.json.sh` is:
248248
249249
```sh
250-
curl https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson
250+
curl -f https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson
251251
```
252252
253253
This will produce the following output root:

docs/data/dft-road-collisions.csv.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ TMPDIR="docs/.observablehq/cache/"
55

66
# Download the data (if it’s not already in the cache).
77
if [ ! -f "$TMPDIR/dft-collisions.csv" ]; then
8-
curl "$URL" -o "$TMPDIR/dft-collisions.csv"
8+
curl -f "$URL" -o "$TMPDIR/dft-collisions.csv"
99
fi
1010

1111
# Generate a CSV file using DuckDB.

docs/quakes.json.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
curl https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson
1+
curl -f https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
curl 'https://www.eia.gov/electricity/930-api//respondents/data?type\[0\]=BA&type\[1\]=BR' \
1+
curl -f 'https://www.eia.gov/electricity/930-api//respondents/data?type\[0\]=BA&type\[1\]=BR' \
22
-H 'Connection: keep-alive' \
33
-A 'Chrome/123.0.0.0' \
44
--compressed

examples/loader-census/src/data/ca.json.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Download the ZIP archive from the Census Bureau (if needed).
22
if [ ! -f src/.observablehq/cache/cb_2023_06_cousub_500k.zip ]; then
3-
curl -o src/.observablehq/cache/cb_2023_06_cousub_500k.zip 'https://www2.census.gov/geo/tiger/GENZ2023/shp/cb_2023_06_cousub_500k.zip'
3+
curl -f -o src/.observablehq/cache/cb_2023_06_cousub_500k.zip 'https://www2.census.gov/geo/tiger/GENZ2023/shp/cb_2023_06_cousub_500k.zip'
44
fi
55

66
# Unzip the ZIP archive to extract the shapefile.

examples/loader-census/src/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Next, here’s a bash script, `ca.json.sh`:
1313
```bash
1414
# Download the ZIP archive from the Census Bureau (if needed).
1515
if [ ! -f src/.observablehq/cache/cb_2023_06_cousub_500k.zip ]; then
16-
curl -o src/.observablehq/cache/cb_2023_06_cousub_500k.zip 'https://www2.census.gov/geo/tiger/GENZ2023/shp/cb_2023_06_cousub_500k.zip'
16+
curl -f -o src/.observablehq/cache/cb_2023_06_cousub_500k.zip 'https://www2.census.gov/geo/tiger/GENZ2023/shp/cb_2023_06_cousub_500k.zip'
1717
fi
1818

1919
# Unzip the ZIP archive to extract the shapefile.

examples/loader-duckdb/src/educ_uoe_lang01.parquet.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ TMPDIR="src/.observablehq/cache/"
66

77
# Download the data (if it’s not already in the cache).
88
if [ ! -f "$TMPDIR/$CODE.csv" ]; then
9-
curl "$URL" -o "$TMPDIR/$CODE.csv"
9+
curl -f "$URL" -o "$TMPDIR/$CODE.csv"
1010
fi
1111

1212
# Generate a Parquet file using DuckDB.

examples/loader-duckdb/src/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ TMPDIR="src/.observablehq/cache/"
1111

1212
# Download the data (if it’s not already in the cache).
1313
if [ ! -f "$TMPDIR/$CODE.csv" ]; then
14-
curl "$URL" -o "$TMPDIR/$CODE.csv"
14+
curl -f "$URL" -o "$TMPDIR/$CODE.csv"
1515
fi
1616

1717
# Generate a Parquet file using DuckDB.

0 commit comments

Comments
 (0)