All files / advanced-spawn-async/lib/classes/errors/termination-error index.ts

100% Statements 10/10
100% Branches 1/1
100% Functions 3/3
100% Lines 10/10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40  1x                       3x 3x 3x       6x                   3x   3x 3x   3x       1x  
import { IsomorphicSpawn, SpawnFactory } from '../../../types'
import SpawnError from '../spawn-error'
 
class TerminationError<
  Process extends IsomorphicSpawn.Return,
  TerminationInformation extends SpawnFactory.TerminationInformation<Process>
> extends SpawnError {
  readonly info: TerminationInformation
 
  constructor (
    info: TerminationInformation,
    message: (info: TerminationInformation) => string = TerminationError.DEFAULT_MESSAGE
  ) {
    super(message(info))
    this.name = this.getName()
    this.info = info
  }
 
  protected getName () {
    return 'SpawnError'
  }
 
  static DEFAULT_MESSAGE (info: SpawnFactory.TerminationInformation<IsomorphicSpawn.Return>) {
    const {
      command,
      args,
      stderr,
      status,
      signal
    } = info
 
    const cli = JSON.stringify([command, ...args])
    const code = JSON.stringify({ status, signal })
 
    return `Failed to execute ${cli} (${code}) ${stderr}`
  }
}
 
export = TerminationError