Skip to content

Commit 7f04358

Browse files
committed
Better error handling
1 parent 1a052bb commit 7f04358

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

src/smsapi/httpClient/index.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@ import { extractDataFromResponse } from './extractDataFromResponse';
22

33
export type QueryParams = Record<string, unknown>;
44

5+
export class HttpError extends Error {
6+
constructor(
7+
message: string,
8+
public status: number,
9+
public statusText: string,
10+
public responseBody: string,
11+
public url: string,
12+
) {
13+
super(message);
14+
this.name = 'HttpError';
15+
}
16+
}
17+
518
export interface RequestConfig {
619
url: string;
720
method: string;
@@ -123,8 +136,12 @@ export class HttpClient {
123136

124137
if (!response.ok) {
125138
const errorData = await response.text();
126-
throw new Error(
127-
`Request failed with status ${response.status}: ${errorData}`,
139+
throw new HttpError(
140+
`Request failed with status ${response.status}: ${response.statusText}`,
141+
response.status,
142+
response.statusText,
143+
errorData,
144+
response.url,
128145
);
129146
}
130147

0 commit comments

Comments
 (0)