import { Context, NewTx } from '@coinweb/contract-kit';

import { ErrorHistoryItem } from '../../module/history/shared';

export class Exception extends Error {
  execute?: (context: Context) => NewTx[];
  historyItem: ErrorHistoryItem;

  constructor({ execute, ...historyItem }: ErrorHistoryItem & { execute?: (context: Context) => NewTx[] }) {
    const message = 'message' in historyItem ? historyItem['message'] : 'An unknown error has occurred';

    super(message);
    this.execute = execute;
    this.historyItem = historyItem;
    this.historyItem.links = historyItem.links ?? null;
  }
}
