Skip to content

Error

An error is a special kind of variable that is used to declare that something has gone wrong in the system. Modulo1 will declare an error automatically, if its rules have been violated. But it is also possible for you to write code that declares custom errors if your own rules have been violated by some circumstance within the application's current state.

For example, it might be a rule that a particular variable is only allowed to contain a value between 0 and 100. To enforce this rule, you could include the following within your app:

1
2
3
4
5
6
if (importantValue < 0 or importantValue > 100) {
    error >>>{
        message : "ImportantValue was not in the correct range",
        data : importantValue
    };
}

We start by using an if statement to evaluate the conditions that might trigger an error. And in the event that our if statement returns true (which would subsequently allow its code block to be run), the error that is sitting inside the if statement will be declared, which might potentially cause the code to shut down.


By default, all errors can cause the code to shut down immediately. But it is possible to write code in such a way that it can recover from many kinds of errors and avoids shutting down.

This is achieved by writing your code within a specialised kind of code block called a Try/Catch block:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
function calculate() returning null {
    try {
        var result1 = doSomeStuff();

        if (result1 < 0 or result1 > 100) {
            error >>>{
                message : "Result was not in the correct range",
                data : result1
            };
        }

        var result2 = doMoreStuff();

    }

    catch (err) {
        print(err.message + "\nThe result was " + err.data);
    }
}

When Modulo1 encounters the try section, it will run normally until the error is encountered. In this example, that will occur if the result1 variable is not between 0 and 100. If that does not occur, it is quite possible that no errors will be declared in this function, and therefore Modulo1 will ignore the catch section.

But if the error is declared, however, the try section will stop running, and then Modulo1 will run the catch section. Modulo1 will pass the error along to the catch section so that the error's information is available. In this case, the catch block has chosen to refer to the error by the name err, but you can choose to use any name you like.

The catch section is intended to contain whatever code is neccessary for your code to recover from the problem. In many cases you don't need to do anything at all to recover, and it might be enough to simply print our the error message, along with any variables that were stored within the error.


Syntax

To create an error without any message or data:

1
error >>>{ };

To create an error with a message:

1
error >>>{ message : "Something went wrong!" };

Spaces and line breaks are not important. The following achieves the exact same result:

1
2
3
error >>>{ 
    message : "Something went wrong!"
};

To create an error with a message and some additional data:

1
2
3
4
error >>>{
    message : "Something went wrong!",
    extraData : "Here is some data"
};

You can attach almost any kind of variable to an error. Here is another example:

1
2
3
4
5
var myNumbers = [1, 2, 3];
error >>>{
    message : "Something went wrong!",
    extraData : myNumbers
};

Properties

A property is a special feature of a variable that can be used to get and/or set some aspect of that variable.

message

The message property can be used to retrieve the message that was included in the error declaration. If no message was included, then this will return null

1
var result = err.message;

type

The type property can be used to find out the data-type of a particular error variable. The result will always be "error"

1
var result = err.type;

Functions

There are no functions specific to error variables