Skip to content

Commit 7ec05f4

Browse files
authored
Merge pull request #29 from T-Reimer:feature/extra-error-fields
Feature/extra-error-fields
2 parents 810c276 + 67c77df commit 7ec05f4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+10294
-10506
lines changed

lib/client/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const globalFetch: (input: RequestInfo, init?: RequestInitOption) => Promise<Res
3535
clearTimeout(timeoutId);
3636
}
3737
return result;
38-
} catch (err) {
38+
} catch (err: any) {
3939
// check if it's an abort error
4040
if (err.name === "AbortError") {
4141
throw new TimeoutError("Request to server timed out!");
@@ -208,6 +208,7 @@ 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;
211212

212213
if (data.error.status) {
213214
error.status = data.error.status;
@@ -254,6 +255,7 @@ export async function sendData(id: number, api: string, body?: any, options?: re
254255
// compile an error based on the data and throw it
255256
const error: any = new Error(data.error.message);
256257
error.name = data.error.name;
258+
error.api = api;
257259

258260
if (data.error.status) {
259261
error.status = data.error.status;

lib/client/socket.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ function createNewConnection() {
105105

106106
}
107107

108-
} catch (err) {
108+
} catch (err: any) {
109109
// send the data to the before register event that was set in the options
110110
setOptions.websocketOnMessage(event.data);
111111
}
@@ -165,7 +165,7 @@ function createNewConnection() {
165165
// set the socket as ready
166166
ready = true;
167167

168-
} catch (err) {
168+
} catch (err: any) {
169169
if (setOptions.unHandledWebSocketMessage) {
170170
setOptions.unHandledWebSocketMessage(err, event.data);
171171
}
@@ -240,7 +240,7 @@ export function fetch(id: number, api: string, body?: any, options?: requestOpti
240240
resolve
241241
});
242242

243-
} catch (err) {
243+
} catch (err: any) {
244244
reject(err);
245245
}
246246
});

lib/createExpressRequest.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ export function createExpressRequest(req: ExpressRequest, res: ExpressResponse,
1515
const name = stripUrlSlashes(req.params['0']);
1616

1717
// get the request body contents
18-
const body = (req.method === "GET" || req.method === "DELETE") && req.query.body ? (req.query.body === "undefined" ? undefined : JSON.parse(req.query.body)) : req.body;
18+
const body = (req.method === "GET" || req.method === "DELETE") && req.query.body ? (req.query.body === "undefined" ? undefined : JSON.parse(req.query.body as string)) : req.body;
1919

2020
// create the new request
21-
let newRequest = new ServerRequest(req.params.id, name, body, method, null);
21+
let newRequest = new ServerRequest(parseInt(req.params.id), name, body, method, null);
2222
// call the metric start function
2323
settings.on.eventReceived(newRequest);
2424

lib/createWSRequest.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function createWSRequest(client: wsClient, id: number, name: string, body
1717
try {
1818
// send the data to the client via ws
1919
client.WebSocket.send(JSON.stringify(value));
20-
} catch (err) {
20+
} catch (err: any) {
2121

2222
// if failed to send check if the ready state is closed... If so then unregister anything for that client
2323
if (client.WebSocket.readyState === client.WebSocket.CLOSED) {
@@ -26,7 +26,7 @@ export function createWSRequest(client: wsClient, id: number, name: string, body
2626
// make sure that disconnect events are called
2727
client.WebSocket.terminate();
2828
//ignore any errors
29-
} catch (err) { }
29+
} catch (err: any) { }
3030
} else {
3131

3232
// call the on error handler

lib/registerExpress.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export function registerExpress(app: Application, route: string, settings: Setti
2020
try {
2121
let event = createExpressRequest(request, response, "get", settings);
2222
getEvent.triggerEvent(event);
23-
} catch (err) {
23+
} catch (err: any) {
2424

2525
/**
2626
* Set the status number for the error
@@ -33,7 +33,7 @@ export function registerExpress(app: Application, route: string, settings: Setti
3333
}
3434

3535
const responseData: ResponseData = {
36-
id: id || 0,
36+
id: parseInt(id) || 0,
3737
name: "",
3838
error,
3939
status
@@ -51,7 +51,7 @@ export function registerExpress(app: Application, route: string, settings: Setti
5151
try {
5252
let event = createExpressRequest(request, response, "post", settings);
5353
postEvent.triggerEvent(event);
54-
} catch (err) {
54+
} catch (err: any) {
5555

5656
/**
5757
* Set the status number for the error
@@ -64,7 +64,7 @@ export function registerExpress(app: Application, route: string, settings: Setti
6464
}
6565

6666
const responseData: ResponseData = {
67-
id: id || 0,
67+
id: parseInt(id) || 0,
6868
name: "",
6969
error,
7070
status
@@ -82,7 +82,7 @@ export function registerExpress(app: Application, route: string, settings: Setti
8282
try {
8383
let event = createExpressRequest(request, response, "put", settings);
8484
putEvent.triggerEvent(event);
85-
} catch (err) {
85+
} catch (err: any) {
8686

8787
/**
8888
* Set the status number for the error
@@ -95,7 +95,7 @@ export function registerExpress(app: Application, route: string, settings: Setti
9595
}
9696

9797
const responseData: ResponseData = {
98-
id: id || 0,
98+
id: parseInt(id) || 0,
9999
name: "",
100100
error,
101101
status
@@ -114,7 +114,7 @@ export function registerExpress(app: Application, route: string, settings: Setti
114114
try {
115115
let event = createExpressRequest(request, response, "delete", settings);
116116
delEvent.triggerEvent(event);
117-
} catch (err) {
117+
} catch (err: any) {
118118

119119
/**
120120
* Set the status number for the error
@@ -127,7 +127,7 @@ export function registerExpress(app: Application, route: string, settings: Setti
127127
}
128128

129129
const responseData: ResponseData = {
130-
id: id || 0,
130+
id: parseInt(id) || 0,
131131
name: "",
132132
error,
133133
status

lib/registerWS.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export function registerWS(wss: WebSocket.Server, settings: SettingsInterface) {
4646
return sendAuthFailed(ws);
4747
}
4848

49-
} catch (err) {
49+
} catch (err: any) {
5050
// disconnect. Authentication error
5151
return sendAuthFailed(ws);
5252
}
@@ -65,15 +65,15 @@ function sendAuthFailed(ws: WebSocket) {
6565
const failed: AuthFailedMessage = { event: "auth-failed" };
6666
try {
6767
ws.send(JSON.stringify(failed));
68-
} catch (err) { }
68+
} catch (err: any) { }
6969
ws.terminate();
7070
}
7171

7272
function sendOpenMessage(ws: WebSocket, client: wsClient, settings: SettingsInterface) {
7373
// send a Open connection event to tell the api on client side to start listening
7474
try {
7575
ws.send(JSON.stringify({ event: "connection" }));
76-
} catch (err) {
76+
} catch (err: any) {
7777
// disconnect the client
7878
ws.terminate();
7979
}
@@ -145,7 +145,7 @@ function sendOpenMessage(ws: WebSocket, client: wsClient, settings: SettingsInte
145145
}
146146
}
147147
}
148-
catch (err) {
148+
catch (err: any) {
149149
if (settings.on.error) {
150150
settings.on.error(err, message);
151151
} else {

lib/snapShots/registerSnapshotRequest.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class SnapshotRequest extends Request {
6767
// try to send the message to client
6868
this.client?.WebSocket.send(JSON.stringify(value));
6969

70-
} catch (err) {
70+
} catch (err: any) {
7171

7272
// if failed to send check if the ready state is closed... If so then unregister anything for that client
7373
if (this.client?.WebSocket.readyState === this.client?.WebSocket.CLOSED) {
@@ -76,7 +76,7 @@ class SnapshotRequest extends Request {
7676
// make sure that disconnect events are called
7777
this.client?.WebSocket.terminate();
7878
//ignore any errors
79-
} catch (err) { }
79+
} catch (err: any) { }
8080
} else {
8181

8282
// call the on error handler

lib/ws/wsClient.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,13 @@ export class wsClient {
140140
reject(err);
141141
clearTimeout(timeout);
142142
},
143-
resolve: (...args: any[]) => {
144-
resolve(...args);
143+
resolve: (args: any) => {
144+
resolve(args);
145145
clearTimeout(timeout);
146146
}
147147
});
148148

149-
} catch (err) {
149+
} catch (err: any) {
150150
reject(err);
151151
}
152152
});

out/Request.d.ts

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
/**
2-
* A simple api request
3-
*
4-
*/
5-
export declare class Request {
6-
id: number;
7-
name: string;
8-
body: object;
9-
method: string;
10-
_status: number;
11-
_send: (value: any) => void;
12-
constructor(id: number, name: string, body: object, method: string);
13-
/**
14-
* Set the request status code
15-
*
16-
* @param code The status code
17-
*/
18-
status(code: number): this;
19-
/**
20-
* Send a response to the client
21-
*
22-
* @param value The value to send to client
23-
*/
24-
send(value: any): this;
25-
}
1+
/**
2+
* A simple api request
3+
*
4+
*/
5+
export declare class Request {
6+
id: number;
7+
name: string;
8+
body: object;
9+
method: string;
10+
_status: number;
11+
_send: (value: any) => void;
12+
constructor(id: number, name: string, body: object, method: string);
13+
/**
14+
* Set the request status code
15+
*
16+
* @param code The status code
17+
*/
18+
status(code: number): this;
19+
/**
20+
* Send a response to the client
21+
*
22+
* @param value The value to send to client
23+
*/
24+
send(value: any): this;
25+
}

0 commit comments

Comments
 (0)