Skip to content

Commit

Permalink
fix: hide getters from console and pass cause
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Aug 23, 2023
1 parent 1258d51 commit 905244a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Parsed error body is available with `error.data`. You may also use `FetchError`

```ts
await ofetch("https://google.com/404");
// FetchError: [GET] "https://google/404": 404 "Not Found"
// FetchError: [GET] "https://google/404": 404 Not Found
// at async main (/project/playground.ts:4:3)
```

Expand Down
28 changes: 23 additions & 5 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import type {
FetchResponse,
} from "./fetch";

export class FetchError<T = any> extends Error {
name = "FetchError";
export interface IFetchError<T = any> extends Error {
request?: FetchRequest;
options?: FetchOptions;
response?: FetchResponse<T>;
Expand All @@ -17,7 +16,23 @@ export class FetchError<T = any> extends Error {
statusMessage?: string;
}

export function createFetchError<T = any>(ctx: FetchContext<T>): FetchError<T> {
export class FetchError<T = any> extends Error implements IFetchError<T> {
constructor(message: string, opts?: { cause: unknown }) {
// @ts-ignore https://v8.dev/features/error-cause
super(message, opts);

this.name = "FetchError";

// Polyfill cause for other runtimes
if (opts?.cause && !this.cause) {
this.cause = opts.cause;
}
}
}

export function createFetchError<T = any>(
ctx: FetchContext<T>
): IFetchError<T> {
const errorMessage = ctx.error?.message || ctx.error?.toString() || "";

const method =
Expand All @@ -26,14 +41,17 @@ export function createFetchError<T = any>(ctx: FetchContext<T>): FetchError<T> {
const requestStr = `[${method}] ${JSON.stringify(url)}`;

const statusStr = ctx.response
? `${ctx.response.status} ${JSON.stringify(ctx.response.statusText)}`
? `${ctx.response.status} ${ctx.response.statusText}`
: "<no response>";

const message = `${requestStr}: ${statusStr}${
errorMessage ? ` ${errorMessage}` : ""
}`;

const fetchError: FetchError<T> = new FetchError(message);
const fetchError: FetchError<T> = new FetchError(
message,
ctx.error ? { cause: ctx.error } : undefined
);

for (const key of ["request", "options", "response"] as const) {
Object.defineProperty(fetchError, key, {
Expand Down
2 changes: 1 addition & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ describe("ofetch", () => {
(error) => error
);
expect(error.toString()).toBe(
'FetchError: [POST] "http://localhost:3000/403": 403 "Forbidden"'
'FetchError: [POST] "http://localhost:3000/403": 403 Forbidden'
);
expect(error.request).to.equal(getURL("403"));
expect(error.options.method).to.equal("POST");
Expand Down

0 comments on commit 905244a

Please sign in to comment.