src/utils.js
/**
 * Generate a new Error object with a reformatted error message which
 * is a little more informative and useful to users.
 *
 * @param {Error|string} err - The Error object the new error will be based on
 * @param {number} retryCount - For those functions which use retry. Number of
 *                              attempts to complete before giving up
 * @returns {Error} New error with custom error message
 */
Generate a new Error object with a reformatted error message which
is a little more informative and useful to users.

@param {Error|string} err - The Error object the new error will be based on
@param {number} retryCount - For those functions which use retry. Number of
attempts to complete before giving up
@returns {Error} New error with custom error message
Generate a new Error object with a reformatted error message which is a little more informative and useful to users . @param { Error|string } err - The Error object the new error will be based on @param { number } retryCount - For those functions which use retry . Number of attempts to complete before giving up @returns { Error } New error with custom error message
function formatError(
  err,
  name = 'sftp',
  eCode = errorCode.generic,
  retryCount
) {
  let msg = '';
  let code = '';
  let retry = retryCount
    ? ` after ${retryCount} ${retryCount > 1 ? 'attempts' : 'attempt'}`
    : '';
  if (err === undefined) {
    msg = `${name}: Undefined error - probably a bug!`;
    code = errorCode.generic;
  } else if (typeof err === 'string') {
    msg = `${name}: ${err}${retry}`;
    code = eCode;
  } else if (err.custom) {
    msg = `${name}->${err.message}${retry}`;
    code = err.code;
  } else {
    switch (err.code) {
      case 'ENOTFOUND':
        msg =
          `${name}: ${err.level} error. ` +
          `Address lookup failed for host ${err.hostname}${retry}`;
        break;
      case 'ECONNREFUSED':
        msg =
          `${name}: ${err.level} error. Remote host at ` +
          `${err.address} refused connection${retry}`;
        break;
      case 'ECONNRESET':
        msg =
          `${name}: Remote host has reset the connection: ` +
          `${err.message}${retry}`;
        break;
      case 'ENOENT':
        msg = `${name}: ${err.message}${retry}`;
        break;
      default:
        msg = `${name}: ${err.message}${retry}`;
    }
    code = err.code ? err.code : eCode;
  }
  let newError = new Error(msg);
  newError.code = code;
  newError.custom = true;
  return newError;
}
function formatError ( err , name = 'sftp' , eCode = errorCode . generic , retryCount ) { let msg = '' ; let code = '' ; let retry = retryCount ? ` ${ retryCount } ${ retryCount > 1 ? 'attempts' : 'attempt' } ` : '' ; if ( err === undefined ) { msg = ` ${ name } ` ; code = errorCode . generic ; } else if ( typeof err === 'string' ) { msg = ` ${ name } ${ err } ${ retry } ` ; code = eCode ; } else if ( err . custom ) { msg = ` ${ name } ${ err . message } ${ retry } ` ; code = err . code ; } else { switch ( err . code ) { case 'ENOTFOUND' : msg = ` ${ name } ${ err . level } ` + ` ${ err . hostname } ${ retry } ` ; break ; case 'ECONNREFUSED' : msg = ` ${ name } ${ err . level } ` + ` ${ err . address } ${ retry } ` ; break ; case 'ECONNRESET' : msg = ` ${ name } ` + ` ${ err . message } ${ retry } ` ; break ; case 'ENOENT' : msg = ` ${ name } ${ err . message } ${ retry } ` ; break ; default : msg = ` ${ name } ${ err . message } ${ retry } ` ; } code = err . code ? err . code : eCode ; } let newError = new Error ( msg ) ; newError . code = code ; newError . custom = true ; return newError ; }
Generate a new Error object with a reformatted error message which
 is a little more informative and useful to users.
Generate a new Error object with a reformatted error message which is a little more informative and useful to users .