Skip to content

Commit 8727294

Browse files
Merge pull request #558 from aiven/misc-error-handling
Miscellaneous error handling fixes
2 parents 88bee3a + 90a360a commit 8727294

4 files changed

Lines changed: 36 additions & 8 deletions

File tree

karapace/kafka_rest_apis/__init__.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
UnknownTopicOrPartitionError,
1414
)
1515
from karapace.config import Config, create_client_ssl_context
16+
from karapace.errors import InvalidSchema
1617
from karapace.kafka_rest_apis.admin import KafkaRestAdminClient
1718
from karapace.kafka_rest_apis.consumer_manager import ConsumerManager
1819
from karapace.kafka_rest_apis.error_codes import RESTErrorCodes
@@ -779,11 +780,20 @@ async def validate_schema_info(self, data: dict, prefix: str, content_type: str,
779780
KafkaRest.r(
780781
body={
781782
"error_code": RESTErrorCodes.SCHEMA_RETRIEVAL_ERROR.value,
782-
"message": f"Error when registering schema. format = {schema_type}, subject = {topic}-{prefix}",
783+
"message": f"Error when registering schema. format = {schema_type.value}, subject = {topic}-{prefix}",
783784
},
784785
content_type=content_type,
785786
status=HTTPStatus.REQUEST_TIMEOUT,
786787
)
788+
except InvalidSchema:
789+
KafkaRest.r(
790+
body={
791+
"error_code": RESTErrorCodes.INVALID_DATA.value,
792+
"message": f'Invalid schema. format = {schema_type.value}, schema = {data[f"{prefix}_schema"]}',
793+
},
794+
content_type=content_type,
795+
status=HTTPStatus.UNPROCESSABLE_ENTITY,
796+
)
787797

788798
async def _prepare_records(
789799
self,

karapace/rapu.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,6 @@ def check_rest_headers(self, request: HTTPRequest) -> dict:
213213
result["requests"] = header_info
214214
result["accepts"] = accept_matcher.groupdict()
215215
return result
216-
self.log.error("Not acceptable: %r", request.get_header("accept"))
217216
http_error(
218217
message="HTTP 406 Not Acceptable",
219218
content_type=result["content_type"],
@@ -331,6 +330,9 @@ async def _handle_request(
331330
data = ex.body
332331
status = ex.status
333332
headers = ex.headers
333+
except asyncio.CancelledError:
334+
# Re-raise if aiohttp cancelled the task (e.g. client disconnected) without internal server error
335+
raise
334336
except: # pylint: disable=bare-except
335337
self.log.exception("Internal server error")
336338
headers = {"Content-Type": "application/json"}

karapace/schema_registry_apis.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
@unique
4040
class SchemaErrorCodes(Enum):
41+
HTTP_BAD_REQUEST = HTTPStatus.BAD_REQUEST.value
4142
HTTP_NOT_FOUND = HTTPStatus.NOT_FOUND.value
4243
HTTP_CONFLICT = HTTPStatus.CONFLICT.value
4344
HTTP_UNPROCESSABLE_ENTITY = HTTPStatus.UNPROCESSABLE_ENTITY.value
@@ -835,11 +836,11 @@ def _validate_schema_request_body(self, content_type: str, body: Union[dict, Any
835836
if not isinstance(body, dict):
836837
self.r(
837838
body={
838-
"error_code": SchemaErrorCodes.HTTP_INTERNAL_SERVER_ERROR.value,
839-
"message": "Internal Server Error",
839+
"error_code": SchemaErrorCodes.HTTP_BAD_REQUEST.value,
840+
"message": "Malformed request",
840841
},
841842
content_type=content_type,
842-
status=HTTPStatus.INTERNAL_SERVER_ERROR,
843+
status=HTTPStatus.BAD_REQUEST,
843844
)
844845
for attr in body:
845846
if attr not in {"schema", "schemaType"}:
@@ -853,6 +854,15 @@ def _validate_schema_request_body(self, content_type: str, body: Union[dict, Any
853854
)
854855

855856
def _validate_schema_type(self, content_type: str, data: JsonData) -> SchemaType:
857+
if not isinstance(data, dict):
858+
self.r(
859+
body={
860+
"error_code": SchemaErrorCodes.HTTP_BAD_REQUEST.value,
861+
"message": "Malformed request",
862+
},
863+
content_type=content_type,
864+
status=HTTPStatus.BAD_REQUEST,
865+
)
856866
schema_type_unparsed = data.get("schemaType", SchemaType.AVRO.value)
857867
try:
858868
schema_type = SchemaType(schema_type_unparsed)

tests/integration/test_schema.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,12 @@ async def test_compatibility_endpoint(registry_async_client: Client, trail: str)
288288
subject = create_subject_name_factory(f"test_compatibility_endpoint-{trail}")()
289289
schema_name = create_schema_name_factory(f"test_compatibility_endpoint_{trail}")()
290290

291+
res = await registry_async_client.post(
292+
f"subjects/{subject}/versions{trail}",
293+
json=-1,
294+
)
295+
assert res.status_code == 400
296+
291297
schema = {
292298
"type": "record",
293299
"name": schema_name,
@@ -2220,9 +2226,9 @@ async def test_schema_body_validation(registry_async_client: Client) -> None:
22202226
assert res.json()["message"] == "Unrecognized field: invalid_field"
22212227
# Invalid body type
22222228
res = await registry_async_client.post(endpoint, json="invalid")
2223-
assert res.status_code == 500
2224-
assert res.json()["error_code"] == 500
2225-
assert res.json()["message"] == "Internal Server Error"
2229+
assert res.status_code == 400
2230+
assert res.json()["error_code"] == 400
2231+
assert res.json()["message"] == "Malformed request"
22262232

22272233

22282234
async def test_version_number_validation(registry_async_client: Client) -> None:

0 commit comments

Comments
 (0)