Developer Center

Resources to get you started with Algorithmia

Better Error Handling

Updated

On Algorithmia you can develop in several different programming languages. Being language agnostic gives you access to libraries and functions in other languages that would not be possible otherwise. For example, you can call a NodeJS library inside of a Python algorithm. All you have to do is to write a wrapper algorithm for that library.

There is a problem with this language agnostic approach though. Not all programming languages aren’t designed in the same way. This is especially true for handling errors and exceptions.

Let’s give an example. Let’s say you’re calling a computer vision algorithm and you get an error. Normally there wouldn’t be a standard way to understand what the error means. The author of that computer vision algorithm may put a descriptive error message, but that’s not guaranteed for all cases. What is worse is that there’s no easy way to parse an error message either. To parse it, you would need to know all possible error messages beforehand.

Algorithm Error Codes

To be able to better develop algorithms across languages, we decided to create a pre-defined list of errors. These error codes were created to reflect the types of algorithms that are most commonly published in the marketplace.

Error Code Error Use Cases When To Raise
InputError Invalid Input/Image/URL/Settings/JSON Invalid/incomplete input, image, URL, settings, etc. is provided.
UnsupportedError Unsupported file/image, URL/Website, etc. Image, or any file format is not supported, or scraper/parser algorithm does not explicitly support website.
InitializationError (Model) Initialization Failed Algorithm fails to load/initialize (ML/DL model).
OutOfMemoryError Out of Memory Algorithm can't access any/additional RAM memory.
OutOfGpuMemoryError Out of GPU Memory Algorithm can't access any/additional GPU memory.
LanguageError Human Language Not Supported Language is not supported in NLP model.
TooLargeError File/Image Size Is Too Large File or image size is too large or small.
ParsingError Scraping/Parsing Failed, and json (de)serialization failed Scraping/parsing fails due to changing website layout, or getting blacklisted etc. or JSON (de)serialization failed
EntityNotFoundError Word/entry not found in dictionary/DB Entry not found in DB, memory, list, file, or any other data source.
ThirdPartyCredentialError When credentials for a 3rd party service doesn't authenticate anymore 3rd party API key fails to authenticate, or hit usage limit.
AlgorithmError When no error code has been specified, and general algorithm error class Is a general error class, and returns for all other exception cases.

Here is also a table of error codes that can only be generated by the backend. You’ll receive these error codes if the corresponding error case ever happens:

Error Code Error Use Cases When To Raise
TimeoutError When a child algorithm timeout in a parent algorithm Child algorithm run fails due to timeout.
PaymentRequiredError When a user runs out of credits for a child algorithm call mid-parent algorithm run. Child algorithm run fails due to lack of credits.

Programming Language Support

Currently only the Python Algorithmia client natively supports these error codes. Support for other clients will be added soon.

Python

Make sure that your algorithmia client version is: >1.1.2

The Python client returns a new AlgorithmException object that has the following attributes:

  • AlgorithmException.error_type: This is the error code.
  • AlgorithmException.message: This is the custom error message for the error code.

Example 1: Raising an error code with a custom message

Even though we have to conform to the predefined list of error codes, we can still specify a custom message with every error.

Algorithm A:

import Algorithmia
from Algorithmia.errors import AlgorithmException

def apply(input):
    if input["type"] == "image":
        return("Image processed!")
    elif input["type"] == "video":
        return("Video processed!")
    else:
        raise AlgorithmException("Type of media is not supported!", "UnsupportedError")

Algorithm A Sample input:

{"type": "audio"}

Algorithm A Sample Output:

Error: 'Type of media is not supported!'
Traceback (most recent call last):
  File "/opt/algorithm/bin/pipe.py", line 45, in get_response
    result = call_algorithm(request)
  File "/opt/algorithm/bin/pipe.py", line 93, in call_algorithm
    return algorithm.apply(data)
  File "/opt/algorithm/src/BE_test1.py", line 10, in apply
    raise AlgorithmException("Type of media is not supported!", "UnsupportedError")
AlgorithmException: 'Type of media is not supported!'

Example 2: Propagating a valid error from algorithm to algorithm

Here is an example when we raise a valid error in one Python algorithm, and catch it in another.

Algorithm A:

import Algorithmia
from Algorithmia.errors import AlgorithmException

def apply(input):
    if input["type"] == "image":
        return("Image processed!")
    elif input["type"] == "video":
        return("Video processed!")
    else:
        raise AlgorithmException("Type of media is not supported!", "UnsupportedError")

Algorithm B:

import Algorithmia
from Algorithmia.errors import AlgorithmException

def apply(input):
    client = Algorithmia.client()

    try:
        client.algo("username_Algorithm_A").pipe(input)
        return("Our call went through")
    except AlgorithmException as e:
        if e.error_type == "UnsupportedError":
            return("Looks like our call is unsupported")

Algorithm B Sample Input:

{"type": "audio"}

Algorithm B Sample Output:

"Looks like our call is unsupported"

Example 3: Propagating an invalid error from algorithm to algorithm

When we pass an invalid error code from one algorithm to the other, it automatically get’s converted into a simple AlgorithmError error code.

Algorithm A:

import Algorithmia
from Algorithmia.errors import AlgorithmException

def apply(input):
    raise AlgorithmException("This is an invalid error code!", "BlablaException")

Algorithm B:

import Algorithmia
from Algorithmia.errors import AlgorithmException

def apply(input):
    client = Algorithmia.client()

    try:
        client.algo("username_Algorithm_A").pipe(input)
        return("Our call went through")
    except AlgorithmError as e:
        if e.error_type == "BlablaException":
            return("Our invalid error code was passed through.")
        elif e.error_type == "AlgorithmException":
            return("Out invalid error code was changed to the default.")

Algorithm B Sample Input:

"Some string."

Algorithm B Sample Output:

"Out invalid error code was changed to the default."