Rishikant181 / Rettiwt-API

A CLI tool and an API for fetching data from Twitter for free!
https://rishikant181.github.io/Rettiwt-API/
MIT License
305 stars 31 forks source link

Add ability to define custom error handling #414

Closed omarcinkonis closed 5 months ago

omarcinkonis commented 6 months ago

Current error handling implementation returns EHttpStatus or EApiErrors. This is a valid approach. However, package users may want to define additional error handling. For example:

Custom error handling would be beneficial when using multiple accounts/proxies. As is now, whenever an account or proxy breaks, all defined configs must be checked manually to determine the cause of the problem. Alternatively, it is possible to try/catch on each fetch, but this introduces a lot of boilerplate code.

The problem can be solved by letting the user define custom error handler. The handler may be passed to FetcherService via RettiwtConfig or via new RettwitErrorHandler optional parameter, like so:

  private errorHandler: RettiwtErrorHandler;

  // ...

  public constructor(config?: RettiwtConfig, errorHanlder?: RettiwtErrorHandler) {
    // ....

    this.errorHandler = errorHandler ?? this.getDefaultErrorHandler();
  }

Then interface and error handler can be defined:

class RettiwtErrorHandler implements IRettiwtErrorHandler {
  handleHttpError(res: AxiosResponse<IResponse<unknown>>): AxiosResponse<IResponse<unknown>> {
    // Custom handling logic for HTTP errors
    return res;
  }

  handleApiError(res: AxiosResponse<IResponse<unknown>>): AxiosResponse<IResponse<unknown>> {
    // Custom handling logic for API errors
    return res;
  }
}

@Rishikant181, what do you think?

Rishikant181 commented 6 months ago

@omarcinkonis Yeah I think this will be better. Currently I have my hands full, any chance you might work on this?

omarcinkonis commented 6 months ago

Sure, I'll do it.

Rishikant181 commented 6 months ago

@omarcinkonis Thanks!

omarcinkonis commented 5 months ago

Added in https://github.com/Rishikant181/Rettiwt-API/pull/418