responseErrorHandler(response, endpoint) {

    // - - -
    // Check headers for more specific errors.

    let wwwAuth = response._headers['www-authenticate'];

    if (wwwAuth && wwwAuth.indexOf('insufficient_scope') !== -1) {
      return when.reject(new ResponseError(
        'Insufficient scopes provided for this call',
        response,
        endpoint));
    }

    // - - -
    // 404 - Page not found
    if (response._status === 404) {
      let msg = 'Page not found. Is this a valid endpoint?';
      return when.reject(new ResponseError(msg, response, endpoint));
    }

    // - - -
    // Access token has expired
    if (response._status === 401 && !this._userConfig.useBrowserCookies) {

      // Atempt to get a new access token!
      let reauthPromise = this.authenticate(endpoint);

      return reauthPromise.then(() => {
        // refresh the authentication headers for this endpoint
        endpoint.setHeaders(this.buildHeaders(endpoint.contextOptions));

        let modifiedEndpoint = new Endpoint(this._userConfig,
                                            endpoint.hostname,
                                            endpoint.method,
                                            endpoint.path,
                                            this.buildHeaders(
                                              endpoint.contextOptions),
                                            endpoint.givenArgs,
                                            endpoint.contextOptions,
                                            endpoint.port);

        return when.resolve(modifiedEndpoint);
      }).catch(error => {
        this.emit('access_token_expired');

        let msg = 'Access token has expired. Listen for ' +
                   'the "access_token_expired" event to ' +
                     'handle this gracefully in your app.';
        return when.reject(new ResponseError(msg, response, endpoint));
      });
    }

    // - - -
    // Access token has expired and we're trying to authenticate without OAuth
    if (response._status === 401 && !this._userConfig.useBrowserCookies) {
      let msg = 'Access token required to access this endpoint.';
      return when.reject(new ResponseError(msg, response, endpoint));
    }

    // - - -
    // Reddit servers are busy. Can't do much here.

    if (String(response._status).substring(0, 1) === '5') {
      let modifiedEndpoint = new Endpoint(this._userConfig,
                                          endpoint.hostname,
                                          endpoint.method,
                                          endpoint.path,
                                          this.buildHeaders(
                                            endpoint.contextOptions),
                                          endpoint.givenArgs,
                                          endpoint.contextOptions,
                                          endpoint.port);

      return when.resolve(modifiedEndpoint);
    }

    // - - -
    // At the end of the day, we just throw an error stating that there
    // is nothing we can do & give general advice
    return when.reject(new ResponseError(
      ('This call failed. ' +
       'Does this call require a user? ' +
       'Is the user missing reddit gold? ' +
       'Trying to change a subreddit that the user does not moderate? ' +
       'This is an unrecoverable error. Check the rest of the ' +
       'error message for more information.'),
      response,
      endpoint));
  }
/*
     Handle a request errors from reddit. This is usually caused when our
     access_token has expired, or reddit servers are under heavy load.

     If we can't renew our access token, we throw an error / emit the
     'access_token_expired' event that users can then handle to
     re-authenticatet clients

     If we can renew our access token, we try to reauthenticate, and call the
     reddit endpoint again.
   */
Handle a request errors from reddit. This is usually caused when our
access_token has expired, or reddit servers are under heavy load.

If we can't renew our access token, we throw an error / emit the
'access_token_expired' event that users can then handle to
re-authenticatet clients

