All Products
Search
Document Center

Function Compute:Error handling

Last Updated:Apr 01, 2026

Function Compute surfaces Go runtime errors through the response body and HTTP headers. To detect errors, check the X-Fc-Error-Type response header — for example, X-Fc-Error-Type: UnhandledInvocationError. For a full list of error types, see the Error handling section in the Basics topic.

The Go runtime provides three ways to return error information from a handler function.

Error response format

Each method produces a different response body structure:

MethoderrorMessageerrorTypestackTrace
Return error from handlerError stringGo error type (e.g., errorString)Not included
Call panicPanic valueGo type of panic value (e.g., string)Included
Call os.Exit / log.FatalGeneric process-exit messageNot includedNot included

Return an error from the handler

Return an error value directly from the handler function. The SDK captures the error and serializes it into the response body.

package main

import (
    "errors"
    "fmt"

    "github.com/aliyun/fc-runtime-go-sdk/fc"
)

func HandleRequest() error {
    fmt.Println("hello world")
    return errors.New("something is wrong")
}

func main() {
    fc.Start(HandleRequest)
}

Response body:

{
  "errorMessage": "something is wrong!",
  "errorType": "errorString"
}

Use panic

Call panic when you need the response to include a stack trace. The SDK catches the panic and serializes the goroutine stack into the stackTrace array.

package main

import (
    "fmt"

    "github.com/aliyun/fc-runtime-go-sdk/fc"
)

func HandleRequest() error {
    fmt.Println("hello world")
    panic("Error: something is wrong")
    return nil
}

func main() {
    fc.Start(HandleRequest)
}

Response body (some stack frames omitted):

{
  "errorMessage": "Error: something is wrong",
  "errorType": "string",
  "stackTrace": [
    {
      "path": "github.com/aliyun/fc-runtime-go-sdk/fc/errors.go",
      "line": 39,
      "label": "fcPanicResponse"
    },
    {
      "path": "github.com/aliyun/fc-runtime-go-sdk/fc/function.go",
      "line": 84,
      "label": "(*Function).Invoke.func1"
    },
    ...
    { "path": "code/main.go", "line": 22, "label": "main" },
    { "path": "runtime/proc.go", "line": 255, "label": "main" },
    { "path": "runtime/asm_amd64.s", "line": 1581, "label": "goexit" }
  ]
}

Avoid os.Exit and log.Fatal

Important

Do not use os.Exit(1) or functions that wrap it, such as log.Fatal. When the process exits this way, the runtime cannot return an error message or stack trace — only a generic process-exit message is returned, which makes debugging difficult.

package main

import (
    "fmt"
    "log"

    "github.com/aliyun/fc-runtime-go-sdk/fc"
)

func HandleRequest() error {
    fmt.Println("hello world")
    log.Fatal("something is wrong")
    return nil
}

func main() {
    fc.Start(HandleRequest)
}

Response body:

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

No errorType or stackTrace fields are included.