This topic describes how to handle errors in a Node.js runtime environment, including error types and exception information.

Error types

The errors for Node.js functions are classified into the following types:
  • HandledInvocationError: the errors that are returned by the first parameter of callback.

    Run the following code to call the callback command:

    exports.handler = function(event, context, callback) {
      callback(new Error('oops'));
    };

    Sample response:

    {
      "errorMessage":"oops",
      "errorType":"Error",
      "stackTrace":[
        "Error: oops","    at exports.handler (/code/index.js:2:12)"
      ]}
  • FunctionUnhandledError: the errors that are returned after Function Compute captures exceptions.

    If your code throws an exception that is not captured, Function Compute captures the exception and returns an error message.

    exports.handler = function(event, context, callback) {
      throw new Error('oops');
    };

    Sample response:

    {
      "errorMessage":"oops",
      "errorType":"FunctionUnhandledError: Error",
      "stackTrace":[
        "Error: oops","    at exports.handler (/code/index.js:2:9)"
      ]}

    If your function proactively exits while it is running, the system returns a general error message.

    exports.handler = function(event, context, callback) {
      process.exit(1);
    };

    Sample response:

    {
      "errorMessage":"Process exited unexpectedly before completing request (duration: 6ms, maxMemoryUsage: 49MB)"
    }

Error information

The following table describes the fields in the error information.
Field Type Description
errorMessage String The error message.
errorType String The error type.
stackTrace List The error stack.

For more information, see Error handling.