Skip to content

Commit

Permalink
refactor(error): factory pattern for getters
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Aug 23, 2023
1 parent b70e6b0 commit 6139785
Showing 1 changed file with 21 additions and 40 deletions.
61 changes: 21 additions & 40 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,46 +35,27 @@ export function createFetchError<T = any>(ctx: FetchContext<T>): FetchError<T> {

const fetchError: FetchError<T> = new FetchError(message);

Object.defineProperty(fetchError, "request", {
get() {
return ctx.request;
},
});
Object.defineProperty(fetchError, "options", {
get() {
return ctx.options;
},
});
Object.defineProperty(fetchError, "response", {
get() {
return ctx.response;
},
});
Object.defineProperty(fetchError, "data", {
get() {
return ctx.response && ctx.response._data;
},
});
Object.defineProperty(fetchError, "status", {
get() {
return ctx.response && ctx.response.status;
},
});
Object.defineProperty(fetchError, "statusText", {
get() {
return ctx.response && ctx.response.statusText;
},
});
Object.defineProperty(fetchError, "statusCode", {
get() {
return ctx.response && ctx.response.status;
},
});
Object.defineProperty(fetchError, "statusMessage", {
get() {
return ctx.response && ctx.response.statusText;
},
});
for (const key of ["request", "options", "response"] as const) {
Object.defineProperty(fetchError, key, {
get() {
return ctx[key];
},
});
}

for (const [key, refKey] of [
["data", "_data"],
["status", "status"],
["statusCode", "status"],
["statusText", "statusText"],
["statusMessage", "statusText"],
] as const) {
Object.defineProperty(fetchError, key, {
get() {
return ctx.response && ctx.response[refKey];
},
});
}

return fetchError;
}

0 comments on commit 6139785

Please sign in to comment.