11import {
22 Injectable ,
33 NgZone ,
4+ Type ,
45} from '@angular/core' ;
56// import @ngrx
67import {
@@ -50,6 +51,7 @@ import {
5051 AuthenticatedSuccessAction ,
5152 AuthenticationErrorAction ,
5253 AuthenticationSuccessAction ,
54+ AuthErrorActionsWithErrorPayload ,
5355 CheckAuthenticationTokenCookieAction ,
5456 LogOutErrorAction ,
5557 LogOutSuccessAction ,
@@ -81,6 +83,16 @@ const IDLE_TIMER_IGNORE_TYPES: string[]
8183 = [ ...Object . values ( AuthActionTypes ) . filter ( ( t : string ) => t !== AuthActionTypes . UNSET_USER_AS_IDLE ) ,
8284 ...Object . values ( RequestActionTypes ) , ...Object . values ( NotificationsActionTypes ) ] ;
8385
86+ export function errorToAuthAction$ < T extends AuthErrorActionsWithErrorPayload > ( actionType : Type < T > , error : unknown ) : Observable < T > {
87+ if ( error instanceof Error ) {
88+ return observableOf ( new actionType ( error ) ) ;
89+ }
90+
91+ // If we caught something that's not an Error: complain & drop type safety
92+ console . warn ( 'AuthEffects caught non-Error object:' , error ) ;
93+ return observableOf ( new actionType ( error as any ) ) ;
94+ }
95+
8496@Injectable ( )
8597export class AuthEffects {
8698
@@ -94,7 +106,7 @@ export class AuthEffects {
94106 return this . authService . authenticate ( action . payload . email , action . payload . password ) . pipe (
95107 take ( 1 ) ,
96108 map ( ( response : AuthStatus ) => new AuthenticationSuccessAction ( response . token ) ) ,
97- catchError ( ( error ) => observableOf ( new AuthenticationErrorAction ( error ) ) ) ,
109+ catchError ( ( error : unknown ) => errorToAuthAction$ ( AuthenticationErrorAction , error ) ) ,
98110 ) ;
99111 } ) ,
100112 ) ) ;
@@ -109,7 +121,8 @@ export class AuthEffects {
109121 switchMap ( ( action : AuthenticatedAction ) => {
110122 return this . authService . authenticatedUser ( action . payload ) . pipe (
111123 map ( ( userHref : string ) => new AuthenticatedSuccessAction ( ( userHref !== null ) , action . payload , userHref ) ) ,
112- catchError ( ( error ) => observableOf ( new AuthenticatedErrorAction ( error ) ) ) ) ;
124+ catchError ( ( error : unknown ) => errorToAuthAction$ ( AuthenticatedErrorAction , error ) ) ,
125+ ) ;
113126 } ) ,
114127 ) ) ;
115128
@@ -155,15 +168,16 @@ export class AuthEffects {
155168 }
156169 return user$ . pipe (
157170 map ( ( user : EPerson ) => new RetrieveAuthenticatedEpersonSuccessAction ( user . id ) ) ,
158- catchError ( ( error ) => observableOf ( new RetrieveAuthenticatedEpersonErrorAction ( error ) ) ) ) ;
171+ catchError ( ( error : unknown ) => errorToAuthAction$ ( RetrieveAuthenticatedEpersonErrorAction , error ) ) ,
172+ ) ;
159173 } ) ,
160174 ) ) ;
161175
162176 public checkToken$ : Observable < Action > = createEffect ( ( ) => this . actions$ . pipe ( ofType ( AuthActionTypes . CHECK_AUTHENTICATION_TOKEN ) ,
163177 switchMap ( ( ) => {
164178 return this . authService . hasValidAuthenticationToken ( ) . pipe (
165179 map ( ( token : AuthTokenInfo ) => new AuthenticatedAction ( token ) ) ,
166- catchError ( ( error ) => observableOf ( new CheckAuthenticationTokenCookieAction ( ) ) ) ,
180+ catchError ( ( error : unknown ) => observableOf ( new CheckAuthenticationTokenCookieAction ( ) ) ) ,
167181 ) ;
168182 } ) ,
169183 ) ) ;
@@ -181,7 +195,7 @@ export class AuthEffects {
181195 return new RetrieveAuthMethodsAction ( response ) ;
182196 }
183197 } ) ,
184- catchError ( ( error ) => observableOf ( new AuthenticatedErrorAction ( error ) ) ) ,
198+ catchError ( ( error : unknown ) => errorToAuthAction$ ( AuthenticatedErrorAction , error ) ) ,
185199 ) ;
186200 } ) ,
187201 ) ) ;
@@ -192,7 +206,7 @@ export class AuthEffects {
192206 return this . authService . refreshAuthenticationToken ( null ) . pipe (
193207 take ( 1 ) ,
194208 map ( ( token : AuthTokenInfo ) => new AuthenticationSuccessAction ( token ) ) ,
195- catchError ( ( error ) => observableOf ( new AuthenticationErrorAction ( error ) ) ) ,
209+ catchError ( ( error : unknown ) => errorToAuthAction$ ( AuthenticationErrorAction , error ) ) ,
196210 ) ;
197211 } ) ,
198212 ) ) ;
@@ -201,7 +215,7 @@ export class AuthEffects {
201215 switchMap ( ( action : RefreshTokenAction ) => {
202216 return this . authService . refreshAuthenticationToken ( action . payload ) . pipe (
203217 map ( ( token : AuthTokenInfo ) => new RefreshTokenSuccessAction ( token ) ) ,
204- catchError ( ( error ) => observableOf ( new RefreshTokenErrorAction ( ) ) ) ,
218+ catchError ( ( error : unknown ) => observableOf ( new RefreshTokenErrorAction ( ) ) ) ,
205219 ) ;
206220 } ) ,
207221 ) ) ;
@@ -245,8 +259,8 @@ export class AuthEffects {
245259 switchMap ( ( ) => {
246260 this . authService . stopImpersonating ( ) ;
247261 return this . authService . logout ( ) . pipe (
248- map ( ( value ) => new LogOutSuccessAction ( ) ) ,
249- catchError ( ( error ) => observableOf ( new LogOutErrorAction ( error ) ) ) ,
262+ map ( ( ) => new LogOutSuccessAction ( ) ) ,
263+ catchError ( ( error : unknown ) => errorToAuthAction$ ( LogOutErrorAction , error ) ) ,
250264 ) ;
251265 } ) ,
252266 ) ) ;
@@ -272,7 +286,7 @@ export class AuthEffects {
272286 return this . authService . retrieveAuthMethodsFromAuthStatus ( action . payload )
273287 . pipe (
274288 map ( ( authMethodModels : AuthMethod [ ] ) => new RetrieveAuthMethodsSuccessAction ( authMethodModels ) ) ,
275- catchError ( ( error ) => observableOf ( new RetrieveAuthMethodsErrorAction ( ) ) ) ,
289+ catchError ( ( ) => observableOf ( new RetrieveAuthMethodsErrorAction ( ) ) ) ,
276290 ) ;
277291 } ) ,
278292 ) ) ;
0 commit comments