-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Allow to pass
errorHandler
as record option (#1107)
* feat: Allow to pass `errorHandler` as record option * add docs * Apply formatting changes
- Loading branch information
Showing
7 changed files
with
922 additions
and
314 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'rrweb': minor | ||
--- | ||
|
||
feat: Allow to pass `errorHandler` as record option |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import type { ErrorHandler } from '../types'; | ||
|
||
type Callback = (...args: unknown[]) => unknown; | ||
|
||
let errorHandler: ErrorHandler | undefined; | ||
|
||
export function registerErrorHandler(handler: ErrorHandler | undefined) { | ||
errorHandler = handler; | ||
} | ||
|
||
export function unregisterErrorHandler() { | ||
errorHandler = undefined; | ||
} | ||
|
||
/** | ||
* Wrap callbacks in a wrapper that allows to pass errors to a configured `errorHandler` method. | ||
*/ | ||
export const callbackWrapper = <T extends Callback>(cb: T): T => { | ||
if (!errorHandler) { | ||
return cb; | ||
} | ||
|
||
const rrwebWrapped = ((...rest: unknown[]) => { | ||
try { | ||
return cb(...rest); | ||
} catch (error) { | ||
if (errorHandler && errorHandler(error) === true) { | ||
return; | ||
} | ||
|
||
throw error; | ||
} | ||
}) as unknown as T; | ||
|
||
return rrwebWrapped; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.