Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions src/js/thirdparty/node-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,25 +197,44 @@ async function fetch(
return response;
}

class AbortError extends DOMException {
constructor(message) {
super(message, "AbortError");
}
}

// node-fetch names each error after its class and keeps the system error's code and syscall:
// https://github.com/node-fetch/node-fetch/blob/8b3320d2a7c07bce4afc6b2bf6c3bbddda85b01f/src/errors/base.js
// https://github.com/node-fetch/node-fetch/blob/8b3320d2a7c07bce4afc6b2bf6c3bbddda85b01f/src/errors/fetch-error.js
class FetchBaseError extends Error {
type: string;

constructor(message, type) {
super(message);
this.type = type;
}

get name() {
return this.constructor.name;
}

get [Symbol.toStringTag]() {
return this.constructor.name;
}
}

class FetchError extends FetchBaseError {
declare code?: string;
declare errno?: string;
declare erroredSysCall?: string;

constructor(message, type, systemError) {
super(message, type);
this.code = systemError?.code;
if (systemError) {
this.code = this.errno = systemError.code;
this.erroredSysCall = systemError.syscall;
}
}
}

// https://github.com/node-fetch/node-fetch/blob/8b3320d2a7c07bce4afc6b2bf6c3bbddda85b01f/src/errors/abort-error.js
class AbortError extends FetchBaseError {
constructor(message, type = "aborted") {
super(message, type);
}
}

Expand Down
26 changes: 25 additions & 1 deletion test/js/node/http/node-fetch.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as vercelFetch from "@vercel/fetch";
import * as iso from "isomorphic-fetch";
import fetch2, { fetch, Headers, Request, Response } from "node-fetch";
import fetch2, { AbortError, fetch, FetchError, Headers, Request, Response } from "node-fetch";
import { once } from "node:events";
import http from "node:http";
import * as stream from "stream";
Expand Down Expand Up @@ -417,6 +417,30 @@ test("node-fetch json() resolves null for a body that is the JSON text null", as
expect(await (await fetch2(server.url)).json()).toBeNull();
});

test("node-fetch FetchError and AbortError match node-fetch's error classes", () => {
const error = new FetchError("request to http://example.test/ failed", "system", {
code: "ECONNRESET",
syscall: "read",
});
expect(error).toBeInstanceOf(Error);
expect(error.name).toBe("FetchError");
expect(String(error)).toBe("FetchError: request to http://example.test/ failed");
expect(Object.prototype.toString.call(error)).toBe("[object FetchError]");
expect(error.type).toBe("system");
expect(error.code).toBe("ECONNRESET");
expect(error.errno).toBe("ECONNRESET");
expect(error.erroredSysCall).toBe("read");

const plain = new FetchError("invalid json", "invalid-json");
expect(plain.name).toBe("FetchError");
expect("code" in plain).toBe(false);

const aborted = new AbortError("The operation was aborted.");
expect(aborted).toBeInstanceOf(Error);
expect(aborted.name).toBe("AbortError");
expect(aborted.type).toBe("aborted");
});

test("node-fetch request body streams properly", async () => {
let responseResolve;
const responsePromise = new Promise(resolve => {
Expand Down