11import { ResultRecord } from "../view-models/result-record" ;
2- import {
3- AsyncWorkload ,
4- SyncWorkload ,
5- CatchHandler ,
6- FinallyHandler ,
7- } from "../types/do-try-types" ;
2+ import { DoTryConfig } from "../interfaces/do-try-config" ;
3+ import { AsyncWorkload } from "../types/async-workload" ;
4+ import { CatchResultHandler } from "../types/catch-result-handler" ;
5+ import { FinallyHandler } from "../types/finally-handler" ;
6+ import { SyncWorkload } from "../types/sync-workload" ;
87
98// -----------------------------------------------------------------------------------------
109// #region Do
@@ -19,8 +18,20 @@ import {
1918class Do < TResourceType , TReturnVal = void > {
2019 private promise : Promise < TReturnVal > ;
2120
21+ private static config : DoTryConfig = {
22+ defaultErrorHandler : undefined ,
23+ } ;
24+
2225 private constructor ( workload : AsyncWorkload < TReturnVal > ) {
23- this . promise = workload ( ) ;
26+ this . promise = workload ( ) . catch ( ( err : any ) => {
27+ if ( err instanceof ResultRecord ) {
28+ Do . config . defaultErrorHandler ?.( err , undefined ) ;
29+ throw err ; // rethrow so it doesn't interrupt call chain
30+ }
31+
32+ Do . config . defaultErrorHandler ?.( undefined , err ) ;
33+ throw err ; // rethrow so it doesn't interrupt call chain
34+ } ) ;
2435 }
2536
2637 /**
@@ -33,7 +44,7 @@ class Do<TResourceType, TReturnVal = void> {
3344 * @returns this
3445 */
3546 public catch (
36- errorHandler : CatchHandler < TResourceType >
47+ errorHandler : CatchResultHandler < TResourceType >
3748 ) : Do < TResourceType , TReturnVal > {
3849 this . promise = this . promise . catch ( ( err : any ) => {
3950 if ( err instanceof ResultRecord ) {
@@ -47,6 +58,14 @@ class Do<TResourceType, TReturnVal = void> {
4758 return this ;
4859 }
4960
61+ /**
62+ * Sets the global configuration object for class {Do}
63+ * @param config the {DoTryConfig} object to set
64+ */
65+ public static configure ( config : DoTryConfig ) : void {
66+ Do . config = config ;
67+ }
68+
5069 /**
5170 * Run some handler when the function completes, whether the
5271 * catch() was hit or not.
@@ -96,6 +115,10 @@ class DoSync<TResourceType, TReturnVal = void> {
96115 private catchHandler ?: ( err : any ) => void ;
97116 private finallyHandler ?: FinallyHandler ;
98117
118+ private static config : DoTryConfig = {
119+ defaultErrorHandler : undefined ,
120+ } ;
121+
99122 private constructor ( workload : SyncWorkload < TReturnVal > ) {
100123 this . workload = workload ;
101124 }
@@ -109,7 +132,7 @@ class DoSync<TResourceType, TReturnVal = void> {
109132 * @param errorHandler handle errors, either as a {ResultRecord} or {any}
110133 */
111134 public catch (
112- errorHandler : CatchHandler < TResourceType >
135+ errorHandler : CatchResultHandler < TResourceType >
113136 ) : DoSync < TResourceType , TReturnVal > {
114137 this . catchHandler = ( err : any ) => {
115138 if ( err instanceof ResultRecord ) {
@@ -123,6 +146,14 @@ class DoSync<TResourceType, TReturnVal = void> {
123146 return this ;
124147 }
125148
149+ /**
150+ * Sets the global configuration for class {DySync}.
151+ * @param config the {DoTryConfig} object to set
152+ */
153+ public static configure ( config : DoTryConfig ) : void {
154+ DoSync . config = config ;
155+ }
156+
126157 /**
127158 * Execute the entire DoSync call chain. For the synchronous version, i.e. DoSync,
128159 * you must manually call .execute() for the call chain to be executed.
@@ -132,6 +163,13 @@ class DoSync<TResourceType, TReturnVal = void> {
132163 try {
133164 return this . workload ( ) ;
134165 } catch ( e ) {
166+ if ( e instanceof ResultRecord ) {
167+ DoSync . config . defaultErrorHandler ?.( e , undefined ) ;
168+ this . catchHandler ?.( e ) ;
169+ return ;
170+ }
171+
172+ DoSync . config . defaultErrorHandler ?.( undefined , e ) ;
135173 this . catchHandler ?.( e ) ;
136174 } finally {
137175 this . finallyHandler ?.( ) ;
0 commit comments