🪨 A well tested suite of tools designed to help FT.com applications be more reliable and measurable
Installations
npm install @dotcom-reliability-kit/fetch-error-handler
Developer Guide
Typescript
Yes
Module System
CommonJS
Min. Node Version
20.x || 22.x
Node Version
22.13.1
NPM Version
10.9.2
Score
71.2
Supply Chain
99.3
Quality
88.6
Maintenance
100
Vulnerability
100
License
Releases
opentelemetry: v3.0.2
Updated on Mar 05, 2025
opentelemetry: v3.0.1
Updated on Feb 19, 2025
middleware-allow-request-methods: v1.0.0
Updated on Feb 12, 2025
fetch-error-handler: v1.0.0
Updated on Jan 30, 2025
opentelemetry: v3.0.0
Updated on Jan 21, 2025
serialize-request: v4.0.0
Updated on Jan 21, 2025
Contributors
Languages
JavaScript (98.32%)
TypeScript (1.2%)
CSS (0.34%)
Shell (0.15%)
validate.email 🚀
Verify real, reachable, and deliverable emails with instant MX records, SMTP checks, and disposable email detection.
Developer
Financial-Times
Download Statistics
Total Downloads
7,261
Last Day
9
Last Week
110
Last Month
517
Last Year
6,013
GitHub Statistics
MIT License
8 Stars
1,106 Commits
1 Forks
50 Watchers
7 Branches
33 Contributors
Updated on Mar 13, 2025
Bundle Size
4.89 kB
Minified
1.74 kB
Minified + Gzipped
Package Meta Information
Latest Version
1.0.0
Package Id
@dotcom-reliability-kit/fetch-error-handler@1.0.0
Unpacked Size
18.65 kB
Size
5.68 kB
File Count
5
NPM Version
10.9.2
Node Version
22.13.1
Published on
Jan 30, 2025
Total Downloads
Cumulative downloads
Total Downloads
7,261
Last Day
-57.1%
9
Compared to previous day
Last Week
-6%
110
Compared to previous week
Last Month
-33.9%
517
Compared to previous month
Last Year
381.8%
6,013
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Dev Dependencies
5
@dotcom-reliability-kit/fetch-error-handler
Properly handle fetch errors and avoid a lot of boilerplate in your app. This module is part of FT.com Reliability Kit.
Usage
Install @dotcom-reliability-kit/fetch-error-handler
as a dependency:
1npm install --save @dotcom-reliability-kit/fetch-error-handler
Include in your code:
1import { handleFetchErrors } from '@dotcom-reliability-kit/fetch-error-handler'; 2// or 3const { handleFetchErrors } = require('@dotcom-reliability-kit/fetch-error-handler');
You can use this function with any fetch
call to throw appropriate errors based on the HTTP status code that you get back.
There are several ways to use it, as long as it is await
ed and is called with either a Response
object or a Promise
that resolves to a Response
.
Some of the options below result in more errors being caught, you can weigh this up when implementing in your own code.
In all of the APIs below, if the response ok
property is false
, i.e. when the status code is 400
or greater, then errors will be thrown.
[!WARNING] If you're using node-fetch then it's important to read the body of the request because of a known memory leak. If an error is thrown then we automatically drain the response body stream but, if the request is successful, you'll need to do this yourself.
Wrap the fetch function
This is the recommended API as this will allow you to handle the most errors (even DNS and timeout errors) correctly:
1const response = await handleFetchErrors(
2 fetch('https://httpbin.org/status/500')
3);
You must not await
the fetch
call itself if you want to handle DNS and timeout errors. This is safe to do and will not result in unhandled promise rejections – handleFetchErrors
takes care of them all.
Handle errors with .then
This API allows you to handle most errors based on the HTTP response, but it will not allow you to handle errors which occur before a valid response is returned, e.g. DNS or timeout errors.
1const response = await fetch('https://httpbin.org/status/500').then(handleFetchErrors);
Handle the response object
This API is for when you already have an HTTP response object, but it will not allow you to handle errors which occur before a valid response is returned, e.g. DNS or timeout errors.
1const response = await fetch('https://httpbin.org/status/500'); 2await handleFetchErrors(response);
Errors thrown
We throw different errors depending on the status code we get back from the fetch
call.
Client errors
If the URL you fetched responds with a status code in the range of 400–499
then this normally indicates that something is wrong with the current system. Maybe we're sending data in an invalid format or our API key is invalid. For this we throw a generic 500
error to indicate an issue with our system. It'll be an HTTPError
. This error will have the following properties to help you debug:
1error.statusCode // 500 2error.code // FETCH_CLIENT_ERROR 3error.data.upstreamUrl // The URL that was fetched 4error.data.upstreamStatusCode // The status code that the URL responded with
Server errors
If the URL you fetched responds with a status code in the range of 500–599
then this indicates something is wrong with the upstream system. For this we can output an UpstreamServiceError
and attribute it to the system we're fetching from. This error will have the following properties to help you debug:
1error.statusCode // 502 2error.code // FETCH_SERVER_ERROR 3error.data.upstreamUrl // The URL that was fetched 4error.data.upstreamStatusCode // The status code that the URL responded with
DNS errors
If the hostname of the URL you fetched cannot be resolved, a DNS error will be thrown, it'll be an OperationalError
. This error will have the following properties to help you debug:
1error.code // FETCH_DNS_LOOKUP_ERROR 2error.cause // The underlying DNS error that was caught
[!IMPORTANT]
This type of error will only be thrown if you use the "wrap the fetch function" API.
Abort and timeout errors
If the request times out or is aborted via AbortSignal
, or the non-standard timeout
option in node-fetch is used, then we throw an OperationalError
. This error will have the following properties to help you debug:
1error.code // FETCH_ABORT_ERROR or FETCH_TIMEOUT_ERROR 2error.cause // The underlying abort or timeout error that was caught
[!IMPORTANT]
This type of error will only be thrown if you use the "wrap the fetch function" API.
Socket errors
If the connection is closed early by the server then we throw an UpstreamServiceError
. This error will have the following properties to help you debug:
1error.code // FETCH_SOCKET_HANGUP_ERROR 2error.cause // The underlying socket error that was caught
[!IMPORTANT]
This type of error will only be thrown if you use the "wrap the fetch function" API.
Unknown errors
If the URL you fetched responds with an ok
property of false
and a status code outside of the 400–599
range, then it's unclear what's happened but we reject with an error anyway to make sure we're able to debug. We output an HTTPError
:
1error.statusCode // 500 2error.code // FETCH_UNKNOWN_ERROR 3error.data.upstreamUrl // The URL that was fetched 4error.data.upstreamStatusCode // The status code that the URL responded with
Creating your own handler
You can customise the thrown errors by creating your own fetch handler and passing in options.
Include in your code:
1import { createFetchErrorHandler } from '@dotcom-reliability-kit/fetch-error-handler'; 2// or 3const { createFetchErrorHandler } = require('@dotcom-reliability-kit/fetch-error-handler');
Create and use your own handler (the handler supports all the same usage methods as outlined here):
1const handleFetchErrors = createFetchErrorHandler({
2 upstreamSystemCode: 'httpbin'
3});
4const response = await handleFetchErrors(fetch('https://httpbin.org/status/500'));
createFetchErrorHandler
configuration options
Config options can be passed into the createFetchErrorHandler
function to change the behaviour of the handler.
options.upstreamSystemCode
Attribute any fetch errors to a given Biz Ops system. This allows you to easily spot in your logs when an upstream system is the cause of an error. This must be a String
and a valid system code.
1const handleFetchErrors = createFetchErrorHandler({
2 upstreamSystemCode: 'next-navigation-api'
3});
When this is set, any errors thrown by handleFetchErrors
will have a relatesToSystems
property which includes the given system code.
Contributing
See the central contributing guide for Reliability Kit.
License
Licensed under the MIT license.
Copyright © 2023, The Financial Times Ltd.

No vulnerabilities found.

No security vulnerabilities found.