Error Handling

As with any function call, your call to an Algorithm may cause an error. Usually this happens because you've sent it unexpected input, or a resource the Algorithm depends on (such as a public data feed) is unavailable, or occasionally because of a coding error in the Algorithm itself.

When this happens, the call will raise (throw) an Exception, usually of the type AlgorithmiaException:

Here, the error is caused because demo/Hello expects a String as input, but was passed an empty List []. The exception raised in the local Python block is an Algorithmia.errors.AlgorithmException, but its message (generated from inside the Algorithm we called) hints at the problem: "Expected STRING but was BEGIN_ARRAY", indicating that the (Java) code of the actual Algorithm we called received an Array (cast from the List we sent) instead of a String.

We can trap errors like this via a standard try/except block (aka try/catch).

One notable exception to how exceptions are handled: in the JavaScript clients (browser and Node), or cURL / raw POST, the object returned from algo.pipe(input) will contain a property .error if there is an exception, and .error.message will contain the message:

To learn more, see the API Docs.

Now it's time to learn how to set Timeouts and other Query Parameters.

 


NEXT