Added a if-else to check response.ok before parsing JSON in query tool

<!--
Please read the contributing guidelines before making a PR:
  
https://github.com/openstreetmap/openstreetmap-website/blob/master/CONTRIBUTING.md

Pay particular attention to the section on how to present PRs:
  
https://github.com/openstreetmap/openstreetmap-website/blob/master/CONTRIBUTING.md#pull-requests
-->

### Description
This PR fixes issue #6667.
The query tool no longer crashes when the network or server returns an error.

## Problem
The query tool tried to read every response as JSON.
When the Overpass API returned an error page or the request failed, this caused 
unclear errors like:

`JSON.parse: unexpected character at line 1 column 1 of the JSON data`

## Solution
The tool now checks the response status before reading JSON.
```
  if (!response.ok) {
          throw new Error(`HTTP ${response.status}: ${response.statusText}`);
        }
```
If the request fails, it shows a clear message, for example:

`HTTP 504: Gateway Timeout`

### How has this been tested?

- Linting passes (eslint, erb_lint, rubocop)

- Manually testing network failure using browser offline mode
 The tool now shows “Failed to fetch”

- Normal queries still work as expected

Fixes #6667

You can view, comment on, or merge this pull request online at:

  https://github.com/openstreetmap/openstreetmap-website/pull/6724

-- Commit Summary --

  * Check response.ok before parsing JSON in query tool

-- File Changes --

    M app/assets/javascripts/index/query.js (8)

-- Patch Links --

https://github.com/openstreetmap/openstreetmap-website/pull/6724.patch
https://github.com/openstreetmap/openstreetmap-website/pull/6724.diff

-- 
Reply to this email directly or view it on GitHub:
https://github.com/openstreetmap/openstreetmap-website/pull/6724
You are receiving this because you are subscribed to this thread.

Message ID: <openstreetmap/openstreetmap-website/pull/[email protected]>
_______________________________________________
rails-dev mailing list
[email protected]
https://lists.openstreetmap.org/listinfo/rails-dev

Reply via email to