Daniel
Daniel

Reputation: 311

How to create a log file when HTTP error response occurs in Angular 10?

I have a simple Angular project where the user can record information about their vehicle such as the identification number, model, etc. I want to create and save text file with message in project folder when 500 error occurs during HTTP event.

Code from the service:

export class ErrorInterceptorService implements HttpInterceptor {
      
      constructor(private router: Router) { }
      
      intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
            return next.handle(req).pipe(retry(environment.REINTENTOS)).pipe(tap(event => {
            if (event instanceof HttpResponse) {
                if (event.body && event.body.error === true && event.body.errorMessage){
                throw new Error(event.body.errorMessage);
            }
          }
       })).pipe(catchError((err) => {

            const str = err.error.message;

            if (err.error.status === 400){
                this.openSnackBar(str.slice(4, str.length));
            } else if (err.error.status === 500) {
                // create the log with the message and save into the folder "logs"

                this.router.navigate(['/error500']);
            }
            return EMPTY;
       }));
    }  
}

Any help is appreciated.

Upvotes: 0

Views: 672

Answers (1)

JDillon522
JDillon522

Reputation: 19686

It sounds like what you want to do is create a log entry in the project directory on the server that the app is served from.

In that case, my first thought is that you need to make an additional request to an api endpoint that will write the log on the server in your logs folder. The implementation of that will vary depending on your api.

Upvotes: 1

Related Questions