Method Error Handling
Error messages are useful because they allow developers to create and identify a controlled failure, like showing a nice message to a user instead of crashing or just doing nothing.
It is not required to specify that methods might throw an error, or what kind of error they might throw, in the method definition. The C3 Agentic AI Platform assumes that any method can throw an error.
Examples
The C3 Agentic AI Platform allows developers to create errors in the normal way for their specified implementation language, and translate those errors into a common format for consumption in other languages and pieces of the C3 Agentic AI Platform.
See below for how to implement method error handling.
Leverage the Type System
In this example, a type called Calculator has been defined with two methods: divide and fractionToDecimal.
If a user simply calls Calculator.divide(), they will get an error message because the method requires two parameters: a numerator and a denominator.
File: Calculator.c3typ
type Calculator {
divide : function (numerator: !int, denominator: !int) : double js-server
fractionToDecimal: function (fraction: string) : double js-server
}Throw
It is possible to be specific with the error messages using "throw" syntax. In Calculator.js, the divide method has been implemented to output the message, "Cannot divide by 0", when the denominator input is 0.
File: Calculator.js
function divide(numerator, denominator) {
if (denominator == 0)
throw "Cannot divide by 0!";
return (numerator / denominator);
}Try / Catch
Additionally, within method error handling, developers can use try / catch syntax. The try statement defines a code block to run (to try). The catch statement defines a code block to handle any error. When transforming a fraction to a decimal, the divide method is able to "catch" the divide by 0 error and reformat it to a more context appropriate error for the user.
File: Calculator.js
function fractionToDecimal(fraction) {
var parts = fraction.split("/");
try {
return Calculator.divide(parts[0], parts[1]);
} catch(e) {
throw "Looks like your denominator is a 0!";
}
}