Skip to content

Commit

Permalink
Modify HTTPError position & field naming
Browse files Browse the repository at this point in the history
  • Loading branch information
pengooseDev committed Mar 7, 2024
1 parent 1f6cc67 commit 74ba5bc
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 32 deletions.
50 changes: 26 additions & 24 deletions lib/exceptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ interface FetchErrorDetails extends Status {
}

// Deprecated
interface AxiosErrorDetails extends Partial<Status> {
interface AxiosErrorDetails {
originalError: Error;
code?: string;
statusCode?: number;
statusMessage?: string;
}

export class SignatureValidationFailed extends Error {
Expand All @@ -43,26 +45,6 @@ export class JSONParseError extends Error {
}
}

export class HTTPFetchError extends Error {
public status: number;

public statusText: string;

public headers: Headers;

public body: string;

constructor(
message: Message,
{ status, statusText, headers, body }: FetchErrorDetails,
) {
super(message);
this.name = this.constructor.name;

Object.assign(this, { status, statusText, headers, body });
}
}

/* Deprecated */
export class RequestError extends Error {
public code: string;
Expand All @@ -89,19 +71,39 @@ export class ReadError extends Error {
}

export class HTTPError extends Error {
public statusCode: number;

public statusMessage: string;

public originalError: any;

constructor(
message: Message,
{ statusCode, statusMessage, originalError }: AxiosErrorDetails,
) {
super(message);
this.name = this.constructor.name;

Object.assign(this, { statusCode, statusMessage, originalError });
}
}

export class HTTPFetchError extends Error {
public status: number;

public statusText: string;

public originalError: any;
public headers: Headers;

public body: string;

constructor(
message: Message,
{ status, statusText, originalError }: AxiosErrorDetails,
{ status, statusText, headers, body }: FetchErrorDetails,
) {
super(message);
this.name = this.constructor.name;

Object.assign(this, { status, statusText, originalError });
Object.assign(this, { status, statusText, headers, body });
}
}
6 changes: 5 additions & 1 deletion lib/http-axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,11 @@ export default class HTTPClient {
const { status, statusText } = err.response;
const { message } = err;

return new HTTPError(message, { status, statusText, originalError: err });
return new HTTPError(message, {
statusCode: status,
statusMessage: statusText,
originalError: err,
});
} else if (err.code) {
const { message, code } = err;
return new RequestError(message, { code, originalError: err });
Expand Down
2 changes: 1 addition & 1 deletion test/http-axios.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ describe("http", () => {
} catch (err) {
ok(err instanceof HTTPError);
equal(scope.isDone(), true);
equal(err.status, 404);
equal(err.statusCode, 404);
}
});

Expand Down
12 changes: 6 additions & 6 deletions test/middleware.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe("middleware", () => {
ok(false);
} catch (err) {
if (err instanceof HTTPError) {
equal(err.status, 415);
equal(err.statusCode, 415);
} else {
throw err;
}
Expand All @@ -116,7 +116,7 @@ describe("middleware", () => {
ok(false);
} catch (err) {
if (err instanceof HTTPError) {
equal(err.status, 500);
equal(err.statusCode, 500);
} else {
throw err;
}
Expand All @@ -142,7 +142,7 @@ describe("middleware", () => {
ok(false);
} catch (err) {
if (err instanceof HTTPError) {
equal(err.status, 401);
equal(err.statusCode, 401);
} else {
throw err;
}
Expand All @@ -160,7 +160,7 @@ describe("middleware", () => {
ok(false);
} catch (err) {
if (err instanceof HTTPError) {
equal(err.status, 401);
equal(err.statusCode, 401);
} else {
throw err;
}
Expand All @@ -177,7 +177,7 @@ describe("middleware", () => {
ok(false);
} catch (err) {
if (err instanceof HTTPError) {
equal(err.status, 400);
equal(err.statusCode, 400);
} else {
throw err;
}
Expand All @@ -193,7 +193,7 @@ describe("middleware", () => {
ok(false);
} catch (err) {
if (err instanceof HTTPError) {
equal(err.status, 401);
equal(err.statusCode, 401);
} else {
throw err;
}
Expand Down

0 comments on commit 74ba5bc

Please sign in to comment.