diff --git a/swh/web/api/views/graph.py b/swh/web/api/views/graph.py --- a/swh/web/api/views/graph.py +++ b/swh/web/api/views/graph.py @@ -140,6 +140,14 @@ if request.GET: graph_query_url += "?" + request.GET.urlencode(safe="/;:") response = requests.get(graph_query_url, stream=True) + + if response.status_code != 200: + return Response( + response.content, + status=response.status_code, + content_type=response.headers["Content-Type"], + ) + # graph stats and counter endpoint responses are not streamed if response.headers.get("Transfer-Encoding") != "chunked": return Response( diff --git a/swh/web/tests/api/views/test_graph.py b/swh/web/tests/api/views/test_graph.py --- a/swh/web/tests/api/views/test_graph.py +++ b/swh/web/tests/api/views/test_graph.py @@ -268,3 +268,25 @@ assert resp.content_type == "application/json" assert resp.data["exception"] == "NotAcceptable" assert resp.data["reason"] == "Could not satisfy the request Accept header." + + +def test_graph_error_response(api_client, keycloak_oidc, requests_mock): + _authenticate_graph_user(api_client, keycloak_oidc) + + graph_query = "foo" + + error_message = "Not found" + content_type = "text/plain" + + requests_mock.get( + get_config()["graph"]["server_url"] + graph_query, + text=error_message, + headers={"Content-Type": content_type}, + status_code=404, + ) + + url = reverse("api-1-graph", url_args={"graph_query": graph_query}) + + resp = check_http_get_response(api_client, url, status_code=404) + assert resp.content_type == content_type + assert resp.content == f'"{error_message}"'.encode()