Skip to content

Generate python exceptions for conjure errors #910

@tgsiegel

Description

@tgsiegel

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions