Skip to content

Node cash when Restclient tried to parse invalid json #16

@kwanLeeFrmVi

Description

@kwanLeeFrmVi

image

 sms(callback, phoneNumber, optionalParams=null) {
        var params = {
            phone_number: phoneNumber,
        };
        if (optionalParams !== null) {
            params = Object.assign(params, optionalParams)
        }

        this.rest.execute(callback, "POST", this.smsResource, params);
    }

the function sms in verify call rest client execute

    execute(callback, methodName, resource, params = null) {
        var telesignURL = this.restEndpoint + resource;
        var bodyData = this.contentType=="application/json" ? "{}" : null;
        if (methodName == "POST" || methodName == "PUT") {
            if (params != null && Object.keys(params).length > 0) {
              if (this.contentType == "application/x-www-form-urlencoded") {
                bodyData = querystring.stringify(params);
              } else {
                bodyData = JSON.stringify(params);
              }
            }
        }
        else { // GET method
            if (params != null) {
                telesignURL = URI(this.restEndpoint + resource).query(params).toString();
            }
            else {
                telesignURL = URI(this.restEndpoint + resource).toString();
            }
        }

        var headers = RestClient.generateTeleSignHeaders(this.customerId,
            this.apiKey,
            methodName,
            resource,
            this.contentType,
            bodyData,
            null,
            null,
            this.userAgent);

        var requestParams = {
            headers: headers,
            uri: telesignURL,
            method: methodName,
            timeout: this.timeout
        };

        if (bodyData != null) {
            requestParams.body = bodyData;
        }

        request(requestParams, function (err, res, bodyStr) {

            if (err) {

                console.error(`FATAL ERROR: ${new Date()}`
                    + ` Problems contacting Telesign Servers. Check your internet connection.`);

                if (callback){
                    callback(err, bodyStr);
                }
            }
            if (res) {
                var body = JSON.parse(bodyStr); // HERE
                if (callback){
                    callback(err, body);
                }
            }
        });

    }
}

where bodyStre being parse should be put in try catch

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions