Skip to content

Node cash when Restclient tried to parse invalid json #16

Open

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

Activity

  1. kwanLeeFrmVi commented on Mar 8, 2024

    @kwanLeeFrmVi
    Author

    P/s: this could happen when I init class Telesign with in-correct restEndpoint arg

  2. aidanatwork commented on Mar 11, 2024

    @aidanatwork

    Hi @quanle1996 , I'm on the Product team here at Telesign, and would love to learn more about the issue you are raising here. I'm not sure I understand fully yet though. Could you explain to me further what you are seeing and what is the behavior you would expect to see instead? Thanks!

  3. kwanLeeFrmVi commented on Mar 12, 2024

    @kwanLeeFrmVi
    Author

    Hi @aidanatwork,
    I accidentally set restEndpoint = 'https://rest-api.telesign.com'
    instead of 'https://rest-ww.telesign.com/v1'
    in

    new TeleSignSDK(
         'customerId',
         'apiKey',
          'incorrect-restEndpoint'
        );
    

    this causes an un-catch exception at runtime.
    this should be catch at node_modules/telesignsdk/src/RestClient.js row :185
    as following

    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 = {};
                    try {
                        body = JSON.parse(bodyStr)
                    } catch (e) { body = bodyStr }
                    if (callback) {
                        callback(err, body);
                    }
                }
            });
    

    because bodyStr could be an invalid json so JSON.parse would throw error

  4. aidanatwork commented on Mar 12, 2024

    @aidanatwork

    Thanks @kwanLeeFrmVi , I'll report this to our SDK team.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions