简体   繁体   English

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

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

I am working on grunt-http .我正在研究grunt-http If I pass an invalid credential then it's throwing an error like:如果我传递了无效的凭据,则会引发如下错误:

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");
    });
};

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

If I pass a valid credential then both grunt task ( http , func2 ) is working properly.如果我通过了有效的凭据,那么两个 grunt 任务( httpfunc2 )都可以正常工作。 But if I pass a wrong credential then stop grunt file execution at first task execution time.但是,如果我传递了错误的凭据,则在第一个任务执行时停止 grunt 文件执行。 That means 2nd is not executing in this scenario.这意味着 2nd 在这种情况下没有执行。

Now my question is, how to handle grunt-http callback error?现在我的问题是,如何处理grunt-http callback错误?

This issue is resolved.此问题已解决。

If add ignoreErrors: true within option , then it's working properly.如果在option中添加ignoreErrors: true ,那么它工作正常。

options: {
    ignoreErrors: true,
    . . .
}

But I am not able to print the http-error .但我无法打印http-error Is there any way to capture it?有什么办法可以捕捉到吗?

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

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