How to write class that simulating Gaxios error?

765 Views Asked by At

I am using google sdk to communicate with the admin api with TypeScript. Google sdk uses Gaxios to send requests. I would like to write error handling to methods from google api. I would like to check if the error is of the Gaxios error type, if so, I would throw the appropriate http error.

export const handleError = (error) => {

  ...

  // Handling errors from google sdk
  if(error instanceof  GaxiosError){
    return Handle(400, error.message)
  }

  return handle(500, "Unknown error");
};

The problem is that I can't write a class that will pass through instanceof, I don't want to install the entire Gaxios library to handle only google sdk errors.

Here is the code of Gaxios https://github.com/googleapis/gaxios/blob/master/src/common.ts

This is my code:

export interface GaxiosResponse<T = any> {
  config: any;
  data: T;
  status: number;
  statusText: string;
  headers: any;
  request: any;
}

export interface GaxiosOptions {}

export class GaxiosError<T = any> extends Error {
  code?: string;
  response?: GaxiosResponse<T>;
  config: GaxiosOptions;
  constructor(message: string, options: GaxiosOptions, response: GaxiosResponse<T>) {
    super(message);
    this.response = response;
    this.config = options;
    this.code = response.status.toString();
  }
}
0

There are 0 best solutions below