All Products
Search
Document Center

Function Compute:Error handling

Last Updated:Apr 08, 2024

This topic describes how to handle errors in a Node.js runtime.

Error types

Capture exceptions

If an exception is thrown during the execution of a function, Function Compute captures the error and generates JSON-formatted data that contains the error message, type, and stack information. The following code snippets show examples.

ECMAScript modules

Note
  • This example supports only Node.js 18 or later.

  • The sample code supports one-click deployment. You can deploy the sample code in Function Compute with one click. nodejs-fc-err-es

export const handler = async (event, context) => {
  throw new Error('oops');
};

CommonJS modules

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

Sample response:

{
    "errorMessage": "oops",
    "errorType": "Error",
    "stackTrace": [
        "Error: oops",
        "    at handler (file:///code/index.mjs:2:9)",
        "    at module.exports (file:///var/fc/runtime/nodejs20/bootstrap.mjs:5655:14)",
        "    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)"
    ]
}

Abnormal exits

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

ECMAScript modules

Note

This example supports only Node.js 18 or later.

export const handler = async (event, context) => {
  process.exit(1);
};

CommonJS modules

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

Sample response:

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

Troubleshooting

If the Function Compute encounters an error, it returns an HTTP status code, a response message, and an exception type that indicates the cause of the error. The client or service that invokes the function can handle the error with code or pass it to end users.

The following list describes the status codes that you may receive from a function.

  • 2xx

    2xx: Function Compute has received your request. If the response contains the X-Fc-Error-Type header, Function Compute has caught a function error, such as an exception thrown in the code.

  • 4xx

    4xx (excluding 429): An error occurs on the client that initiated the invocation.

    429: A request is throttled.

  • 5xx

    5xx: An internal error occurs in Function Compute, function configurations are invalid, or a resource issue exists.

For more information about invocation errors, see Error handling.