0

This code fetches categories and give them to controller.

sampleApp.factory('SCService', function($http, $q) {

    var SuperCategories = [];
    var SCService =  {};

    SCService.GetSuperCategories = function() {
        var req = {
            method: 'POST',
            url: SuperCategoryURL,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            data: "action=GET"
        };
        if ( SuperCategories.length == 0 ) {
            return $http(req).then(function (response) {
                SuperCategories = response.data;
                return SuperCategories;
            });
        }else {
            return $q.when(SuperCategories);
        }
    }

    return SCService;
});

I think code is perfect until there is no error in http request.

My query is how to do error handling (try catch or something like that), in case if server have some issue or may be cgi-script have some issue and not able to server the request.

4 Answers 4

1

Angular promises use a method catch for that.

return $http(req).then(function (response) {
    SuperCategories = response.data;
    return SuperCategories;
}).catch(function(error) {
    // Do what you want here
});

You should use also finally :

return $http(req).then(function (response) {
    SuperCategories = response.data;
    return SuperCategories;
}).catch(function(error) {
    // Do what you want here
}).finally(function() {
    // Always executed. Clean up variables, call a callback, etc...
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the response. Just wanted to understand which is good option? Using try catch or success/error call backs?
Keep in mind that a "classic" try {} cath(...) {} will have a different behavior with the $exceptionHandler. Read this article : medium.com/@bluepnume/…
1

Write like

 return $http(req).then(function (response) {
               //success callback
            },
 function(){
//Failure callback
});

Comments

0

Use callback methods from controller Like

Controller.js

service.GetSuperCategories(function (data) {console.log('success'},function (error){console.log('error'});

service.js

sampleApp.factory('SCService', function($http, $q) {

    var SuperCategories = [];
    var SCService =  {};

    SCService.GetSuperCategories = function(successMethod,errorMethod) {
        var req = {
            method: 'POST',
            url: SuperCategoryURL,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            data: "action=GET"
        };
        return $http(req).then(successMethod(data),
errorMethod(error));
    }

    return SCService;
});

Comments

0

You can use the .success and .error methods of $http service, as below

$http(req).success(function(data, status, headers){
    // success callback:  Enters if status = 200
}).error(function(status, headers){
    // error callback: enters otherwise
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.