Skip to content

Commit

Permalink
Merge pull request #619 from vespa-engine/jobergum/option_to_not_rais…
Browse files Browse the repository at this point in the history
…e_on_404

Jobergum/option to not raise on 404
  • Loading branch information
kkraune authored Nov 8, 2023
2 parents 86d4ffe + 103e0ac commit 5681635
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
8 changes: 6 additions & 2 deletions vespa/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import sys
import ssl
from xmlrpc.client import Boolean
import aiohttp
import asyncio
import requests
Expand Down Expand Up @@ -35,13 +36,14 @@

VESPA_CLOUD_SECRET_TOKEN: str = "VESPA_CLOUD_SECRET_TOKEN"

def raise_for_status(response: Response) -> None:
def raise_for_status(response: Response, ignore_not_found:bool=False) -> None:
"""
Raises an appropriate error if necessary.
If the response contains an error message, VespaError is raised along with HTTPError to provide more details.
:param response: Response object from Vespa API.
:param ignore_not_found: If True, ignore 404 status code.
:raises HTTPError: If status_code is between 400 and 599.
:raises VespaError: If the response JSON contains an error message.
"""
Expand All @@ -50,6 +52,8 @@ def raise_for_status(response: Response) -> None:
except HTTPError as http_error:
try:
response_json = response.json()
if response.status_code == 404 and ignore_not_found:
return
except JSONDecodeError:
raise http_error
errors = response_json.get("root", {}).get("errors", [])
Expand Down Expand Up @@ -782,7 +786,7 @@ def get_data(
self.app.end_point, namespace, schema, str(data_id)
)
response = self.http_session.get(end_point, params=kwargs)
raise_for_status(response) # This is questionable, since it will throw on 404 as well.
raise_for_status(response,ignore_not_found=True)
return VespaResponse(
json=response.json(),
status_code=response.status_code,
Expand Down
3 changes: 3 additions & 0 deletions vespa/test_integration_vespa_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,15 @@ def test_key_cert_arguments(self):
},
self.app.get_data(data_id="1").json,
)
self.assertEqual(self.app.get_data(data_id="1").is_successfull(), False)


def tearDown(self) -> None:
self.app.delete_all_docs(
content_cluster_name="msmarco_content", schema="msmarco"
)
shutil.rmtree(self.disk_folder, ignore_errors=True)
#self.vespa_cloud.delete()


class TestMsmarcoApplication(TestApplicationCommon):
Expand Down
6 changes: 5 additions & 1 deletion vespa/test_integration_vespa_cloud_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import asyncio
import shutil
import unittest
import pytest
from requests import HTTPError
from vespa.application import Vespa
from vespa.package import AuthClient, Parameter
from vespa.deployment import VespaCloud
Expand Down Expand Up @@ -48,20 +50,22 @@ def test_right_endpoint_used_with_token(self):
# The token is used to access the application status endpoint.
print("Endpoint used " + self.app.url)
self.app.wait_for_application_up(max_wait=APP_INIT_TIMEOUT)
self.assertEqual(200, self.app.get_application_status().status_code)
self.assertDictEqual(
{
"pathId": "/document/v1/msmarco/msmarco/docid/1",
"id": "id:msmarco:msmarco::1",
},
self.app.get_data(data_id="1").json,
)
self.assertEqual(self.app.get_data(data_id="1").is_successfull(), False)


def tearDown(self) -> None:
self.app.delete_all_docs(
content_cluster_name="msmarco_content", schema="msmarco"
)
shutil.rmtree(self.disk_folder, ignore_errors=True)
#self.vespa_cloud.delete()


class TestMsmarcoApplicationWithTokenAuth(TestApplicationCommon):
Expand Down
3 changes: 1 addition & 2 deletions vespa/test_integration_vespa_cloud_vector_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import os
import shutil
import asyncio
from typing import Iterable
import unittest
from vespa.application import Vespa, ApplicationPackage
from vespa.package import Schema, Document, Field, HNSW, RankProfile
Expand Down Expand Up @@ -163,5 +161,6 @@ def tearDown(self) -> None:
self.assertEqual(len(response.hits), 0)
print(response.get_json())
shutil.rmtree(self.disk_folder, ignore_errors=True)
#self.vespa_cloud.delete()


0 comments on commit 5681635

Please sign in to comment.