coreui/coreui-angular

send toast inside interceptor

32x0lf opened this issue · 5 comments

Hi,

I have an interceptor and I want to display a message for a certain error to user. I was trying to use toaster service but I don't know how to implement this.

here is my code

intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
    return next.handle(request).pipe(
      catchError((error: HttpErrorResponse) => {
        if(error) {
          switch(error.status) {
            case 400:
              if (error.error.errors) {
                const modelStateErrors = [];
                for (const key in error.error.errors){                          
                  if(error.error.errors[key]) {
                    modelStateErrors.push(error.error.errors[key])
                  }               
                }
                throw modelStateErrors;
              }
              else {
                this.toaster.toasterState$.pipe<HttpErrorResponse>(
                  error.error, 
                );error.status.toString()
              }
              break;
            case 401:
               console.log('Unauthorized', error.status);
               break;
            case 404:
               this.router.navigateByUrl('/404');
               break;
            case 500:
               const navigationextras : NavigationExtras = {state: {error: error.error}};
               this.router.navigateByUrl('/500',navigationextras);
               break;
            default:
              console.log('Something went wrong',error);
              break;
          }
        }
        throw error;
      })
    );
  }

hi how can I send a toast if it is a 400 status?

Hello any update on this?

@32x0lf One of your options is to create a sort of app-toast-service, injectable at the root of your app. It can handle communication between components (or any part of your code). The question is how to leverage Angular capabilities, not CoreUI in particular.

@xidedix The coreui toaster service cannot be used for this?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions