@@ -31,34 +31,49 @@ export class PerfLog {
3131
3232/**
3333 * Call a function f and if it fails, log the error with performance information included.
34- * @param action label of action in the error log.
34+ * @param action label of action to include in the error log.
3535 * @param f action to attempt.
36- * @param params params that were passed to f.
37- * @param errMap optional mapping to apply to error to potentially add information .
36+ * @param params params that were passed to f. Defaults to empty.
37+ * @param getCode optional mapping to extract code from error. Defaults to name of error .
3838 * @returns result of f
3939 */
40- export function withPerfLogOnFail < Result , E extends Error = never > (
40+ export function withPerfLogOnFail < Result > (
41+ action : string ,
42+ f : ( ) => Result ,
43+ params ?: object ,
44+ getCode ?: ( e : Error ) => string
45+ ) : ( ) => Result
46+ export function withPerfLogOnFail < Result > (
47+ action : string ,
48+ f : ( ) => Promise < Result > ,
49+ params ?: object ,
50+ getCode ?: ( e : Error ) => string
51+ ) : ( ) => Promise < Result >
52+ export function withPerfLogOnFail < Result > (
4153 action : string ,
4254 f : ( ) => Result | Promise < Result > ,
43- params : object = { } ,
44- errMap ?: ( e : Error ) => E
55+ params ? : object ,
56+ getCode ?: ( e : Error ) => string
4557) {
46- return async function ( ) {
58+ return function ( ) {
4759 const perflog = new PerfLog ( action )
4860 try {
49- return await f ( )
61+ return f ( )
5062 } catch ( e ) {
5163 if ( e instanceof Error ) {
52- const errWithoutStack = errMap ? errMap ( e ) : { ... e }
64+ const errWithoutStack = { ... e , name : e . name , message : e . message }
5365 delete errWithoutStack [ 'stack' ]
5466 const timecost = perflog . elapsed ( ) . toFixed ( 1 )
5567 getLogger ( ) . error (
5668 `${ action } failed (time: %dms) \nparams: %O\nerror: %O` ,
5769 timecost ,
58- params ,
70+ params ?? { } ,
5971 errWithoutStack
6072 )
61- throw new ToolkitError ( `${ action } : ${ e . message } ` , { code : e . message , cause : e } )
73+ throw new ToolkitError ( `${ action } : ${ errWithoutStack . message } ` , {
74+ code : getCode ? getCode ( errWithoutStack ) : errWithoutStack . name ,
75+ cause : errWithoutStack ,
76+ } )
6277 }
6378 throw e
6479 }
0 commit comments