Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bad handling of API errors in library #27

Open
elondaits opened this issue Jan 3, 2021 · 2 comments
Open

Bad handling of API errors in library #27

elondaits opened this issue Jan 3, 2021 · 2 comments

Comments

@elondaits
Copy link

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.

@wojtekmaj
Copy link

For anyone looking for a solution, my fork has this issue resolved.

@damion-add-on-depot
Copy link

Ran into the same issue. Looked at a few options and @wojtekmaj's fork worked well for me.

@wojtekmaj wojtekmaj mentioned this issue Aug 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants