index.js
function applyCreate (err, message, code, metadata) {
  if (err instanceof Error === false) {
    throw new Error('Source error must be an instance of Error')
  }

  if (message instanceof Error) {
    err.message = message.message.toString()
    if (typeof message.code === 'number') {
      err.code = message.code
    }
    if (typeof message.metadata === 'object') {
      err.metadata = createMetadata(message.metadata)
    }
  }

  const isMsgMD = message instanceof Error
  if (message && typeof message === 'object' && !isMsgMD) {
    metadata = message
    message = ''
    code = null
  }

  if (typeof message === 'number') {
    metadata = code
    code = message
    message = ''
  }

  if (code && typeof code === 'object') {
    metadata = code
    code = null
  }

  if (!metadata) {
    metadata = null
  }

  if (typeof message === 'string') {
    err.message = message
  }

  if (typeof code === 'number') {
    err.code = code
  }

  if (metadata && typeof metadata === 'object') {
    if (err.metadata) {
      const existingMeta = err.metadata && typeof err.metadata.getMap === 'function'
        ? err.metadata.getMap()
        : null

      const md = typeof metadata.getMap === 'function'
        ? metadata.getMap()
        : metadata

      if (existingMeta || md) {
        err.metadata = createMetadata(Object.assign({}, existingMeta || {}, md || {}))
      }
    } else {
      err.metadata = createMetadata(metadata)
    }
  }

  return err
}
function applyCreate (err, message, code, metadata) {
  if (err instanceof Error === false) {
    throw new Error('Source error must be an instance of Error')
  }

  if (message instanceof Error) {
    err.message = message.message.toString()
    if (typeof message.code === 'number') {
      err.code = message.code
    }
    if (typeof message.metadata === 'object') {
      err.metadata = createMetadata(message.metadata)
    }
  }

  const isMsgMD = message instanceof Error
  if (message && typeof message === 'object' && !isMsgMD) {
    metadata = message
    message = ''
    code = null
  }

  if (typeof message === 'number') {
    metadata = code
    code = message
    message = ''
  }

  if (code && typeof code === 'object') {
    metadata = code
    code = null
  }

  if (!metadata) {
    metadata = null
  }

  if (typeof message === 'string') {
    err.message = message
  }

  if (typeof code === 'number') {
    err.code = code
  }

  if (metadata && typeof metadata === 'object') {
    if (err.metadata) {
      const existingMeta = err.metadata && typeof err.metadata.getMap === 'function'
        ? err.metadata.getMap()
        : null

      const md = typeof metadata.getMap === 'function'
        ? metadata.getMap()
        : metadata

      if (existingMeta || md) {
        err.metadata = createMetadata(Object.assign({}, existingMeta || {}, md || {}))
      }
    } else {
      err.metadata = createMetadata(metadata)
    }
  }

  return err
}
function applyCreate ( err , message , code , metadata ) { if ( err instanceof Error === false ) { throw new Error ( 'Source error must be an instance of Error' ) } if ( message instanceof Error ) { err . message = message . message . toString ( ) if ( typeof message . code === 'number' ) { err . code = message . code } if ( typeof message . metadata === 'object' ) { err . metadata = createMetadata ( message . metadata ) } } const isMsgMD = message instanceof Error if ( message && typeof message === 'object' && ! isMsgMD ) { metadata = message message = '' code = null } if ( typeof message === 'number' ) { metadata = code code = message message = '' } if ( code && typeof code === 'object' ) { metadata = code code = null } if ( ! metadata ) { metadata = null } if ( typeof message === 'string' ) { err . message = message } if ( typeof code === 'number' ) { err . code = code } if ( metadata && typeof metadata === 'object' ) { if ( err . metadata ) { const existingMeta = err . metadata && typeof err . metadata . getMap === 'function' ? err . metadata . getMap ( ) : null const md = typeof metadata . getMap === 'function' ? metadata . getMap ( ) : metadata if ( existingMeta || md ) { err . metadata = createMetadata ( Object . assign ( { } , existingMeta || { } , md || { } ) ) } } else { err . metadata = createMetadata ( metadata ) } } return err }
Actual function that does all the work.
Same as createGRPCError but applies cretion to the existing error.
@param  {Error} err The error to apply creation to
@param  {String|Number|Error|Object} message See <code>createGRPCError</code> description
@param  {Number|Object} code See <code>createGRPCError</code> description
@param  {Object} metadata See <code>createGRPCError</code> description
@return {Error} See <code>createGRPCError</code> description
Actual function that does all the work . Same as createGRPCError but applies cretion to the existing error .