Skip to content

Commit

Permalink
chore: improve FetchError message
Browse files Browse the repository at this point in the history
  • Loading branch information
MellKam committed Jul 11, 2023
1 parent d5c71aa commit 40ca062
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 31 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "yumi-fetch",
"version": "0.2.7",
"version": "0.2.8",
"description": "🍭 An extensible fetch wrapper for simplified and powerful HTTP requests using native web APIs",
"main": "dist/index.cjs",
"module": "dist/index.js",
Expand Down
79 changes: 52 additions & 27 deletions src/http_error.test.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,97 @@
import { createFetchError, isHTTPError } from "./http_error.ts";
import { createFetchError, FetchError, isHTTPError } from "./http_error.ts";
import { assert, assertEquals } from "std/testing/asserts.ts";

Deno.test("FetchError", async () => {
const req = new Request("http://example.com");
Deno.test("createFetchError - with body as JSON", async () => {
const req = new Request("http://example.com/");
const res = new Response(JSON.stringify({ foo: "bar" }), {
status: 500,
statusText: "Internal Server Error",
headers: {
"Content-Type": "application/json",
},
});

const error = await createFetchError(req, res);

assertEquals(error.body, { foo: "bar" });
assert(error.status === 500);
assert(error.url === res.url);
assertEquals(error.status, 500);
assertEquals(error.url, res.url);
assertEquals(error.request, req);
assertEquals(error.response, res);
assertEquals(
error.message,
"500 Internal Server Error (http://example.com/)",
);
});

Deno.test("FetchError - without body", async () => {
const req = new Request("http://example.com");
const res = new Response(null, { status: 400 });
Deno.test("createFetchError - without body", async () => {
const req = new Request("http://example.com/");
const res = new Response(null, { status: 400, statusText: "Bad Request" });

const error = await createFetchError(req, res);

assert(error.body === null);
assert(error.message === "Unknown error");
assert(error.status === 400);
assertEquals(error.body, null);
assertEquals(error.message, "400 Bad Request (http://example.com/)");
assertEquals(error.status, 400);
assertEquals(error.request, req);
assertEquals(error.response, res);
});

Deno.test("FetchError - with body as string", async () => {
const req = new Request("http://example.com");
Deno.test("createFetchError - with body as string", async () => {
const req = new Request("http://example.com/");
const res = new Response("Bad request", { status: 400 });

const error = await createFetchError(req, res);

assert(error.message === "Bad request");
assert(error.body === "Bad request");
assertEquals(error.message, "400 (http://example.com/)");
assertEquals(error.body, "Bad request");
});

Deno.test("FetchError - with invalid json", async () => {
const req = new Request("http://example.com");
Deno.test("createFetchError - with invalid JSON", async () => {
const req = new Request("http://example.com/");
const res = new Response("{ abc: undefined }", {
status: 400,
statusText: "Bad Request",
headers: { "Content-Type": "application/json" },
});

const error = await createFetchError(req, res);

assert(error.message === "{ abc: undefined }");
assert(error.body === "{ abc: undefined }");
assertEquals(error.message, "400 Bad Request (http://example.com/)");
assertEquals(error.body, "{ abc: undefined }");
});

Deno.test("FetchError - with statusText message", async () => {
const req = new Request("http://example.com");
Deno.test("createFetchError - with statusText message", async () => {
const req = new Request("http://example.com/");
const res = new Response(undefined, {
status: 400,
statusText: "Bad request",
});

const error = await createFetchError(req, res);

assert(error.message === "Bad request");
assert(error.body === null);
assertEquals(error.message, "400 Bad request (http://example.com/)");
assertEquals(error.body, null);
});

Deno.test("isHTTPError", async () => {
const req = new Request("http://example.com");
Deno.test("isHTTPError", () => {
const req = new Request("http://example.com/");
const res = new Response(null, { status: 400 });

assert(isHTTPError(await createFetchError(req, res)));
assert(isHTTPError(new Error("Not a HTTPError")) === false);
const fetchError = new FetchError("Test error", req, res, null);
const regularError = new Error("Not a HTTPError");

assert(isHTTPError(fetchError));
assert(!isHTTPError(regularError));
});

Deno.test("isHTTPError", () => {
const req = new Request("http://example.com/");
const res = new Response(null, { status: 400 });

const fetchError = new FetchError("Test error", req, res, null);
const regularError = new Error("Not a HTTPError");

assert(isHTTPError(fetchError));
assert(!isHTTPError(regularError));
});
6 changes: 3 additions & 3 deletions src/http_error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ export const createFetchError = async <T_Body = unknown>(
response: Response,
options?: ErrorOptions,
) => {
let message = response.statusText || "Unknown error";
const message = response.statusText
? `${response.status} ${response.statusText} (${request.url})`
: `${response.status} (${request.url})`;
let body: T_Body = null as T_Body;

if (!response.body || response.type === "opaque") {
Expand All @@ -80,8 +82,6 @@ export const createFetchError = async <T_Body = unknown>(

try {
body = await response.text() as T_Body;
message = body as string;

const contentType = response.headers.get(CONTENT_TYPE_HEADER);

if (
Expand Down

0 comments on commit 40ca062

Please sign in to comment.