import { DatabaseError } from "sequelize";
import { InfrastructureError } from "./infrastructure-errors";

export class InfrastructureRepositoryError extends InfrastructureError {
  public readonly code = "REPOSITORY_ERROR" as const;
  constructor(message = "Repository operation failed", options?: ErrorOptions) {
    const { cause } = options || {};
    let _message = message;

    if (cause && cause instanceof DatabaseError) {
      _message = `${message}: ${(cause as DatabaseError).message}`;
    }

    super(_message, options);
  }
}

export const isInfrastructureRepositoryError = (e: unknown): e is InfrastructureRepositoryError =>
  e instanceof InfrastructureRepositoryError;
