diff --git a/conjure_python_client/__init__.py b/conjure_python_client/__init__.py index 7508fd4f..8aeebfb9 100644 --- a/conjure_python_client/__init__.py +++ b/conjure_python_client/__init__.py @@ -28,6 +28,7 @@ "ConjureUnionType", "DecodableType", "DictType", + "ErrorCode", "ListType", "OptionalType", "OptionalTypeWrapper", diff --git a/conjure_python_client/_http/__init__.py b/conjure_python_client/_http/__init__.py index d22a1099..9b31d7a2 100644 --- a/conjure_python_client/_http/__init__.py +++ b/conjure_python_client/_http/__init__.py @@ -13,6 +13,7 @@ # limitations under the License. from .configuration import SslConfiguration, ServiceConfiguration +from .errors import ErrorCode from .requests_client import RequestsClient, Service, ConjureHTTPError __all__ = [ @@ -21,4 +22,5 @@ "RequestsClient", "Service", "ConjureHTTPError", + "ErrorCode", ] diff --git a/conjure_python_client/_http/errors.py b/conjure_python_client/_http/errors.py new file mode 100644 index 00000000..1ce5e6e8 --- /dev/null +++ b/conjure_python_client/_http/errors.py @@ -0,0 +1,34 @@ +# (c) Copyright 2025 Palantir Technologies Inc. All rights reserved. +# +# Licensed 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 Enum + + +class ErrorCode(str, Enum): + """Conjure error codes as defined in the Conjure specification.""" + + PERMISSION_DENIED = "PERMISSION_DENIED" + INVALID_ARGUMENT = "INVALID_ARGUMENT" + NOT_FOUND = "NOT_FOUND" + CONFLICT = "CONFLICT" + REQUEST_ENTITY_TOO_LARGE = "REQUEST_ENTITY_TOO_LARGE" + FAILED_PRECONDITION = "FAILED_PRECONDITION" + INTERNAL = "INTERNAL" + TIMEOUT = "TIMEOUT" + CUSTOM_CLIENT = "CUSTOM_CLIENT" + CUSTOM_SERVER = "CUSTOM_SERVER" + + def __str__(self) -> str: + """Return just the string value""" + return self.value diff --git a/conjure_python_client/_http/requests_client.py b/conjure_python_client/_http/requests_client.py index 4d776226..abc11ab9 100644 --- a/conjure_python_client/_http/requests_client.py +++ b/conjure_python_client/_http/requests_client.py @@ -20,6 +20,7 @@ from requests.packages.urllib3.util.ssl_ import create_urllib3_context from requests.packages.urllib3.util import Retry from .configuration import ServiceConfiguration +from .errors import ErrorCode import binascii import os @@ -260,7 +261,7 @@ class ConjureHTTPError(HTTPError): attributes extracted from the response.""" _cause: Optional[HTTPError] - _error_code: str + _error_code: ErrorCode _error_name: str _error_instance_id: str _parameters: Dict[str, str] diff --git a/test/_http/test_errors.py b/test/_http/test_errors.py new file mode 100644 index 00000000..26d67eb8 --- /dev/null +++ b/test/_http/test_errors.py @@ -0,0 +1,30 @@ +# (c) Copyright 2025 Palantir Technologies Inc. All rights reserved. +# +# Licensed 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 conjure_python_client import ErrorCode + + +class TestErrorCode: + def test_string_convertion(self): + assert str(ErrorCode.NOT_FOUND) == "NOT_FOUND" + assert str(ErrorCode.INVALID_ARGUMENT) == "INVALID_ARGUMENT" + + def test_error_code_string_equality(self): + assert ErrorCode.NOT_FOUND == "NOT_FOUND" + assert ErrorCode.INVALID_ARGUMENT == "INVALID_ARGUMENT" + assert ErrorCode.NOT_FOUND != "INVALID_ARGUMENT" + + def test_error_code_creation_from_string(self): + assert ErrorCode("NOT_FOUND") == ErrorCode.NOT_FOUND + assert ErrorCode("INVALID_ARGUMENT") == ErrorCode.INVALID_ARGUMENT