简体   繁体   中英

How should angular services convey exceptions to controllers?

I have a service which is basically a wrapper around some CRUD type operations.

app.service('fooService', function ($http) {

    this.getfoo = function(id) {
        return $http.get('api/foo/Get/' + id).then(function (result) {
            return result.data;
        });
    };

    this.getfoos = function () {
        return $http.get('api/foo/Get').then(function (result) {
            return result.data;
        });
    };

    this.createfoo = function(foo) {
        return $http.post('api/foo/Create', foo).then(function(result) {
            return result;
        });
    };

    this.updatefoo = function(id, foo) {
        return $http.put('api/foo/Update/' + foo.id, foo).then(function (result) {
            return result.status;
        });
    };

    this.deletefoo = function(id) {
        return $http.delete('api/foo/Delete/' + id).then(function (result) {
            return result.status;
        });
    };
});

What is the best way to handle exceptions? Is there a nice unit testable way to do this? I am guessing that the controller using this service shouldn't be aware that it is AJAX calls. Does that mean I need some standard way of communicating with services that act as datastores? Some object that I return with a success indicator and a message? Eg

var tmp = {
    success: false,
    error: 13,
    message: 'This foo already exists'
}

$http.delete('api/foo/Get/' + id) returns promise therefore we can use Error handler like:

app.service('fooService', function ($http) {

 var tmp = {
           success: false,
           error: -1,
           message: ''
           }

    this.getfoo = function(id) {
        return $http.get('api/foo/Get/' + id).then(function (result) {
            return result.data;
        }, function (result) {
            tmp.message = "blablblab";
            tmp.error = result.status;
            return tmp;
        });
    };
    ....
});

Demo Fiddle

To start with your service methods can return promise object. Since a promise get resolved to a success or failure you would know when action completed and when it failed. Remember promise can used for any call of async nature. They are not tied to AJAX call only.

As a first step your service methods should return the promise directly when using $http service.

 this.getfoo = function(id) {
        return $http.get('api/foo/Get/' + id);
    };

In your case you are returning then function return value. As the documentation mentions

This method returns a new promise which is resolved or rejected via the return value of the successCallback, errorCallback. It also notifies via the return value of the notifyCallback method. The promise can not be resolved or rejected from the notifyCallback method.

you are suppressing the error that you get from server, because you did not add a error callback function. So better do return $http.get(...) . This way you can handle the success and error case in the controller or any other place like

If you want to wrap the errors into specific object you can create you own promise in the service and return custom on promise rejection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM