Skip to content

Commit

Permalink
feat: throwing more descriptive errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Sadaf-A committed Feb 25, 2024
1 parent dcfab68 commit 1d48a83
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 16 deletions.
26 changes: 26 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export class NeutralinoError extends Error {
code: string;
message: string;

constructor({
code,
message
}: {
code: string;
message: string;
}) {
super();
this.code = code;
this.message = message;
}
}

export function logError(e) {
const error = JSON.stringify(e);
const errorObj = JSON.parse(error);
if (errorObj && typeof errorObj === 'object' && 'code' in errorObj && 'message' in errorObj) {
throw new NeutralinoError({ code: errorObj.code, message: errorObj.message});
} else {
console.error('Invalid error object:', errorObj);
}
}
37 changes: 21 additions & 16 deletions src/ws/websocket.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as extensions from '../api/extensions';
import * as events from '../browser/events';
import { logError } from '../errors';
import { base64ToBytesArray } from '../helpers';

let ws;
Expand All @@ -15,27 +16,31 @@ export function init() {
registerSocketEvents();
}

export function sendMessage(method: string, data?: any): Promise<any> {
return new Promise((resolve: any, reject: any) => {
export async function sendMessage(method: string, data?: any): Promise<any> {
try {
return await new Promise((resolve: any, reject: any) => {

if(ws?.readyState != WebSocket.OPEN) {
sendWhenReady({method, data, resolve, reject});
return;
}
if(ws?.readyState != WebSocket.OPEN) {
sendWhenReady({method, data, resolve, reject});
return;
}

const id: string = uuidv4();
const accessToken: string = getAuthToken();
const id: string = uuidv4();
const accessToken: string = getAuthToken();

nativeCalls[id] = {resolve, reject};
nativeCalls[id] = {resolve, reject};

ws.send(JSON.stringify({
id,
method,
data,
accessToken
}));
ws.send(JSON.stringify({
id,
method,
data,
accessToken
}));

});
});
} catch (e) {
logError(e);
}
}

export function sendWhenReady(message: any) {
Expand Down

0 comments on commit 1d48a83

Please sign in to comment.