Add UI and logic for handling worker errors
This commit is contained in:
@ -30,7 +30,7 @@ const resultMessage = <RS, A extends C.WorkerAckType>(
|
||||
|
||||
/** Create a worker response for unexpected errors */
|
||||
const errorMessage = <RS, A extends C.WorkerAckType>(
|
||||
error: Error
|
||||
error: E.WorkerError
|
||||
): C.WorkerResponseData<RS, A> => ({ type: "error", error });
|
||||
|
||||
/** Initialize the execution controller */
|
||||
@ -129,15 +129,16 @@ export const setupWorker = <RS>(engine: LanguageEngine<RS>) => {
|
||||
if (ev.data.type === "ValidateCode")
|
||||
return validateCode(controller, ev.data.params.code);
|
||||
if (ev.data.type === "Execute")
|
||||
return execute(controller, ev.data.params.interval);
|
||||
return await execute(controller, ev.data.params.interval);
|
||||
if (ev.data.type === "Pause") return await pauseExecution(controller);
|
||||
if (ev.data.type === "ExecuteStep") return executeStep(controller);
|
||||
if (ev.data.type === "UpdateBreakpoints")
|
||||
return updateBreakpoints(controller, ev.data.params.points);
|
||||
} catch (error) {
|
||||
// Error here indicates an implementation bug
|
||||
console.error(error);
|
||||
postMessage(errorMessage(error as Error));
|
||||
const err = error as Error;
|
||||
postMessage(errorMessage(E.serializeError(err)));
|
||||
return;
|
||||
}
|
||||
throw new Error("Invalid worker message type");
|
||||
});
|
||||
|
@ -73,4 +73,4 @@ export type WorkerResponseData<RS, A extends WorkerAckType> =
|
||||
error?: E.WorkerRuntimeError;
|
||||
}
|
||||
/** Response indicating a bug in worker/engine logic */
|
||||
| { type: "error"; error: Error };
|
||||
| { type: "error"; error: E.WorkerError };
|
||||
|
@ -58,6 +58,13 @@ export type WorkerRuntimeError = {
|
||||
message: string;
|
||||
};
|
||||
|
||||
/** Error sent by worker indicating an implementation bug */
|
||||
export type WorkerError = {
|
||||
name: string;
|
||||
message: string;
|
||||
stack?: string;
|
||||
};
|
||||
|
||||
/** Serialize a RuntimeError instance into a plain object */
|
||||
export const serializeRuntimeError = (
|
||||
error: RuntimeError
|
||||
@ -69,3 +76,8 @@ export const serializeRuntimeError = (
|
||||
export const serializeParseError = (error: ParseError): WorkerParseError => {
|
||||
return { name: "ParseError", message: error.message, range: error.range };
|
||||
};
|
||||
|
||||
/** Serialize an arbitrary error into a plain object */
|
||||
export const serializeError = (error: Error): WorkerError => {
|
||||
return { name: error.name, message: error.message, stack: error.stack };
|
||||
};
|
||||
|
Reference in New Issue
Block a user