-
Notifications
You must be signed in to change notification settings - Fork 381
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#4012] improvement(client-python): Refactor Error Handling in client…
…-python (#4093) ### What changes were proposed in this pull request? * Refactor the error handling structure, each API can implement their own error handler to raise custom exceptions * Add unit test for error handler, but unit tests and integration tests for each API(ex. metalake, catalog, schema) have not been added, I will create issues for them. - [ ] Add Metalake Error Handler and related exceptions, test cases - [ ] Add Catalog Error Handler and related exceptions, test cases - [ ] Add Schema Error Handler and related exceptions, test cases - [ ] Add OAuth Error Handler and related exceptions, test cases * Create `gravitino/exceptions/base.py` to define all the exceptions. * Remove some unused files and exceptions ### Why are the changes needed? Fix: #4012 ### Does this PR introduce _any_ user-facing change? No ### How was this patch tested? UT added and one IT added, test with `./gradlew clients:client-python:test` --------- Co-authored-by: TimWang <[email protected]>
- Loading branch information
1 parent
7c8fc04
commit 9e80cbc
Showing
31 changed files
with
531 additions
and
253 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
""" | ||
Licensed to the Apache Software Foundation (ASF) under one | ||
or more contributor license agreements. See the NOTICE file | ||
distributed with this work for additional information | ||
regarding copyright ownership. The ASF licenses this file | ||
to you under the Apache License, Version 2.0 (the | ||
"License"); you may not use this file except in compliance | ||
with the License. You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, | ||
software distributed under the License is distributed on an | ||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations | ||
under the License. | ||
""" | ||
|
||
from enum import IntEnum | ||
|
||
from gravitino.exceptions.base import ( | ||
RESTException, | ||
IllegalArugmentException, | ||
NotFoundException, | ||
InternalError, | ||
AlreadyExistException, | ||
NotEmptyException, | ||
UnsupportedOperationException, | ||
) | ||
|
||
|
||
class ErrorConstants(IntEnum): | ||
"""Constants representing error codes for responses.""" | ||
|
||
# Error codes for REST responses error. | ||
REST_ERROR_CODE = 1000 | ||
|
||
# Error codes for illegal arguments. | ||
ILLEGAL_ARGUMENTS_CODE = 1001 | ||
|
||
# Error codes for internal errors. | ||
INTERNAL_ERROR_CODE = 1002 | ||
|
||
# Error codes for not found. | ||
NOT_FOUND_CODE = 1003 | ||
|
||
# Error codes for already exists. | ||
ALREADY_EXISTS_CODE = 1004 | ||
|
||
# Error codes for non empty. | ||
NON_EMPTY_CODE = 1005 | ||
|
||
# Error codes for unsupported operation. | ||
UNSUPPORTED_OPERATION_CODE = 1006 | ||
|
||
# Error codes for invalid state. | ||
UNKNOWN_ERROR_CODE = 1100 | ||
|
||
|
||
EXCEPTION_MAPPING = { | ||
RESTException: ErrorConstants.REST_ERROR_CODE, | ||
IllegalArugmentException: ErrorConstants.ILLEGAL_ARGUMENTS_CODE, | ||
InternalError: ErrorConstants.INTERNAL_ERROR_CODE, | ||
NotFoundException: ErrorConstants.NOT_FOUND_CODE, | ||
AlreadyExistException: ErrorConstants.ALREADY_EXISTS_CODE, | ||
NotEmptyException: ErrorConstants.NON_EMPTY_CODE, | ||
UnsupportedOperationException: ErrorConstants.UNSUPPORTED_OPERATION_CODE, | ||
} | ||
|
||
ERROR_CODE_MAPPING = {v: k for k, v in EXCEPTION_MAPPING.items()} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
clients/client-python/gravitino/dto/responses/error_response.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
""" | ||
Licensed to the Apache Software Foundation (ASF) under one | ||
or more contributor license agreements. See the NOTICE file | ||
distributed with this work for additional information | ||
regarding copyright ownership. The ASF licenses this file | ||
to you under the Apache License, Version 2.0 (the | ||
"License"); you may not use this file except in compliance | ||
with the License. You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, | ||
software distributed under the License is distributed on an | ||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations | ||
under the License. | ||
""" | ||
|
||
from typing import List, Optional | ||
from dataclasses import dataclass, field | ||
from dataclasses_json import config | ||
|
||
from gravitino.dto.responses.base_response import BaseResponse | ||
from gravitino.constants.error import ErrorConstants, EXCEPTION_MAPPING | ||
from gravitino.exceptions.base import UnknownError | ||
|
||
|
||
@dataclass | ||
class ErrorResponse(BaseResponse): | ||
"""Represents an error response.""" | ||
|
||
_type: str = field(metadata=config(field_name="type")) | ||
_message: str = field(metadata=config(field_name="message")) | ||
_stack: Optional[List[str]] = field(metadata=config(field_name="stack")) | ||
|
||
def type(self): | ||
return self._type | ||
|
||
def message(self): | ||
return self._message | ||
|
||
def stack(self): | ||
return self._stack | ||
|
||
def validate(self): | ||
super().validate() | ||
|
||
assert ( | ||
self._type is not None and len(self._type) != 0 | ||
), "type cannot be None or empty" | ||
assert ( | ||
self._message is not None and len(self._message) != 0 | ||
), "message cannot be None or empty" | ||
|
||
def __repr__(self) -> str: | ||
return ( | ||
f"ErrorResponse(code={self._code}, type={self._type}, message={self._message})" | ||
+ ("\n\t" + "\n\t".join(self._stack) if self._stack is not None else "") | ||
) | ||
|
||
def format_error_message(self) -> str: | ||
return ( | ||
f"{self._message}\n" + "\n".join(self._stack) | ||
if self._stack is not None | ||
else "" | ||
) | ||
|
||
@classmethod | ||
def generate_error_response( | ||
cls, exception: Exception, message: str | ||
) -> "ErrorResponse": | ||
for exception_type, error_code in EXCEPTION_MAPPING.items(): | ||
if issubclass(exception, exception_type): | ||
return cls(error_code, exception.__name__, message, None) | ||
|
||
return cls( | ||
ErrorConstants.UNKNOWN_ERROR_CODE, UnknownError.__name__, message, None | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
""" | ||
Licensed to the Apache Software Foundation (ASF) under one | ||
or more contributor license agreements. See the NOTICE file | ||
distributed with this work for additional information | ||
regarding copyright ownership. The ASF licenses this file | ||
to you under the Apache License, Version 2.0 (the | ||
"License"); you may not use this file except in compliance | ||
with the License. You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, | ||
software distributed under the License is distributed on an | ||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations | ||
under the License. | ||
""" | ||
|
||
|
||
class GravitinoRuntimeException(RuntimeError): | ||
"""Base class for all Gravitino runtime exceptions.""" | ||
|
||
def __init__(self, message, *args): | ||
super().__init__(message % args) | ||
|
||
|
||
class RESTException(RuntimeError): | ||
"""An exception thrown when a REST request fails.""" | ||
|
||
def __init__(self, message, *args): | ||
super().__init__(message % args) | ||
|
||
|
||
class IllegalArugmentException(ValueError): | ||
"""Base class for all exceptions thrown when arguments are invalid.""" | ||
|
||
def __init__(self, message, *args): | ||
super().__init__(message % args) | ||
|
||
|
||
class IllegalNameIdentifierException(IllegalArugmentException): | ||
"""An exception thrown when a name identifier is invalid.""" | ||
|
||
|
||
class IllegalNamespaceException(IllegalArugmentException): | ||
"""An exception thrown when a namespace is invalid.""" | ||
|
||
|
||
class InternalError(GravitinoRuntimeException): | ||
"""Base class for all exceptions thrown internally""" | ||
|
||
|
||
class NotFoundException(GravitinoRuntimeException): | ||
"""Base class for all exceptions thrown when a resource is not found.""" | ||
|
||
|
||
class NoSuchSchemaException(NotFoundException): | ||
"""An exception thrown when a schema is not found.""" | ||
|
||
|
||
class NoSuchFilesetException(NotFoundException): | ||
"""Exception thrown when a file with specified name is not existed.""" | ||
|
||
|
||
class NoSuchMetalakeException(NotFoundException): | ||
"""An exception thrown when a metalake is not found.""" | ||
|
||
|
||
class AlreadyExistException(GravitinoRuntimeException): | ||
"""Base exception thrown when an entity or resource already exists.""" | ||
|
||
|
||
class NotEmptyException(GravitinoRuntimeException): | ||
"""Base class for all exceptions thrown when a resource is not empty.""" | ||
|
||
|
||
class UnsupportedOperationException(GravitinoRuntimeException): | ||
"""Base class for all exceptions thrown when an operation is unsupported""" | ||
|
||
|
||
class UnknownError(RuntimeError): | ||
"""An exception thrown when other unknown exception is thrown""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.