Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhanced Error Reporting for Improved Debugging and User Experience #100

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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} - code : ${errorObj.code}`});
} 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
Loading