-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
- Loading branch information
Showing
5 changed files
with
886 additions
and
293 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,37 @@ | ||
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.