/* eslint-disable max-classes-per-file */
export default class CustomError extends Error {
  code: string;

  details?: Record<string, unknown>;

  constructor(code = 'GENERIC', ...params: any[]) {
    super(...params);

    if (Error.captureStackTrace) {
      Error.captureStackTrace(this, CustomError);
    }

    this.code = code;
  }

  withDetails(details: Record<string, unknown>) {
    this.details = details;
    return this;
  }

  toJSON() {
    return {
      code: this.code,
      message: this.message,
      details: this.details,
    };
  }
}

/**
 * NonRetryableError - Used for errors that should not be retried
 * Typical cases: configuration errors, missing data, validation failures
 */
export class NonRetryableError extends CustomError {
  nonRetryable: boolean = true;

  constructor(code: string, message: string) {
    super(code, message);
    this.name = 'NonRetryableError';
  }
}
