Yandex Cloud
Search
Contact UsGet started
  • Blog
  • Pricing
  • Documentation
  • All Services
  • System Status
    • Featured
    • Infrastructure & Network
    • Data Platform
    • Containers
    • Developer tools
    • Serverless
    • Security
    • Monitoring & Resources
    • ML & AI
    • Business tools
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Customer Stories
    • Start testing with double trial credits
    • Cloud credits to scale your IT product
    • Gateway to Russia
    • Cloud for Startups
    • Education and Science
    • Yandex Cloud Partner program
  • Blog
  • Pricing
  • Documentation
© 2025 Direct Cursus Technology L.L.C.
Yandex Cloud Functions
  • Comparison with other Yandex Cloud services
    • Overview
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Handling errors
    • Using the SDK
  • Tools
  • Pricing policy
  • Access management
  • Terraform reference
  • Monitoring metrics
  • Audit Trails events
  • Release notes
  • FAQ
  1. Developing in Kotlin
  2. Handling errors

Kotlin function error handling

Written by
Yandex Cloud
Updated at November 2, 2024

If a handler reports a function runtime or loading error, the runtime environment automatically captures the error and returns a JSON document with the error type in the response. For more information about the JSON document format, see Calling a function. Error info is written to the execution log.

Examples of error handlingExamples of error handling

Let's assume a handler returns a sum of numbers. An empty request will cause the handler to throw IllegalArgumentException.

Function code:

fun handle(request: IntArray): Int {
    if (request.isEmpty()) throw IllegalArgumentException()
    return request.sum()
}

Case 1. When invoking a function, the user provides the [1, 22, 333] array:

curl \
     --header "Authorization: Bearer <IAM_token>" \
     --data "[1, 22, 333]" \
     "https://functions.yandexcloud.net/<function_ID>?integration=raw"

The function returns 356, the sum of the numbers.

Case 2. The user provides an empty array:

curl \
     --header "Authorization: Bearer <IAM_token>" \
     --data "[]" \
     "https://functions.yandexcloud.net/<function_ID>?integration=raw"

The runtime environment captures the exception and generates a JSON document that contains an error message (errorMessage field), error type (errorType field), and stack trace (stackTrace field).

JSON document returned:

{
    "errorMessage": "An exception has occurred in the user code, details: java.lang.IllegalArgumentException. [ERR_USER_CODE]",
    "errorType": "IllegalArgumentException",
    "stackTrace": [
        {
            "function": "handle",
            "file": "Handler.kt",
            "line": 2
        },
            ...
    ]
}

Case 3. The user provides a string instead of numbers, which results in a deserialization error:

curl \
     --header "Authorization: Bearer <IAM_token>" \
     --data "notanarray" \
     "https://functions.yandexcloud.net/<function_ID>?integration=raw"

JSON document returned:

{
    "errorMessage": "An exception has occurred during request deserialization. Details: com.squareup.moshi.JsonEncodingException: Use JsonReader.setLenient(true) to accept malformed JSON at path $. [ERR_INVALID_REQUEST]",
    "errorType": "FunctionException"
}

Was the article helpful?

Previous
Logging
Next
Using the SDK
© 2025 Direct Cursus Technology L.L.C.