Skip to content

Bad handling of API errors in library #27

@elondaits

Description

@elondaits

I was calling getFormSubmissions() and seeing debug output, but my process exited immediately without waiting for the promise. After much frustration I accessed the API URL outputted by the debug info through my browser, and found the following error:

{"responseCode":301,"location":"https:\/\/eu-api.jotform.com\/form\/REDACTED\/submissions","message":"Your account is in EU Safe mode. Please use \"eu-api.jotform.com\" endpoint to get your results."}

I was handling errors correctly, so I didn't understand why I was not getting an exception (through the "fail" handler). I checked the code and came across the following in the sendRequest function of your index.js file (line 41):

request(options, function(err, response, body){
            if(err){
                deferred.reject(err);
                return;
            }
            if(response.statusCode == 200 && body.responseCode == 200){
                deferred.resolve(body.content);
            }
            if(response.statusCode != 200){
                deferred.reject(new Error(body.message));
            }
        });

If the request comes back with a response.statusCode == 200, but there's a different response code in the JSON body, the deferred is neither resolved nor rejected. The code assumes that the HTTP status code will match the API response code, but to be sure you should check

            if(response.statusCode != 200 || body.responseCode != 200)

you probably also should initialize your Error with body.message only when there's a body, and with a library error otherwise... if the HTTP response fails it's not guaranteed you'll have a JSON body.

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