coreui / coreui-angular

CoreUI Components Library for Angular https://coreui.io/angular/docs/
https://coreui.io/angular/
MIT License
244 stars 145 forks source link

send toast inside interceptor #158

Closed 32x0lf closed 1 year ago

32x0lf commented 1 year ago

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;
      })
    );
  }
32x0lf commented 1 year ago

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

32x0lf commented 1 year ago

Hello any update on this?

xidedix commented 1 year ago

@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.

32x0lf commented 1 year ago

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

github-actions[bot] commented 1 year ago

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