0

I have following code:

CaseFolderResource
 .exportByCaseId()
 .getWithGenericResponse(
    {caseId : request.caseId}, 
    function (result) {
      console.log(result);
      console.log('in 4')
    }, 
    ErrorHandler.handleError
  )
 .$promise
 .then(
   function (success) {
     console.log(success);
     console.log('in 2');
     console.log('success');

     CaseFolderResource
     .getByCaseId()
     .getWithGenericResponse(
       {caseId : request.caseId}, 
       function (result) {
         console.log('nested call');
         console.log(result);
       }
     );
   },  
   ErrorHandler.handleError
 );

In the console log i see in then in 2 and then success even the output was a failure so promise logs: data with success:false.

I get HTTP 500 on first and second promise.

Edit:

getWithGenericResponse is:

        getWithGenericResponse: {
            method: 'GET',
            interceptor: genericResponseInterceptor,
            headers: {
                'Authorization': function () {
                    return AuthHolder.getAuthData();
                },
                'X-Company': function(){
                    return getCompany();
                }
            }
        },

genericResponseInterceptor is:

var genericResponseInterceptor = {
    response: function (data) {
        var response = data.data;
        console.log(response);
        if (data.status === 401 || data.status === 403) {
            reject();
        } else if (!response.success && response.errors !== undefined && response.errors !== null) {
            response.errors.forEach(function (error) {
                console.log(error.code);
                if (error.message) {
                    ToasterService.showTranslatedWarningToasterInstant(error.message);
                } else {
                    ToasterService.showTranslatedWarningToaster('error.' + error.code, {params: error.args});
                }
            });
        }
        return response;
    },
    responseError: function (data) {
        if (data.status === 401) {
            reject();
        } else if (data.status === 403) {
            ToasterService.showTranslatedErrorToaster('error.message.403');
        } else {
            ToasterService.showTranslatedErrorToaster('error.message.default');
        }
        return data;
    }
};

Even if i delete: ,ErrorHandler.handleError from the first request - the result is the same as described above.

Edit:

I can resolve it with if (response.data.success) :

CaseFolderResource.exportByCaseId().getWithGenericResponse({caseId : request.caseId}).$promise
                    .then(function (response) {
                        console.log(response.data);
                        console.log(response.success);
                        if (response.data.success) {
                            console.log('in 2');
                            console.log('success');
                            CaseFolderResource.getByCaseId().getWithGenericResponse({caseId : request.caseId}, function (result) {
                                console.log('nested call');
                                console.log(result);
                            });
                        }
                        });
tryingHard
  • 1,794
  • 4
  • 35
  • 74
  • Don't pass the `ErrorHandler.handleError` in the first callback? – Bergi Jan 30 '19 at 16:39
  • What exactly is `getWithGenericResponse`, can you post/link its code? Looks like it should not accept callbacks put rather return a promise. – Bergi Jan 30 '19 at 16:40
  • I think that even if i removed handle error from first promise the result was the same. I will edit the question with code you are asking tommorow. The method you are asking for is for parameters for API. I mean path parameters. It adds this param to the path of the request. – tryingHard Jan 30 '19 at 16:45
  • If my suspicion is correct and the first callbacks go into a `then` internally, then yes [that changes the result](https://stackoverflow.com/questions/16371129/chained-promises-not-passing-on-rejection) – Bergi Jan 30 '19 at 16:47
  • @Bergi I've added the code you where asking for – tryingHard Jan 31 '19 at 08:17
  • I resolved it with ` if (response.data.success)` but i kind of don't like that i can not chain `promises` here – tryingHard Jan 31 '19 at 09:03
  • The `getWithGenericResponse` you posted doesn't look like a function you could call with an object and two callbacks? – Bergi Jan 31 '19 at 10:16
  • As I wrote before this methods adds `param` to query (https://en.wikipedia.org/wiki/Query_string). So it's usage is one request per one `getWithGenericResponse` – tryingHard Jan 31 '19 at 10:33

0 Answers0