src/util/callback.js
/**
 * Apply error-first callback with error if available.
 *
 * @param {function} callback - Error-first callback
 * @returns {function} - Response handler function
 */
Apply error-first callback with error if available.
@param {function} callback - Error-first callback
@returns {function} - Response handler function
Apply error - first callback with error if available . @param { function } callback - Error - first callback @returns { function } - Response handler function
function failure (callback) {
  return (err) => {
    if (callback) callback(err)
    if (!err) {
      throw new Error(`No error message available, err was: ${JSON.stringify(err)}`)
    }
    if (err.name && err.name.includes('Error')) {
      throw err
    }
    if (typeof err.ok !== 'undefined' && !err.ok) {
      return err.text().then((body) => Promise.reject(body))
    }
    if (typeof err === 'string') {
      try {
        err = JSON.parse(err)
      } catch (e) {
      }
    }
    throw new Error(err.message || JSON.stringify(err))
  }
}
function failure ( callback ) { return ( err ) => { if ( callback ) callback ( err ) if ( ! err ) { throw new Error ( ` ${ JSON . stringify ( err ) } ` ) } if ( err . name && err . name . includes ( 'Error' ) ) { throw err } if ( typeof err . ok !== 'undefined' && ! err . ok ) { return err . text ( ) . then ( ( body ) => Promise . reject ( body ) ) } if ( typeof err === 'string' ) { try { err = JSON . parse ( err ) } catch ( e ) { } } throw new Error ( err . message || JSON . stringify ( err ) ) } }
Apply error-first callback with error if available.
Apply error - first callback with error if available .
// Native Error? // If a Fetch API response, get the body text // Must use 'then' instead of an async function here, or a client-side // 'catch' will pass a rejected Promise instead of an error. // If it's text // The error text is not JSON // Throw a native Error to play nicer with error handling/retry libraries // Bonus: no need to check both e.message and e.errors