如何在Angular中捕获http错误响应

时间:2022-10-07 08:30:47

Background

背景

  • My Angular application communicates with a database through a set of API endpoints.
  • 我的Angular应用程序通过一组API端点与数据库通信。
  • "user/authenticate" endpoints accepts username and password and returns a success (200) or a bad request (400) http response.
  • “user / authenticate”端点接受用户名和密码,并返回成功(200)或错误请求(400)http响应。

Controller

调节器

authService.login(email, password)
    .success(function (response) {
        /* go to application */
    })
    .error(function (response) {
        /* display proper error message */
    });

auth Service

身份验证服务

factory.login = function(email, password) {
    return $http({
        method: 'POST',
        url: "http://myserver/user/authenticate",
        data: $.param({email: email, password: password}),
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    });
}

Problem

问题

When username and password fails and API returns 400 response, even though I'm catching the error and I'm displaying a proper message to user, the error appears in browser console.

当用户名和密码失败并且API返回400响应时,即使我正在捕获错误并且我向用户显示正确的消息,该错误也会出现在浏览器控制台中。

POST http://myserver/user/authenticate 400 (Bad Request)

Can I handle errors in a better way?

我能以更好的方式处理错误吗?

1 个解决方案

#1


4  

Try this

尝试这个

factory.login = function(email, password) {
    return $http({
        method: 'POST',
        url: "http://myserver/user/authenticate",
        data: $.param({email: email, password: password}),
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    }).then(function (response) {
       /**Check here your status code 400 if you found that status code is 400 the reject the promise**/
            if (statuscode !==400) {
                        return response.data;
                    } else {
                        // invalid response
                        return $q.reject(response.data);
                    }

        }, function (response) {
            // something went wrong
            return $q.reject(response);
        });
}

and then use following code

然后使用以下代码

 authService.login(email, password)
        .then(function (response) {
            /* go to application */
        },function(error) {
                console.log(error); /* catch 400  Error here */
    });

#1


4  

Try this

尝试这个

factory.login = function(email, password) {
    return $http({
        method: 'POST',
        url: "http://myserver/user/authenticate",
        data: $.param({email: email, password: password}),
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    }).then(function (response) {
       /**Check here your status code 400 if you found that status code is 400 the reject the promise**/
            if (statuscode !==400) {
                        return response.data;
                    } else {
                        // invalid response
                        return $q.reject(response.data);
                    }

        }, function (response) {
            // something went wrong
            return $q.reject(response);
        });
}

and then use following code

然后使用以下代码

 authService.login(email, password)
        .then(function (response) {
            /* go to application */
        },function(error) {
                console.log(error); /* catch 400  Error here */
    });