Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(errors): show text search detail #226

Merged
merged 3 commits into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions frontend/plugins/vuex-orm-axios.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ export default ({ $axios, app }) => {
if (error instanceof ExpiredAuthSessionError || [401, 403].includes(code)) {
app.$auth.logout();
}

if (code === 400) {
// Add more erros once are better handled
return Notification.dispatch("notify", {
message:
"Error: " + JSON.stringify(error.response.data.detail || error),
type: "error",
});
}

return Notification.dispatch("notify", {
message: error,
type: "error",
Expand Down
7 changes: 7 additions & 0 deletions src/rubrix/server/commons/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ def __init__(self):
)


class InvalidTextSearchError(HTTPException):
"""Error related with input params in search"""

def __init__(self, detail: str):
super().__init__(status_code=status.HTTP_400_BAD_REQUEST, detail=detail)


class EntityNotFoundError(HTTPException):
"""Error raised when entity not found"""

Expand Down
30 changes: 21 additions & 9 deletions src/rubrix/server/commons/es_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import os
from rubrix.server.commons.errors import InvalidTextSearchError
from typing import Any, Callable, Dict, Iterable, List, Optional

import elasticsearch
from elasticsearch import Elasticsearch, NotFoundError
from elasticsearch import Elasticsearch, NotFoundError, RequestError
from elasticsearch.helpers import bulk as es_bulk, scan as es_scan
from rubrix.logging import LoggingMixin

Expand Down Expand Up @@ -86,14 +87,25 @@ def search(
-------

"""
return self.__client__.search(
index=index,
body=query or {},
routing=routing,
track_total_hits=True,
rest_total_hits_as_int=True,
size=size,
)
try:
return self.__client__.search(
index=index,
body=query or {},
routing=routing,
track_total_hits=True,
rest_total_hits_as_int=True,
size=size,
)
except RequestError as rex:

if rex.error != "search_phase_execution_exception":
raise rex

detail = rex.info["error"]
detail = detail.get("root_cause")
detail = detail[0].get("reason") if detail else rex.info["error"]

raise InvalidTextSearchError(detail)

def create_index(
self, index: str, force_recreate: bool = False, mappings: Dict[str, Any] = None
Expand Down
31 changes: 31 additions & 0 deletions tests/server/text_classification/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
TextClassificationQuery,
TextClassificationRecord,
TextClassificationSearchResults,
TextClassificationSearchRequest,
)

client = TestClient(app)
Expand Down Expand Up @@ -433,3 +434,33 @@ def test_metadata_with_point_in_field_name():
assert "field.one" in results.aggregations.metadata
assert results.aggregations.metadata.get("field.one", {})["1"] == 2
assert results.aggregations.metadata.get("field.two", {})["2"] == 2


def test_wrong_text_query():
dataset = "test_wrong_text_query"
assert client.delete(f"/api/datasets/{dataset}").status_code == 200

response = client.post(
f"/api/datasets/{dataset}/TextClassification:bulk",
data=TextClassificationBulkData(
records=[
TextClassificationRecord(
**{
"id": 0,
"inputs": {"text": "Esto es un ejemplo de texto"},
"metadata": {"field.one": 1, "field.two": 2},
}
),
],
).json(by_alias=True),
)

response = client.post(
f"/api/datasets/{dataset}/TextClassification:search",
json=TextClassificationSearchRequest(
query=TextClassificationQuery(query_text="!")
).dict(),
)

assert response.status_code == 400
assert response.json()["detail"] == "Failed to parse query [!]"