This topic describes how to handle errors in the C# runtime environment of Function Compute.
Errors thrown by a function
If an error occurs during function executions, Function Compute captures the error and returns the error information.
Sample code:
using System;
using System.IO;
using System.Threading.Tasks;
using Aliyun.Serverless.Core;
using Microsoft.Extensions.Logging;
namespace Example
{
public class Hello
{
public async void StreamHandler(Stream input, IFcContext context)
{
throw new Exception("oops");
}
static void Main(string[] args){}
}
}
When a function is invoked, the following response is returned:
{
"errorMessage": "oops",
"errorType": "System.Exception",
"stackTrace": [...]
}
When an error occurs, the HTTP header in the response to the function invocation contains X-Fc-Error-Type UnhandledInvocationError
. For information about the error types of Function Compute, see Basics.
Proactive exit of a function
If an error occurs when a function proactively exists during running, you cannot obtain the error information or stack information. This method is not recommended.
Sample code:
using System;
using System.IO;
using System.Threading.Tasks;
using Aliyun.Serverless.Core;
using Microsoft.Extensions.Logging;
namespace Example
{
public class Hello
{
public async void StreamHandler(Stream input, IFcContext context)
{
System.Environment.Exit(1);
}
static void Main(string[] args){}
}
}
When a function is invoked, the following response is returned:
{
"errorMessage": "Process exited unexpectedly before completing request (duration: 45ms, maxMemoryUsage: 49MB)"
}