Skip to content

Raising custom error messages in your deployment

Normally, UbiOps will throw the default error message Something went wrong during the request, see logs for details. when something goes wrong during a request. This standard error message prevents sensitive information from being displayed to the person making the request. However, you might have the need to return custom error messages for specific situations in your code. It is possible to override the default error message by passing UbiOps a public error message.

In your deployment.py you can override the default error message by raising an error with a public_error_message attribute. You can use any name for your error class, as long as it contains the public_error_message attribute.

# Define an error class with attribute 'public_error_message'
# Any error that is raised with this attribute will override the default error message
class PublicError(Exception):

    def __init__(self, public_error_message):
        super().__init__()
        self.public_error_message = public_error_message

class Deployment:

    def __init__(self, base_directory, context):

        ...

    def request(self, data):

        # Let's say you want to raise the error here
        raise PublicError(public_error_message="MY CUSTOM ERROR MESSAGE")

        return {'some_output': ...}