Hello World 錯誤
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
示範如何回報錯誤。
深入探索
如需包含這個程式碼範例的詳細說明文件,請參閱下列內容:
程式碼範例
Java
如要驗證 Cloud Run 函式,請設定應用程式預設憑證。
詳情請參閱「為本機開發環境設定驗證」。
Node.js
如要驗證 Cloud Run 函式,請設定應用程式預設憑證。
詳情請參閱「為本機開發環境設定驗證」。
Python
如要驗證 Cloud Run 函式,請設定應用程式預設憑證。
詳情請參閱「為本機開發環境設定驗證」。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis page demonstrates how to report errors in Cloud Run functions across multiple languages, including Go, Java, Node.js, and Python.\u003c/p\u003e\n"],["\u003cp\u003ePanics in Go code and uncaught exceptions in Java and Node.js will be reported to Error Reporting.\u003c/p\u003e\n"],["\u003cp\u003eIn Python, you can report exceptions to Error Reporting using the \u003ccode\u003egoogle.cloud.error_reporting\u003c/code\u003e library, and you may choose to either terminate the function or not.\u003c/p\u003e\n"],["\u003cp\u003ePrinting to \u003ccode\u003estdout\u003c/code\u003e or \u003ccode\u003estderr\u003c/code\u003e, using \u003ccode\u003elog.Fatal\u003c/code\u003e in Go, or logging severe errors in Java, and calling \u003ccode\u003eabort\u003c/code\u003e in python will not be reported to error reporting.\u003c/p\u003e\n"],["\u003cp\u003eSetting up Application Default Credentials is necessary for authenticating to Cloud Run functions.\u003c/p\u003e\n"]]],[],null,["# Hello World error\n\nDemonstrates how to report an error.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Report errors](https://firebase.google.com/docs/functions/reporting-errors)\n- [Report runtime function errors (1st gen)](/functions/1stgendocs/monitoring/error-reporting)\n\nCode sample\n-----------\n\n### Go\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n package tips\n\n import (\n \t\"fmt\"\n \t\"net/http\"\n \t\"os\"\n\n \t\"github.com/GoogleCloudPlatform/functions-framework-go/functions\"\n )\n\n func init() {\n \tfunctions.HTTP(\"HTTPError\", HTTPError)\n }\n\n // HTTPError describes how errors are handled in an HTTP function.\n func HTTPError(w http.ResponseWriter, r *http.Request) {\n \t// An error response code is NOT reported to Error Reporting.\n \t// http.Error(w, \"An error occurred\", http.StatusInternalServerError)\n\n \t// Printing to stdout and stderr is NOT reported to Error Reporting.\n \tfmt.Println(\"An error occurred (stdout)\")\n \tfmt.Fprintln(os.Stderr, \"An error occurred (stderr)\")\n\n \t// Calling log.Fatal sets a non-zero exit code and is NOT reported to Error\n \t// Reporting.\n \t// log.Fatal(\"An error occurred (log.Fatal)\")\n\n \t// Panics are reported to Error Reporting.\n \tpanic(\"An error occurred (panic)\")\n }\n\n### Java\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n import com.google.cloud.functions.HttpFunction;\n import com.google.cloud.functions.HttpRequest;\n import com.google.cloud.functions.HttpResponse;\n import java.io.IOException;\n import java.util.logging.Logger;\n\n public class HelloError implements HttpFunction {\n\n private static final Logger logger = Logger.getLogger(HelloError.class.getName());\n\n @Override\n public void service(HttpRequest request, HttpResponse response)\n throws IOException {\n // These will NOT be reported to Error Reporting\n System.err.println(\"I failed you\");\n logger.severe(\"I failed you\");\n\n // This WILL be reported to Error Reporting\n throw new RuntimeException(\"I failed you\");\n }\n }\n\n### Node.js\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n // These WILL be reported to Error Reporting\n throw new Error('I failed you'); // Will cause a cold start if not caught\n\n### Python\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n @functions_framework.http\n def hello_error_1(request):\n # This WILL be reported to Error Reporting,\n # and WILL NOT show up in logs or\n # terminate the function.\n from google.cloud import error_reporting\n\n client = error_reporting.https://cloud.google.com/python/docs/reference/clouderrorreporting/latest/google.cloud.error_reporting.client.Client.html()\n\n try:\n raise RuntimeError(\"I failed you\")\n except RuntimeError:\n https://cloud.google.com/python/docs/reference/clouderrorreporting/latest/google.cloud.error_reporting.client.html.https://cloud.google.com/python/docs/reference/clouderrorreporting/latest/google.cloud.error_reporting.client.Client.html#google_cloud_error_reporting_client_Client_report_exception()\n\n # This WILL be reported to Error Reporting,\n # and WILL terminate the function\n raise RuntimeError(\"I failed you\")\n\n\n @functions_framework.http\n def hello_error_2(request):\n # These errors WILL NOT be reported to Error\n # Reporting, but will show up in logs.\n import logging\n import sys\n\n print(RuntimeError(\"I failed you (print to stdout)\"))\n logging.warning(RuntimeError(\"I failed you (logging.warning)\"))\n logging.error(RuntimeError(\"I failed you (logging.error)\"))\n sys.stderr.write(\"I failed you (sys.stderr.write)\\n\")\n\n # This is considered a successful execution and WILL NOT be reported\n # to Error Reporting, but the status code (500) WILL be logged.\n from flask import abort\n\n return abort(500)\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=functions)."]]