Skip to content

Commit d0ef932

Browse files
committed
better error wrapping
1 parent 560b196 commit d0ef932

File tree

6 files changed

+65
-74
lines changed

6 files changed

+65
-74
lines changed

packages/sdk/src/dialect-cloud-api/data-service-errors.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ export async function withErrorParsing<T>(
2424
}
2525
if (e instanceof ApiError) {
2626
if (e.statusCode === 400) {
27-
console.error(e);
28-
throw new IllegalArgumentError(createMessage(e));
27+
throw new IllegalArgumentError(e.error, createMessage(e));
2928
}
3029
if (e.statusCode === 401) {
3130
throw new AuthenticationError(createMessage(e));
@@ -51,5 +50,5 @@ export async function withErrorParsing<T>(
5150
}
5251

5352
function createMessage(e: ApiError) {
54-
return `${e.message ?? e.error}`;
53+
return `${e.message ?? JSON.stringify(e.error)}`;
5554
}

packages/sdk/src/dialect-cloud-api/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export class ApiError {
3939
readonly error: string,
4040
readonly statusCode: number,
4141
readonly message?: string | null,
42+
readonly details?: any,
4243
readonly requestId?: string | null,
4344
) {}
4445
}

packages/sdk/src/dialect-cloud-api/v2/application-api.ts

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -79,46 +79,42 @@ export class AppV2Api {
7979
clientKey: this.clientKey,
8080
});
8181

82-
try {
83-
const response = await fetch(`${this.baseUrl}${path}`, {
84-
...init,
85-
headers: {
86-
...headers,
87-
...init?.headers,
88-
},
89-
});
90-
91-
if (response.ok) {
92-
return response.json();
93-
}
94-
const isJsonResponse = response.headers
95-
.get('Content-Type')
96-
?.includes('application/json');
97-
98-
if (!isJsonResponse) {
99-
console.error(await response.text());
100-
throw new ApiError(
101-
'Unknown Error',
102-
response.status,
103-
null,
104-
headers[XRequestIdHeader] ?? null,
105-
);
106-
}
107-
108-
const { error, message } = (await response.json()) as {
109-
message?: string;
110-
error?: any;
111-
};
82+
const response = await fetch(`${this.baseUrl}${path}`, {
83+
...init,
84+
headers: {
85+
...headers,
86+
...init?.headers,
87+
},
88+
});
89+
90+
if (response.ok) {
91+
return response.json();
92+
}
93+
const isJsonResponse = response.headers
94+
.get('Content-Type')
95+
?.includes('application/json');
96+
97+
if (!isJsonResponse) {
11298
throw new ApiError(
113-
message ?? 'Unknown Error',
99+
'Unknown Error',
114100
response.status,
115-
error,
101+
null,
116102
headers[XRequestIdHeader] ?? null,
117103
);
118-
} catch (e) {
119-
console.error(e);
120-
throw new NetworkError();
121104
}
105+
106+
const { error, message } = (await response.json()) as {
107+
message?: string;
108+
error?: any;
109+
};
110+
111+
throw new ApiError(
112+
'ApiError',
113+
response.status,
114+
message ?? JSON.stringify(error),
115+
error,
116+
headers[XRequestIdHeader] ?? null,
117+
);
122118
}
123119

124120
async sendAlert(appId: string, request: SendAlertRequest): Promise<void> {

packages/sdk/src/dialect-cloud-api/v2/auth-api.ts

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -31,47 +31,42 @@ export class AuthV2Api {
3131
clientKey: this.clientKey,
3232
});
3333

34-
try {
35-
const response = await fetch(`${this.baseUrl}${path}`, {
36-
...init,
37-
headers: {
38-
...headers,
39-
...init?.headers,
40-
},
41-
});
42-
43-
if (response.ok) {
44-
return response.json();
45-
}
46-
const isJsonResponse = response.headers
47-
.get('Content-Type')
48-
?.includes('application/json');
34+
const response = await fetch(`${this.baseUrl}${path}`, {
35+
...init,
36+
headers: {
37+
...headers,
38+
...init?.headers,
39+
},
40+
});
4941

50-
if (!isJsonResponse) {
51-
const text = await response.text();
52-
console.error(text);
53-
throw new ApiError(
54-
'Unknown Error',
55-
response.status,
56-
text,
57-
headers[XRequestIdHeader] ?? null,
58-
);
59-
}
42+
if (response.ok) {
43+
return response.json();
44+
}
45+
const isJsonResponse = response.headers
46+
.get('Content-Type')
47+
?.includes('application/json');
6048

61-
const { error, message } = (await response.json()) as {
62-
message?: string;
63-
error?: any;
64-
};
49+
if (!isJsonResponse) {
6550
throw new ApiError(
66-
message ?? 'Unknown Error',
51+
'Unknown Error',
6752
response.status,
68-
error,
53+
null,
6954
headers[XRequestIdHeader] ?? null,
7055
);
71-
} catch (e) {
72-
console.error(e);
73-
throw new NetworkError();
7456
}
57+
58+
const { error, message } = (await response.json()) as {
59+
message?: string;
60+
error?: any;
61+
};
62+
63+
throw new ApiError(
64+
'ApiError',
65+
response.status,
66+
message ?? JSON.stringify(error),
67+
error,
68+
headers[XRequestIdHeader] ?? null,
69+
);
7570
}
7671

7772
async getAuth(): Promise<AuthResponse> {

packages/sdk/src/internal/errors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { DialectSdkError } from '../sdk/errors';
33
export abstract class DialectCloudError extends DialectSdkError {}
44

55
export class DialectCloudUnreachableError extends DialectCloudError {
6-
constructor(details?: any[]) {
6+
constructor(details?: any) {
77
super(
88
DialectCloudUnreachableError.name,
99
'Lost connection to Dialect Cloud',

packages/sdk/src/sdk/errors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export class DialectSdkError extends Error {
33
readonly type: string,
44
readonly title: string,
55
readonly msg?: string,
6-
readonly details?: any[],
6+
readonly details?: any,
77
) {
88
super(msg);
99
this.msg = msg;

0 commit comments

Comments
 (0)