javascript - त्रुटि स्थिति कोड पकड़े नहीं जा रहे हैं
angularjs ionic-framework (2)
पहला। .catch
पूरा होने के लिए अस्वीकार कर रहा है। रूपांतरण को रोकने के लिए, .catch
विधि को त्रुटि को throw
की आवश्यकता है।
authenticationResponse.$promise.catch(function(error){
alert('catched error!!!');
//throw to chain error
throw error;
}).then(function(result) {
// this is always fired, even then response code is 400 or 503 :(
console.log(result);
console.log('success!');
//return to chain data
return result
}, function(error){
// This should be executed when status code is different then 200?
// its never executed at all :(
console.log('error!');
//throw to chain rejection
throw error;
});
जब फ़ंक्शन किसी return
या throw
स्टेटमेंट को छोड़ देता है, तो यह undefined
रिटर्न देता है। $q
सेवा एक व्युत्पन्न वादा बनाती है जो undefined
करने के लिए हल करता है।
ngResource
समस्याएं निदान
$resource
विधियों के साथ समस्याओं का पता लगाने के लिए, प्रतिक्रिया इंटरसेप्टर जोड़ें:
userDevices: $resource(api_url+'/requestRegistration/userDevices/:action', {}, {
registerDevice: {
method: 'POST',
params: {
action: ''
},
interceptor: {
response: function (response) {
console.log("registerDevice success");
console.log(response.status);
return response;
},
errorResponse: function (errorResponse) {
console.log("registerDevice error");
console.log(errorResponse.status);
throw errorResponse;
}
}
},
verify: {
method: 'POST',
अन्य बातों को देखने के लिए अन्य $ HTTP इंटरसेप्टर ऐप कनवर्टिंग प्रतिक्रियाओं में एक फेंक स्टेटमेंट को छोड़कर।
मैं कोणीय संसाधन के HTTP त्रुटि स्थिति कोड (! = 200) को पकड़ने की कोशिश कर रहा हूं। मेरी सेवा, जहां मेरे पास संसाधन हैं: (apiService.js)
.factory('ApiService', function($resource, $http, localStorageService, CONFIG) {
var base_api_url = api_url = CONFIG.api_url, api_version_prefix = CONFIG.api_version_prefix;
return {
userDevices: $resource(api_url+'/requestRegistration/userDevices/:action', {}, {
registerDevice: {
method: 'POST',
params: {
action: ''
}
},
verify: {
method: 'POST',
params: {
action: 'verify'
}
},
}
}
});
मेरा नियंत्रक का कोड:
.controller('LoginCtrl', function(CONFIG, $scope, $state, $ionicPlatform, $ionicPopup, ApiService) {
$scope.data = {
username: null
};
$scope.registerDevice = function() {
if($scope.data.username) {
var authenticationResponse = ApiService.userDevices.registerDevice({
username: $scope.data.username
});
authenticationResponse.$promise.then(function(result) {
// this is always fired, even then response code is 400 or 503 :( I am not able to check response status code.
console.log(result);
console.log('success!');
}, function(error){
// this code is not being exectued even when response status code is different then 200
// its never executed at all :(
console.log('error!');
});
}
};
});
जब मैं अनुरोध भेजता हूं और मुझे प्रतिसाद कोड 400/503 मिलता है, तो मुझे विश्वास है कि फ़ंक्शन (त्रुटि) कोड निष्पादित किया जाना चाहिए, लेकिन ऐसा नहीं है।
इसके बजाय, $promise.then(function(result)(...)
code में मेरे कोड। तब $promise.then(function(result)(...)
निष्पादित किया जाता है और मैं प्रतिक्रिया HTTP स्थिति कोड का पता लगाने में सक्षम नहीं हूं।
तो, मेरे प्रश्न:
- मेरी त्रुटि को फ़ंक्शन चलाने में क्यों नहीं किया जा रहा है?
- मैं HTTP प्रतिक्रिया स्थिति कोड कैसे पहचान सकता हूं?
आप एक इंटरसेप्टर का उपयोग कर सकते हैं
इन अनुरोधों को शुरू करने वाले आवेदन कोड को सौंपे जाने से पहले सर्वर और प्रतिक्रियाओं को सौंपे जाने से पहले उन्हें रोकें अनुरोध
इसलिए, यह $ HTTP से होने वाली सभी प्रतिक्रिया त्रुटियों को प्राप्त करेगा जो कि $ संसाधन है।
$httpProvider.interceptors.push(function($q) {
return {
'responseError': function(response) {
if (response.status == 400) {
// Handle 400 error code
}
if (response.status == 503) {
// Handle 503 error code
}
// Rejects the derived promise.
return $q.reject(response);
}
};
});