import { GenericError, IGenericError } from "./GenericError";

export interface IDomainError extends IGenericError {}

function _isJoiError(error: Error) {
  return error.name === "ValidationError";
}

export class DomainError extends GenericError implements IDomainError {
  public static readonly INVALID_INPUT_DATA = "INVALID_INPUT_DATA";

  public static create(
    code: string,
    message: string,
    payload?: Record<string, any>
  ): DomainError {
    return new DomainError(code, message, payload);
  }
}

export function handleDomainError2(
  code: string,
  error: Error,
  payload?: Record<string, any>
): DomainError {
  if (_isJoiError(error)) {
    // ??
  }

  return DomainError.create(code, error.message, payload);
}

export function handleDomainError(
  code: string,
  message: string = "",
  payload?: Record<string, any>
): DomainError {
  return DomainError.create(code, message, payload);
}
