avwx-rest / avwx-api

REST API for parsing aviation weather data
https://avwx.rest
MIT License
71 stars 89 forks source link

API returns HTTP 200 for invalid ICAO codes #1

Closed JanC closed 6 years ago

JanC commented 6 years ago

Hi, when a client submits a non-existing icao code, the API returns correctly an error message

{
    "Error": "Station Lookup Error: METAR not found for ABCD. There might not be a current report in ADDS",
    "Meta": {
        "Timestamp": "Sun, 18 Feb 2018 20:20:21 GMT"
    }
}

but the HTTP code is 200.

curl -I 'https://avwx.rest/api/metar/ABCD'
HTTP/1.1 200 OK
Content-Length: 176
Content-Type: application/json
Server: gunicorn/19.7.1
Access-Control-Allow-Origin: *
Date: Sun, 18 Feb 2018 20:32:36 GMT

I believe the api.py should handle this and return an appropriate error code to the client. In the if error: case:

https://github.com/flyinactor91/AVWX-API/blob/bed76e4960d46420d2fdcfe424fa814182c2760c/avwx_api/api.py#L82-L92

maybe simply:

if error:
    resp = jsonify({'Error': error})
    resp.status_code = 404
    return resp
devdupont commented 6 years ago

I agree. I’ll go through the API and find these error responses. However, I think the error code should be 400 in this case because the endpoint exists but the error arose because of client-supplied info.

devdupont commented 6 years ago

The full range of status code pass throughs was a little more than expected, but https://github.com/flyinactor91/AVWX-API/commit/d7547f78a2082c33b42ca0babbc02264300082e3 should handle all of these cases for API endpoints. It also let me change if 'Error' in data: to if code != 200: which is more flexible.

JanC commented 6 years ago

thanks!

JanC commented 6 years ago

When supplying a invalid station icao code, the returned error is still 500 though:

curl http://localhost:8000/api/metar/ABC  | jq
{
  "message": "Internal Server Error"
}