繁体   English   中英

Gruntfile.js:处理 grunt-http 回调...错误

[英]Gruntfile.js: Handlling grunt-http callback...ERROR

我正在研究grunt-http 如果我传递了无效的凭据,则会引发如下错误:

Running "http:test_grunt_http" (http) task
Fatal error: 401 {"status":"error","errorCodes":["INVALID_CREDENTIALS"],"data":{"message":"Invalid email or password. Please try again."}}

Gruntfile.js

 module.exports = function (grunt) {
    require('time-grunt')(grunt);

    grunt.config.init({
        http: {
            "test_grunt_http": {
                options: {
                    url: "http://localhost:8000/studentdb/login/auth/",
                    headers: {
                        "Accept": "*/*",
                        "Content-Type": "application/json"
                    },
                    method: 'POST',
                    body: JSON.stringify({
                        email: 'loginID',
                        password: 'loginPassword'
                    }),
                    callback: function (error, response, body) {
                        if (response.statusCode === 200) {
                            var resData = JSON.parse(body);
                            console.log('body: ', resData);
                        } else {
                            console.warn(error); // error is not handling
                        }
                    }
                }
            }
        }
    });

    require('load-grunt-tasks')(grunt);
    grunt.loadNpmTasks('grunt-http');

    grunt.registerTask('default', ['http', 'func2']);
    grunt.registerTask('func2', function (key, value) {
        console.log("I am with in function-02");
    });
};

运行: grunt default Output: 在此处输入图像描述

如果我通过了有效的凭据,那么两个 grunt 任务( httpfunc2 )都可以正常工作。 但是,如果我传递了错误的凭据,则在第一个任务执行时停止 grunt 文件执行。 这意味着 2nd 在这种情况下没有执行。

现在我的问题是,如何处理grunt-http callback错误?

此问题已解决。

如果在option中添加ignoreErrors: true ,那么它工作正常。

options: {
    ignoreErrors: true,
    . . .
}

但我无法打印http-error 有什么办法可以捕捉到吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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