/**
 * Error class meant to be returned by integrations in case of exceptions. These errors will be caught and handled
 * appropriately. Any other error would result in an unhandled server error accompanied by a 500 status code.
 *
 * @field message - The error message
 * @field status - The HTTP status code to return
 */
export class HttpError extends Error {
  readonly status: number;

  constructor(message: string, status: number) {
    super(message);
    this.status = status;
    this.name = this.constructor.name;
  }
}

/**
 * Used to generate a 400 Bad Request. Usually used when something is missing to properly handle the request.
 */
export class BadRequestError extends HttpError {
  constructor(message?: string) {
    super(message || 'Bad request', 400);
  }
}

/**
 * Used to generate a 401 Unauthorized. Usually used when the credentials are missing or invalid.
 */
export class UnauthorizedError extends HttpError {
  constructor(message?: string) {
    super(message || 'Unauthorized', 401);
  }
}

/**
 * Used to generate a 403 Forbidden. Usually used when user lacks sufficient permission to access a ressource.
 */
export class ForbiddenError extends HttpError {
  constructor(message?: string) {
    super(message || 'Forbidden', 403);
  }
}

/**
 * Used to generate a 404 Not Found. Usually used when the requested `Item` is not found.
 */
export class NotFoundError extends HttpError {
  constructor(message?: string) {
    super(message || 'Not found', 404);
  }
}

/**
 * Used to generate a 408 Timeout Error. Usually used when the call length exceeds the received Operation Deadline.
 */
export class TimeoutError extends HttpError {
  constructor(message?: string) {
    super(message || 'Not found', 408);
  }
}

/**
 * Used to generate a 410 Resource Gone.
 *
 * @deprecated Use ProviderInstanceLockedError instead when the provider instance is locked or unavailable.
 */
export class ResourceGoneError extends HttpError {
  constructor(message?: string) {
    super(message || 'Resource Gone', 410);
  }
}

/**
 * Used to generate a 422 Unprocessable Entity. Usually used when an operation is invalid.
 */
export class UnprocessableEntityError extends HttpError {
  constructor(message?: string) {
    super(message || 'Unprocessable Entity', 422);
  }
}

/**
 * Used to generate a 423 Provider Instance Locked.
 */
export class ProviderInstanceLockedError extends HttpError {
  constructor(message?: string) {
    super(message || 'Provider instance locked or unavailable', 423);
  }
}

/**
 * Used to generate a 429 Rate Limit Exceeded. Usually used when an operation triggers or would trigger a rate limit
 * error on the provider's side.
 */
export class RateLimitExceededError extends HttpError {
  constructor(message?: string) {
    super(message || 'Rate Limit Exceeded', 429);
  }
}
