1
resposta

Silenciar erros de requisição do HttpClient

É possível silenciar o erro, no caso o 401, mas qualquer erro de forma que ele não apareça no console?

return this.http
      .post(
        `${API_URL}login`,
        login,
        { responseType: 'text' }
      )
      .pipe(tap(authToken => this.userService.setToken(authToken)))
`

estou recebendo o dessa forma:

this.authService
            .authenticate({ email, password })     
            .subscribe(
                (authToken) => {
                    this.userService.setToken(authToken);
                    this.router.navigate(['system/dashboard']);
                }
                , err => {
                    if (err instanceof HttpErrorResponse) {
                        if (err.status === 401) {
                            this.loginForm.reset();

                            this.platformDetectorService.isPlatformBrowser() &&
                                this.emailInput.nativeElement.focus();

                            this.alertService.warning('Usuário ou senha inválidos');
                        }
                        else {
                            throwError(err);
                        }
                    }
                }
            )

Eu estou tratando o erro, no entanto ele continua estourando na tela do console.

1 resposta

Acredito que o melhor seria tratar os casos que são relevantes e deixar outros erros estourar no console. Os mais comuns:

400 Bad Request — Client sent an invalid request — such as lacking required request body or parameter
401 Unauthorized — Client failed to authenticate with the server
403 Forbidden — Client authenticated but does not have permission to access the requested resource
404 Not Found — The requested resource does not exist
412 Precondition Failed — One or more conditions in the request header fields evaluated to false
500 Internal Server Error — A generic error occurred on the server
503 Service Unavailable — The requested service is not available

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software