Home Reference Source Repository

es6/Errors/index.js

import Extendable_Error from 'es6-error'

/**
 * @class JSON_Error
 * 
 * JSONable Error object for ease of capturing
 */
export class JSON_Error extends Extendable_Error {
  toJSON () {
    return {
        message : this.message
      , stack   : this.stack
      , type    : this.constructor.name
    }
  }
}

/**
 * @class Environment_Error
 * 
 * Thrown when an ENV variable that was required was not set
 */

export class Environment_Error extends JSON_Error {
  constructor (key) {
    super(`Could not find process.env.${key}, did you forget to set it?`)
  }
}

/**
 * @class Executable_Error
 * 
 * Thrown when an executable/bin is not found in the path, but is required
 */

export class Executable_Error extends JSON_Error {
  constructor (exe) {
    super(`Could not find ${exe}, did you forget to set install it?`)
  }
}

/**
 * @class Thread_Error
 * 
 * Thrown when an Error occurs with a Thread
 */
export class Thread_Error extends JSON_Error {
  constructor (msg = 'Created an instance of Thread in an unisolated process\nThis should never happen.') {
    super(msg)
  }
}

/**
 * @class Data_Integrity_Error
 * 
 * Thrown by data validation failure
 */
export class Data_Integrity_Error extends JSON_Error {
  constructor (msg = "this job is invalid") {
    super(msg)
  }
}

/**
 * @class Timeout_Error
 * 
 * Error specific to code taking too long to run
 */
export class Timeout_Error extends JSON_Error {
  constructor (msg = "Job took too long:\nEither increase the `max_run_time` or use `this.touch()` for a long running job") {
    super(msg)
  }
}

/**
 * @class Plugin_Error
 * 
 * Error specific to Wyst Plugins
 */
export class Plugin_Error extends JSON_Error {
  constructor (plugin) {
    super(`${plugin} tried to occupy a previously reserved space on the Wyst object `)
  }
}

/**
 * @class Lock_Not_Owned_Error
 * 
 * Error specific to Lock
 */
export class Lock_Not_Owned_Error extends JSON_Error {
  constructor () {
    super(`tried to release a lock not owned by this process`)
  }
}


/**
 * @class Lock_Not_Owned_Error
 * 
 * Error specific to Lock
 */
export class Lock_Lost_Error extends JSON_Error {
  constructor () {
    super(`tried to release a lock but it had already expired`)
  }
}


/**
 * @class Executable_Runtime_Error
 * 
 * Error specific to child_process.exec
 */
export class Executable_Runtime_Error extends JSON_Error {
  constructor (msg) {
    super(msg)
  }
}

/**
 * captures a stacktrace at the current code execution point
 * 
 * @function captureStack
 */
export function captureStack () {
  return (new Error()).stack
}