'use strict';

/**
 * Process control signal which may be configured with instructions describing how to treat an error from the process
 */
class ControlSignal extends Error {

  /** Cause error */
  public cause?: Error;

  /**
   * Constructs instance
   * @param options options
   */
  constructor(public options?: ControlSignal.Options) {
    super(options?.message || 'Process control signal');
    this.cause = options?.error;
  }
}

namespace ControlSignal {

  /** Constructing options */
  export type Options = {
    /** Error the process failed with */
    error?: Error;
    /** Overriden message */
    message?: string;
    /**
     * What to do with the process
     * - `cancel` means the process will be canceled, but only if it was not rescheduled with new options
     * - `failover` means a some problem arised and the process should be failovered with throttle delay
     * - `stop` means the process should gracefully stop, and restart without throttle delays if it is still scheduled
     * @default "failover"
     */
    action?: 'cancel' | 'failover' | 'stop';
    /**
     * Overriden log message severity. Default behaviour:
     * - `error` when `error` field is specified
     * - `warn` otherwise, when the action is `failover`
     * - `info` otherwise
     */
    severity?: 'error' | 'warn' | 'info' | 'debug';
  };
}

export default ControlSignal;
