Commit 3df80053 authored by Alejandro Celaya's avatar Alejandro Celaya Committed by Alejandro Celaya

Migrate frame-error-capture to TS

parent 47bf47f2
/** @type {Window|null} */ let errorDestination: Window | null = null;
let errorDestination = null;
/** /**
* Wrap a callback with an error handler which forwards errors to another frame * Wrap a callback with an error handler which forwards errors to another frame
* using {@link sendError}. * using {@link sendError}.
* *
* @template {unknown[]} Args * @param context - A short message indicating where the error happened.
* @template Result
* @param {(...args: Args) => Result} callback
* @param {string} context - A short message indicating where the error happened.
* @return {(...args: Args) => Result}
*/ */
export function captureErrors(callback, context) { export function captureErrors<Result, Args = unknown>(
callback: (...args: Args[]) => Result,
context: string
): (...args: Args[]) => Result {
return (...args) => { return (...args) => {
try { try {
return callback(...args); return callback(...args);
...@@ -22,22 +20,18 @@ export function captureErrors(callback, context) { ...@@ -22,22 +20,18 @@ export function captureErrors(callback, context) {
}; };
} }
/** type ErrorData = {
* @typedef ErrorData message: string;
* @prop {string} message stack?: string;
* @prop {string} [stack] };
*/
/** /**
* Return a cloneable representation of an Error. * Return a cloneable representation of an Error.
* *
* This is needed in browsers that don't support structured-cloning of Error * This is needed in browsers that don't support structured-cloning of Error
* objects, or if the error is not cloneable for some reason. * objects, or if the error is not cloneable for some reason.
*
* @param {Error|unknown} err
* @return {ErrorData}
*/ */
function serializeError(err) { function serializeError(err: Error | unknown): ErrorData {
if (!(err instanceof Error)) { if (!(err instanceof Error)) {
return { message: String(err), stack: undefined }; return { message: String(err), stack: undefined };
} }
...@@ -50,11 +44,8 @@ function serializeError(err) { ...@@ -50,11 +44,8 @@ function serializeError(err) {
/** /**
* Convert error data serialized by {@link serializeError} back into an Error. * Convert error data serialized by {@link serializeError} back into an Error.
*
* @param {ErrorData} data
* @return {Error}
*/ */
function deserializeError(data) { function deserializeError(data: ErrorData): ErrorData {
const err = new Error(data.message); const err = new Error(data.message);
err.stack = data.stack; err.stack = data.stack;
return err; return err;
...@@ -71,10 +62,9 @@ function deserializeError(data) { ...@@ -71,10 +62,9 @@ function deserializeError(data) {
* for the moment because we are trying to rule out problems with * for the moment because we are trying to rule out problems with
* MessageChannel/MessagePort when setting up sidebar <-> host communication. * MessageChannel/MessagePort when setting up sidebar <-> host communication.
* *
* @param {unknown} error * @param context - A short message indicating where the error happened.
* @param {string} context - A short message indicating where the error happened.
*/ */
export function sendError(error, context) { export function sendError(error: unknown, context: string) {
if (!errorDestination) { if (!errorDestination) {
return; return;
} }
...@@ -109,12 +99,12 @@ export function sendError(error, context) { ...@@ -109,12 +99,12 @@ export function sendError(error, context) {
/** /**
* Register a handler for errors sent to the current frame using {@link sendError} * Register a handler for errors sent to the current frame using {@link sendError}
* *
* @param {(error: unknown, context: string) => void} callback * @return A function that unregisters the handler
* @return {() => void} A function that unregisters the handler
*/ */
export function handleErrorsInFrames(callback) { export function handleErrorsInFrames(
/** @param {MessageEvent} event */ callback: (error: unknown, context: string) => void
const handleMessage = event => { ): () => void {
const handleMessage = (event: MessageEvent) => {
const { data } = event; const { data } = event;
if (data && data?.type === 'hypothesis-error') { if (data && data?.type === 'hypothesis-error') {
const { context, error } = data; const { context, error } = data;
...@@ -130,9 +120,7 @@ export function handleErrorsInFrames(callback) { ...@@ -130,9 +120,7 @@ export function handleErrorsInFrames(callback) {
/** /**
* Register a destination frame that {@link sendError} should submit errors to. * Register a destination frame that {@link sendError} should submit errors to.
*
* @param {Window|null} destination
*/ */
export function sendErrorsTo(destination) { export function sendErrorsTo(destination: Window | null) {
errorDestination = destination; errorDestination = destination;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment