正确处理$http调用AngularJS中的错误

时间:2022-11-29 14:17:20

Is there a good way to catch errors in Angular regarding connection problems?

是否有一个很好的方法来捕获角上关于连接问题的错误?

Service $http POST:

服务http POST美元:

    dossierService.getDossier(uid)

    .then(function (result) {

        vm.dossier = result.dossier;

    }, function (error) {

        handleError(error)

    });//end dossierService.getDossier(uid)

If there is a connecting problem it will return an error:

如果存在连接问题,则返回一个错误:

POST http://192.168.25.1:8080/api/query net::ERR_CONNECTION_TIMED_OUT

How can I show the correct message to the user letting him/her know what the exact error is?

如何向用户显示正确的消息,让用户知道确切的错误是什么?

2 个解决方案

#1


0  

$http.post("/path", {data:"data"}).then(function(response){
    // do something with success response
}).catch(function(error) {

  console.log(JSON.stringify(error));
  $scope.myerror=error

});

and in your view ...

在你看来……

<div ng-if="myerror!=null">
    {{myerror}} OR {{myerror.message}}
</div>

#2


0  

You can create an object with the http response codes and link a message. To throw these errors you can use an interceptor.

您可以使用http响应代码创建一个对象并链接消息。要抛出这些错误,可以使用拦截器。

moduleError.factory('HttpErrorHandler', function (){
    return {
        401: 'You are not logged in',
        403: 'You do not have permissions'
    }
});



$httpProvider.interceptors.push(function($q, HttpErrorHandler) {
  return {
   'responseError': function(rejection) {
      console.log(HttpErrorHandler[rejection.status]);
  };
});

If the error is related to this operations, launch errors where you are launching. You can throw the code 400 for your errors with a object in the body like:

如果错误与此操作相关,则在您要启动的地方启动错误。您可以将代码400用于您的错误,对象在主体中如下:

{
   code: 30002, //Thats your custom code
   message: 'My custom error message'
}

.

dossierService.getDossier(uid)

.then(function (result) {

    vm.dossier = result.dossier;

}, function (error) {

    if(error.status == 400){
      var my_error = JSON.parse(error.data);
      console.log(my_error.message);
    }

});

I think this is a good aproach.

我认为这是个好主意。

#1


0  

$http.post("/path", {data:"data"}).then(function(response){
    // do something with success response
}).catch(function(error) {

  console.log(JSON.stringify(error));
  $scope.myerror=error

});

and in your view ...

在你看来……

<div ng-if="myerror!=null">
    {{myerror}} OR {{myerror.message}}
</div>

#2


0  

You can create an object with the http response codes and link a message. To throw these errors you can use an interceptor.

您可以使用http响应代码创建一个对象并链接消息。要抛出这些错误,可以使用拦截器。

moduleError.factory('HttpErrorHandler', function (){
    return {
        401: 'You are not logged in',
        403: 'You do not have permissions'
    }
});



$httpProvider.interceptors.push(function($q, HttpErrorHandler) {
  return {
   'responseError': function(rejection) {
      console.log(HttpErrorHandler[rejection.status]);
  };
});

If the error is related to this operations, launch errors where you are launching. You can throw the code 400 for your errors with a object in the body like:

如果错误与此操作相关,则在您要启动的地方启动错误。您可以将代码400用于您的错误,对象在主体中如下:

{
   code: 30002, //Thats your custom code
   message: 'My custom error message'
}

.

dossierService.getDossier(uid)

.then(function (result) {

    vm.dossier = result.dossier;

}, function (error) {

    if(error.status == 400){
      var my_error = JSON.parse(error.data);
      console.log(my_error.message);
    }

});

I think this is a good aproach.

我认为这是个好主意。