Hello World error
Stay organized with collections
Save and categorize content based on your preferences.
Demonstrates how to report an error.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","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)."]]