♻️(back) stop returning a 500 on cors_proxy on request failure
On the cors_proxy endpoint, if the fetched url fails we were returning an error 500. Instead, we log the exception and return a 400 to not give back information to the frontend application.
This commit is contained in:
@@ -1481,10 +1481,10 @@ class DocumentViewSet(
|
|||||||
return proxy_response
|
return proxy_response
|
||||||
|
|
||||||
except requests.RequestException as e:
|
except requests.RequestException as e:
|
||||||
logger.error("Proxy request failed: %s", str(e))
|
logger.exception(e)
|
||||||
return drf_response.Response(
|
return drf.response.Response(
|
||||||
{"error": f"Failed to fetch resource: {e!s}"},
|
{"error": f"Failed to fetch resource from {url}"},
|
||||||
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
status=status.HTTP_400_BAD_REQUEST,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
import responses
|
import responses
|
||||||
|
from requests.exceptions import RequestException
|
||||||
from rest_framework.test import APIClient
|
from rest_framework.test import APIClient
|
||||||
|
|
||||||
from core import factories
|
from core import factories
|
||||||
@@ -170,3 +171,20 @@ def test_api_docs_cors_proxy_invalid_url(url_to_fetch):
|
|||||||
)
|
)
|
||||||
assert response.status_code == 400
|
assert response.status_code == 400
|
||||||
assert response.json() == ["Enter a valid URL."]
|
assert response.json() == ["Enter a valid URL."]
|
||||||
|
|
||||||
|
|
||||||
|
@responses.activate
|
||||||
|
def test_api_docs_cors_proxy_request_failed():
|
||||||
|
"""Test the CORS proxy API for documents with a request failed."""
|
||||||
|
document = factories.DocumentFactory(link_reach="public")
|
||||||
|
|
||||||
|
client = APIClient()
|
||||||
|
url_to_fetch = "https://external-url.com/assets/index.html"
|
||||||
|
responses.get(url_to_fetch, body=RequestException("Connection refused"))
|
||||||
|
response = client.get(
|
||||||
|
f"/api/v1.0/documents/{document.id!s}/cors-proxy/?url={url_to_fetch}"
|
||||||
|
)
|
||||||
|
assert response.status_code == 400
|
||||||
|
assert response.json() == {
|
||||||
|
"error": "Failed to fetch resource from https://external-url.com/assets/index.html"
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user