Release Notes
Announcements
import jsondef main_handler(event, context):print("Received event: " + json.dumps(event, indent = 2))print("Received context: " + str(context))return("Hello World")
index.main_handler, where index refers to the index.py entry file, and main_handler refers to the main_handler entry function. When submitting the zip code package by uploading a local zip package or uploading it through COS, the root directory of the package should contain the specified entry file and the file should contain the specified entry function. The filename and function name should match those entered in the execution method to ensure successful execution.return to return a value. The return value will be handled differently depending on the function invocation type.dict object can be directly returned:def main_handler(event, context):resp = {"isBase64Encoded": false,"statusCode": 200,"headers": {"Content-Type":"text/html","Key":["value1","value2","value3"]},"body": "<html><body><h1>Heading</h1><p>Paragraph.</p></body></html>"}return(resp)
raise Exception inside the function.return will be returned.
The following sample codes show that the function is successfully executed and will return Hello World.# -*- coding: utf8 -*-def main_handler(event, context):try:print("try exception")raise Exception("err msg")except Exception as e:print(e)return("Hello World")
# -*- coding: utf8 -*-def main_handler(event, context):print("try exception")raise Exception("err msg")return("Hello World")
{"errorCode":-1,"errorMessage":"user code exception caught","requestId":"a325b967-ef5b-4aa3-a329-c6bb0df72948","stackTrace":"Traceback (most recent call last):\\n File \\"/var/user/index.py\\", line 4, in main_handler\\n raise Exception(\\"err msg\\")\\nException: err msg","statusCode":430}
errorCode field indicates a code error, and errorMessage provides error details. The stackTrace field indicates an error stack, and statusCode provides error details. For more information about statusCode, see Function Status Code.Was this page helpful?
You can also Contact sales or Submit a Ticket for help.
Help us improve! Rate your documentation experience in 5 mins.
Feedback