If we can renew our access token, we try to reauthenticate, and call the
reddit endpoint again.
Handle a request errors from reddit . This is usually caused when our access_token has expired or reddit servers are under heavy load . If we can ' t renew our access token we throw an error / emit the ' access_token_expired ' event that users can then handle to re - authenticatet clients If we can renew our access token we try to reauthenticate and call the reddit endpoint again .
responseErrorHandler(response, endpoint) {
    let wwwAuth = response._headers['www-authenticate'];
    if (wwwAuth && wwwAuth.indexOf('insufficient_scope') !== -1) {
      return when.reject(new ResponseError(
        'Insufficient scopes provided for this call',
        response,
        endpoint));
    }
    if (response._status === 404) {
      let msg = 'Page not found. Is this a valid endpoint?';
      return when.reject(new ResponseError(msg, response, endpoint));
    }
    if (response._status === 401 && !this._userConfig.useBrowserCookies) {
      let reauthPromise = this.authenticate(endpoint);
      return reauthPromise.then(() => {
        endpoint.setHeaders(this.buildHeaders(endpoint.contextOptions));
        let modifiedEndpoint = new Endpoint(this._userConfig,
                                            endpoint.hostname,
                                            endpoint.method,
                                            endpoint.path,
                                            this.buildHeaders(
                                              endpoint.contextOptions),
                                            endpoint.givenArgs,
                                            endpoint.contextOptions,
                                            endpoint.port);
        return when.resolve(modifiedEndpoint);
      }).catch(error => {
        this.emit('access_token_expired');
        let msg = 'Access token has expired. Listen for ' +
                   'the "access_token_expired" event to ' +
                     'handle this gracefully in your app.';
        return when.reject(new ResponseError(msg, response, endpoint));
      });
    }
     and we're trying to authenticate without OAuth
    if (response._status === 401 && !this._userConfig.useBrowserCookies) {
      let msg = 'Access token required to access this endpoint.';
      return when.reject(new ResponseError(msg, response, endpoint));
    }
    if (String(response._status).substring(0, 1) === '5') {
      let modifiedEndpoint = new Endpoint(this._userConfig,
                                          endpoint.hostname,
                                          endpoint.method,
                                          endpoint.path,
                                          this.buildHeaders(
                                            endpoint.contextOptions),
                                          endpoint.givenArgs,
                                          endpoint.contextOptions,
                                          endpoint.port);
      return when.resolve(modifiedEndpoint);
    }
    return when.reject(new ResponseError(
      ('This call failed. ' +
       'Does this call require a user? ' +
       'Is the user missing reddit gold? ' +
       'Trying to change a subreddit that the user does not moderate? ' +
       'This is an unrecoverable error. Check the rest of the ' +
       'error message for more information.'),
      response,
      endpoint));
  }
responseErrorHandler ( response , endpoint ) { let wwwAuth = response . _headers [ 'www-authenticate' ] ; if ( wwwAuth && wwwAuth . indexOf ( 'insufficient_scope' ) !== - 1 ) { return when . reject ( new ResponseError ( 'Insufficient scopes provided for this call' , response , endpoint ) ) ; } if ( response . _status === 404 ) { let msg = 'Page not found. Is this a valid endpoint?' ; return when . reject ( new ResponseError ( msg , response , endpoint ) ) ; } if ( response . _status === 401 && ! this . _userConfig . useBrowserCookies ) { let reauthPromise = this . authenticate ( endpoint ) ; return reauthPromise . then ( ( ) => { endpoint . setHeaders ( this . buildHeaders ( endpoint . contextOptions ) ) ; let modifiedEndpoint = new Endpoint ( this . _userConfig , endpoint . hostname , endpoint . method , endpoint . path , this . buildHeaders ( endpoint . contextOptions ) , endpoint . givenArgs , endpoint . contextOptions , endpoint . port ) ; return when . resolve ( modifiedEndpoint ) ; } ) . catch ( error => { this . emit ( 'access_token_expired' ) ; let msg = 'Access token has expired. Listen for ' + 'the "access_token_expired" event to ' + 'handle this gracefully in your app.' ; return when . reject ( new ResponseError ( msg , response , endpoint ) ) ; } ) ; } if ( response . _status === 401 && ! this . _userConfig . useBrowserCookies ) { let msg = 'Access token required to access this endpoint.' ; return when . reject ( new ResponseError ( msg , response , endpoint ) ) ; } if ( String ( response . _status ) . substring ( 0 , 1 ) === '5' ) { let modifiedEndpoint = new Endpoint ( this . _userConfig , endpoint . hostname , endpoint . method , endpoint . path , this . buildHeaders ( endpoint . contextOptions ) , endpoint . givenArgs , endpoint . contextOptions , endpoint . port ) ; return when . resolve ( modifiedEndpoint ) ; } return when . reject ( new ResponseError ( ( 'This call failed. ' + 'Does this call require a user? ' + 'Is the user missing reddit gold? ' + 'Trying to change a subreddit that the user does not moderate? ' + 'This is an unrecoverable error. Check the rest of the ' + 'error message for more information.' ) , response , endpoint ) ) ; }
Handle a request errors from reddit.
Handle a request errors from reddit .
// - - - // Check headers for more specific errors. // - - - // 404 - Page not found // - - - // Access token has expired // Atempt to get a new access token! // refresh the authentication headers for this endpoint // - - - // Access token has expired and we're trying to authenticate without OAuth // - - - // Reddit servers are busy. Can't do much here. // - - - // At the end of the day, we just throw an error stating that there // is nothing we can do & give general advice