@@ -4,29 +4,37 @@ import {
44 HttpHandler ,
55 HttpEvent ,
66 HttpInterceptor ,
7+ HttpErrorResponse ,
78} from '@angular/common/http' ;
8- import { Observable , throwError } from 'rxjs' ;
9+ import { Observable , of } from 'rxjs' ;
910import { catchError } from 'rxjs/operators' ;
10-
1111import { Store } from '@ngrx/store' ;
12+
1213import { logoutAction } from '@app/modules/authentication/store/auth.actions' ;
14+ import { fetchFormsErrorAction } from '@app/core/store/session/session.actions' ;
1315
1416@Injectable ( )
15- export class ErrorInterceptor implements HttpInterceptor {
17+ export class ErrorsInterceptor implements HttpInterceptor {
1618 constructor ( private store : Store ) { }
1719
1820 intercept (
1921 request : HttpRequest < unknown > ,
2022 next : HttpHandler
2123 ) : Observable < HttpEvent < unknown > > {
2224 return next . handle ( request ) . pipe (
23- catchError ( err => {
24- if ( [ 401 , 403 ] . indexOf ( err . status ) !== - 1 ) {
25+ catchError ( ( response : HttpErrorResponse ) => {
26+ if ( [ 401 , 403 ] . includes ( response . status ) ) {
2527 this . store . dispatch ( logoutAction ( ) ) ;
2628 }
2729
28- const error = err . error . message || err . statusText ;
29- return throwError ( error ) ;
30+ if ( [ 422 ] . includes ( response . status ) ) {
31+ this . store . dispatch (
32+ fetchFormsErrorAction ( { formErrors : response . error } )
33+ ) ;
34+ }
35+
36+ const error = response . error . message || response . statusText ;
37+ return of ( error ) ;
3038 } )
3139 ) ;
3240 }
0 commit comments