Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
3df80053
Commit
3df80053
authored
Apr 21, 2023
by
Alejandro Celaya
Committed by
Alejandro Celaya
Apr 25, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Migrate frame-error-capture to TS
parent
47bf47f2
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
20 additions
and
32 deletions
+20
-32
frame-error-capture.ts
src/shared/frame-error-capture.ts
+20
-32
No files found.
src/shared/frame-error-capture.
j
s
→
src/shared/frame-error-capture.
t
s
View file @
3df80053
/** @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
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment