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

Improve/error handling #143

Merged
merged 5 commits into from
Mar 14, 2024
Merged
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
2 changes: 1 addition & 1 deletion lib/client/components/logger/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ const LogComp = ({
{isLoading && <LoadingComp />}

{errors.length ? (
<pre>{JSON.stringify(errors)}</pre>
<pre>{JSON.stringify(errors, null, 2)}</pre>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (code_refinement): Formatting the errors with JSON.stringify(errors, null, 2) improves readability in the UI. This is a good practice for enhancing the debugging experience.

) : (
logs.length > 0 && (
<LogBlock
Expand Down
66 changes: 40 additions & 26 deletions lib/client/helpers/socket/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -226,35 +226,49 @@ export const SockProvider = ({ children }: ChildrenProps) => {
useDebounce(
() => {
if (typeof window !== 'undefined') {
try {
sockPromise.current = new Promise<wsock.w3cwebsocket>((res, rej) => {
let rejected = false;
try {
// eslint-disable-next-line new-cap
const w = new wsock.w3cwebsocket(`${socketUrl}/ws`, '', '', {});

w.onmessage = onMessage;

w.onopen = () => {
res(w);
};

w.onerror = (e) => {
console.error('socket closed:', e);
if (!rejected) {
rejected = true;
const connnect = (recon = () => {}) => {
try {
sockPromise.current = new Promise<wsock.w3cwebsocket>(
(res, rej) => {
try {
// eslint-disable-next-line new-cap
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (code_refinement): Disabling ESLint rules can hide potential issues. If the 'new-cap' rule is frequently disabled, consider configuring this rule project-wide or ensuring that the naming conventions for constructors are followed.

const w = new wsock.w3cwebsocket(
`${socketUrl}/ws`,
'',
'',
{}
);

w.onmessage = onMessage;

w.onopen = () => {
res(w);
};

w.onerror = (e) => {
console.error(e);
recon();
Comment on lines +248 to +250
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (code_refinement): Logging the error before calling the reconnect function could provide valuable debugging information. Consider logging the error using logger.error or console.error before the recon() call.

};

w.onclose = () => {
recon();
};
} catch (e) {
rej(e);
}
};
}
);
} catch (e) {
logger.error(e);
}
};
Comment on lines +229 to +264
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick (typo): The function name 'connnect' seems to be a typo. Consider renaming it to 'connect' for clarity.


w.onclose = () => {};
} catch (e) {
rej(e);
}
});
} catch (e) {
logger.error(e);
}
connnect(() => {
setTimeout(() => {
console.log('reconnecting');
connnect();
}, 1000);
});
Comment on lines +266 to +271
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (performance): Implementing an exponential backoff strategy for reconnection attempts could improve the robustness of the socket connection, especially in unstable network conditions. Constantly reconnecting every 1 second might not be the most efficient approach.

}
},
1000,
Expand Down
Loading