On Algorithmia you can develop in several different programming languages. This flexibility enables you to leverage your capabilities in the language(s) that you are most comfortable using, while also leveraging whichever libraries and functions best meet the needs of your specific use case. For example, you can call a NodeJS library from 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 are designed in the same way. This is especially true with regard to error and exception handling. Errors are often cryptic and there isn’t a standard way to understand what they actually mean.
As an example, let’s say you are calling a computer vision algorithm from a Java library and you get an error. If you are lucky, the author of that algorithm will have included a descriptive error message. However, depending on the message, you still might need to have a relatively strong grasp on Java programming, the Java compiler, and/or Java error types in order to understand what the error actually means, let alone to determine how to fix the issue in your code.
Algorithm Error Codes
To be able to better support algorithm development and use across languages, we decided to create a predefined, standardized list of error types. This list is reflective of the types of algorithms that are most commonly published on the Algorithmia platform.
Error Code | Error Use Cases | When To Raise |
---|---|---|
InputError | Invalid Input/Image/URL/Settings/JSON | Input, image, URL, settings, etc., provided is invalid/incomplete. |
UnsupportedError | Unsupported file/image, URL/Website, etc. | Image or other 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 cannot access any/additional RAM memory. |
OutOfGpuMemoryError | Out of GPU Memory | Algorithm cannot 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 | A general error class; returns for all other exception cases. |
Additionally, there are error codes that can only be generated on the back end. You’ll receive the following error codes if the corresponding error case ever happens:
Error Code | Error Use Cases | When To Raise |
---|---|---|
TimeoutError | When a child algorithm times out 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 gets 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("Our invalid error code was changed to the default.")
Algorithm B Sample Input:
"Some string."
Algorithm B Sample Output:
"Our invalid error code was changed to the default."