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. It works slightly different in R and Python, so you can view the examples below to see how you can do it in the deployment.R or in the deployment.py.

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': ...}

In your deployment.R you can override the default error message by raising an error which starts with "Public error message:". Everything that follows the colon will be used by UbiOps as the new error message.

init <- function(base_directory, context) {
    ...
}

request <- function(input_data, base_directory, context) {

    # Let's say you want to raise the error here
    # Any error message that is preceded by "Public error message:" will override the default error message
    stop("Public error message: MY CUSTOM ERROR MESSAGE")


    list( some_output = ... )
}