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

Generate python exceptions for conjure errors #910

Open
tgsiegel opened this issue Oct 16, 2024 · 0 comments
Open

Generate python exceptions for conjure errors #910

tgsiegel opened this issue Oct 16, 2024 · 0 comments

Comments

@tgsiegel
Copy link

When using conjure generated clients, any error is represented by a generic ConjureHTTPException that requires manual parsing to determine exactly what the error is. If I have some conjure error

SomeException:
  docs: Something went wrong
  code: INVALID_ARGUMENT
  namespace: MyService
  safe-args:
    argument: string
    object: SomeObjectWithFields
  unsafe-args:
    unsafeArgument: string 

I would like to get some generated python exception

class SomeException(Exception):
    """
    Something went wrong
    """

    NAMESPACE = "MyService"
    ERROR_CODE = "INVALID_ARGUMENT" # probably want this to be an enum
    ERROR_NAME = "MyService:SomeException"

    def __init__(self, conjure_exception: ConjureHTTPError):
        # do some validation
        self.unsafe_args = ...
        self.safe_args = ...

    @staticmethod
    def is_instance(self, conjure_exception: ConjureHTTPError) -> bool:
        # check if the conjure exception is this
        return conjure_exception.error_name == SomeException.ERROR_NAME

That way I can use this in my code to convert a ConjureHTTPError into a more user friendly exception, with some actual types

try:
    return some_service.endpoint()
except ConjureHTTPError as err:
    if SomeException.is_error(err):
        actual_exception = SomeException(err)
        raise UserFriendlyError(actual_exception.safe_args.argument, actual_exception.object.some_object_field)
	# maybe check some other exceptions here too
    raise err

Both java and typescript conjure clients do this, I think it would be really useful to have it in python as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant