Skip to content

Commit e23b4b1

Browse files
committed
Improved how the api info is added to client errors
1 parent 746696a commit e23b4b1

File tree

4 files changed

+25
-14
lines changed

4 files changed

+25
-14
lines changed

lib/client/index.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,6 @@ export async function getData(id: number, api: string, body?: any, options?: req
208208
// compile an error based on the data and throw it
209209
const error: any = new Error(data.error.message);
210210
error.name = data.error.name;
211-
error.api = api;
212211

213212
if (data.error.status) {
214213
error.status = data.error.status;
@@ -255,7 +254,6 @@ export async function sendData(id: number, api: string, body?: any, options?: re
255254
// compile an error based on the data and throw it
256255
const error: any = new Error(data.error.message);
257256
error.name = data.error.name;
258-
error.api = api;
259257

260258
if (data.error.status) {
261259
error.status = data.error.status;
@@ -288,17 +286,28 @@ export async function fetch(api: string, body?: any, options?: requestOptions):
288286
let id = newIndex();
289287

290288
let method = options && options.method ? options.method : "GET";
291-
switch (method) {
292-
case "POST":
293-
return await sendData(id, api, body, options);
294-
case "PUT":
295-
return await sendData(id, api, body, options);
296-
case "DELETE":
297-
return await getData(id, api, body, options);
298-
case "GET":
299-
default:
300-
return await getData(id, api, body, options);
289+
290+
try {
291+
switch (method) {
292+
case "POST":
293+
return await sendData(id, api, body, options);
294+
case "PUT":
295+
return await sendData(id, api, body, options);
296+
case "DELETE":
297+
return await getData(id, api, body, options);
298+
case "GET":
299+
default:
300+
return await getData(id, api, body, options);
301+
}
302+
} catch(err: any){
303+
if(err instanceof Error){
304+
(err as any).api = api;
305+
}
306+
307+
throw err;
301308
}
309+
310+
302311
}
303312

304313
/**

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "websocketapi",
3-
"version": "1.1.12",
3+
"version": "1.1.13",
44
"description": "",
55
"main": "out/index.js",
66
"types": "out/index.d.ts",

static/bundle.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ async function getData(id, api, body, options) {
257257
// compile an error based on the data and throw it
258258
const error = new Error(data.error.message);
259259
error.name = data.error.name;
260+
error.api = api;
260261
if (data.error.status) {
261262
error.status = data.error.status;
262263
}
@@ -297,6 +298,7 @@ async function sendData(id, api, body, options) {
297298
// compile an error based on the data and throw it
298299
const error = new Error(data.error.message);
299300
error.name = data.error.name;
301+
error.api = api;
300302
if (data.error.status) {
301303
error.status = data.error.status;
302304
}

0 commit comments

Comments
 (0)