简体   繁体   中英

Nestjs HttpService error handling with AxiosRequestConfig's validateStatus function

I need to handle http errors status code (such as 401, 500, etc) which can occur when consuming an external service using HttpService (HttpModule of Nestjs). Here is the implementation i am working on:

import { Injectable } from '@nestjs/common';
import { HttpService } from '@nestjs/axios';
import { Logger } from '@nestjs/common';
import { AxiosRequestConfig } from 'axios';
import { catchError, firstValueFrom, map } from 'rxjs';

type Person = {
  name: string;
  lastName: string;
};

@Injectable()
export class PersonService {
  constructor(private httpService: HttpService) {}
  async findPerson(): Promise<Person> {
    const axiosConfig: AxiosRequestConfig = {
      method: 'get',
      url: 'https://service.dns/path/person',
      headers: {
        'Content-Type': 'application/json',
        Authorization: `Bearer ${jwt}`,
      },
      validateStatus: function (status: number) {
        return status === 200;
      },
    };

    const personInstance: Person = await firstValueFrom(
      this.httpService.request(axiosConfig).pipe(
        catchError((e) => {
          Logger.error(e.response.data.errorMessage);
          throw new Error('internal communication error');
        }),
        map((res) => {
          return res.data;
        }),
      ),
    );
    return personInstance;
  }
}

In the code above, I just need the function catchError throws the custom error, but I am not able to make the function validateStatus to trigger the execution of catchError .

I have implemented next code in order to take advantage of validateStatus function of AxiosRequestConfig for giving solution to my needs:

import { Injectable } from '@nestjs/common';
import { HttpService } from '@nestjs/axios';
import { Logger } from '@nestjs/common';
import { AxiosRequestConfig } from 'axios';
import { firstValueFrom } from 'rxjs';

type Person = {
  name: string;
  lastName: string;
};

@Injectable()
export class PersonService {
  constructor(private httpService: HttpService) {}
  async findPerson(): Promise<Person> {
    const axiosConfig: AxiosRequestConfig = {
      method: 'get',
      url: 'https://service.dns/path/person',
      headers: {
        'Content-Type': 'application/json',
        Authorization: `Bearer fake_jwt`,
      },
      validateStatus: function (status: number) {
        return status === 200;
      },
    };

    return firstValueFrom(this.httpService.request(axiosConfig))
      .then((res) => res.data)
      .catch((e) => {
        Logger.error(e.errorMessage);
        throw new Error('internal communication error');
      });
  }
}

Note: this code deals with Promise<AxiosResponse<any>> instead of Observable<AxiosResponse<any> methods

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM