import Failure from "core/failure";
import {
  BaseFailureHandler,
  ResponseFailureHandler,
} from "./responseFailureHandler";

class NotFoundFailureHandler
  extends BaseFailureHandler
  implements ResponseFailureHandler
{
  private callback: Function;
  constructor(callback: Function, nextHandler?: ResponseFailureHandler) {
    super(nextHandler);
    this.callback = callback;
  }

  handle(reason: Failure): void {
    if (reason.meta && reason.meta.isRequestFailure && reason.status === 404) {
      this.callback(reason);
      return;
    }

    return super.handle(reason);
  }
}

export default NotFoundFailureHandler;
