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

Fetch adapter should not call body.text() if body.json() throws an error #340

Closed
holly-cummins opened this issue Jun 8, 2022 · 1 comment · Fixed by #346
Closed

Fetch adapter should not call body.text() if body.json() throws an error #340

holly-cummins opened this issue Jun 8, 2022 · 1 comment · Fixed by #346

Comments

@holly-cummins
Copy link

holly-cummins commented Jun 8, 2022

If my geo data provider fails to give a healthy response, the error handling is not correct. I'm seeing errors of the following form

Error getting geography information for San Francisco, CA, USA
Error: HttpError: body used already for: https://nominatim.openstreetmap.org/search?addressdetails=1&q=San+Francisco%2C+CA%2C+USA&format=json
    at /home/styff/platform/node_modules/node-geocoder/lib/httpadapter/fetchadapter.js:58:15
    at tryCatcher (/home/stuff/platform/node_modules/bluebird/js/release/util.js:16:23)

This seems to be the same as node-fetch/node-fetch#533. The cause is this block in https://github.com/nchaulet/node-geocoder/blob/master/lib/httpadapter/fetchadapter.js:

        try {
          return await res.json();
        } catch (e) {
          throw new HttpError(await res.text(), {
            code: res.statusCode
          });
        }

Because the response is a stream, calling res.json() and then res.text() is not valid. The recommended solution seems to be either:

  • pre-check the response's status code before calling res.json() to reduce the likelihood of error
  • just call res.text() so there's always a response assigned to a variable, and then call JSON.parse(the-text) and wrap that call in a try-catch. If the try-catch fails, then you can print the-text without trying to read the stream twice.
Ephigenia added a commit to Ephigenia/node-geocoder that referenced this issue Feb 27, 2023
martinpfannemueller pushed a commit to martinpfannemueller/node-geocoder that referenced this issue Mar 12, 2023
@Ephigenia
Copy link
Contributor

@martinpfannemueller your fix is not visible here? maybe you can check with my fix: #346 and vote here so that it gets merged?

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.

2 participants