简体   繁体   中英

Gruntfile.js: Handlling grunt-http callback...ERROR

I am working on 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: 在此处输入图像描述

If I pass a valid credential then both grunt task ( http , func2 ) is working properly. But if I pass a wrong credential then stop grunt file execution at first task execution time. That means 2nd is not executing in this scenario.

Now my question is, how to handle grunt-http callback error?

This issue is resolved.

If add ignoreErrors: true within option , then it's working properly.

options: {
    ignoreErrors: true,
    . . .
}

But I am not able to print the http-error . Is there any way to capture it?

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