Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Melaporkan error fungsi runtime
Anda harus menangani dan melaporkan error runtime yang terjadi di fungsi Cloud Run.
Pengecualian yang tidak diketahui atau eksekusi yang menyebabkan error pada proses dapat menyebabkan
cold start,
yang umumnya harus Anda coba minimalkan.
Cara yang direkomendasikan agar fungsi mendeteksi error bergantung pada jenis
fungsi:
Fungsi HTTP akan menampilkan kode status HTTP yang sesuai, yang menunjukkan
error. Lihat Fungsi HTTP
untuk informasi lebih lanjut.
Fungsi berbasis peristiwa harus mencatat dan menampilkan pesan error. Lihat artikel Menulis fungsi berbasis peristiwa untuk mengetahui informasi selengkapnya.
Jika error ditangani dengan tepat, instance fungsi yang mengalami
error dapat tetap aktif dan tersedia untuk melayani permintaan.
Melaporkan error ke Error Reporting
Anda dapat melaporkan error dari fungsi Cloud Run ke Error Reporting seperti yang ditunjukkan di bawah ini:
Node.js
// These WILL be reported to Error ReportingthrownewError('I failed you');// Will cause a cold start if not caught
Python
@functions_framework.httpdefhello_error_1(request):# This WILL be reported to Error Reporting,# and WILL NOT show up in logs or# terminate the function.fromgoogle.cloudimporterror_reportingclient=error_reporting.Client()try:raiseRuntimeError("I failed you")exceptRuntimeError:client.report_exception()# This WILL be reported to Error Reporting,# and WILL terminate the functionraiseRuntimeError("I failed you")@functions_framework.httpdefhello_error_2(request):# These errors WILL NOT be reported to Error# Reporting, but will show up in logs.importloggingimportsysprint(RuntimeError("I failed you (print to stdout)"))logging.warning(RuntimeError("I failed you (logging.warning)"))logging.error(RuntimeError("I failed you (logging.error)"))sys.stderr.write("I failed you (sys.stderr.write)\n")# This is considered a successful execution and WILL NOT be reported# to Error Reporting, but the status code (500) WILL be logged.fromflaskimportabortreturnabort(500)
Go
packagetipsimport("fmt""net/http""os""github.com/GoogleCloudPlatform/functions-framework-go/functions")funcinit(){functions.HTTP("HTTPError",HTTPError)}// HTTPError describes how errors are handled in an HTTP function.funcHTTPError(whttp.ResponseWriter,r*http.Request){// An error response code is NOT reported to Error Reporting.// http.Error(w, "An error occurred", http.StatusInternalServerError)// Printing to stdout and stderr is NOT reported to Error Reporting.fmt.Println("An error occurred (stdout)")fmt.Fprintln(os.Stderr,"An error occurred (stderr)")// Calling log.Fatal sets a non-zero exit code and is NOT reported to Error// Reporting.// log.Fatal("An error occurred (log.Fatal)")// Panics are reported to Error Reporting.panic("An error occurred (panic)")}
Java
importcom.google.cloud.functions.HttpFunction;importcom.google.cloud.functions.HttpRequest;importcom.google.cloud.functions.HttpResponse;importjava.io.IOException;importjava.util.logging.Logger;publicclassHelloErrorimplementsHttpFunction{privatestaticfinalLoggerlogger=Logger.getLogger(HelloError.class.getName());@Overridepublicvoidservice(HttpRequestrequest,HttpResponseresponse)throwsIOException{// These will NOT be reported to Error ReportingSystem.err.println("I failed you");logger.severe("I failed you");// This WILL be reported to Error ReportingthrownewRuntimeException("I failed you");}}
Anda dapat melihat error yang dilaporkan di Error Reporting
pada konsol Google Cloud. Anda juga dapat melihat error yang dilaporkan dari
fungsi tertentu dengan memilihnya dari daftar fungsi di konsol Google Cloud.
Pengecualian yang tidak tertangkap yang dihasilkan oleh fungsi Anda akan muncul di Error Reporting.
Perhatikan bahwa beberapa jenis pengecualian yang tidak tertangkap (seperti yang ditampilkan
secara asinkron) akan menyebabkan cold
start terjadi pada
pemanggilan fungsi di masa mendatang. Ini akan meningkatkan jumlah waktu yang diperlukan
untuk menjalankan fungsi.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-04-11 UTC."],[[["Cloud Run functions should handle and report runtime errors to prevent cold starts and maintain availability."],["HTTP functions should use appropriate HTTP status codes to signal errors, while event-driven functions should log and return error messages."],["Unhandled exceptions in Node.js, Python, and Go runtimes are surfaced to the function caller and should be reviewed to ensure no sensitive information is exposed."],["Errors can be emitted to Error Reporting using language-specific methods, and some methods, like raising an exception, will both report and terminate the function, while other approaches may not terminate it."],["Uncaught exceptions are reported in Error Reporting and can lead to cold starts, which increase function execution time, so it's best to try to handle errors."]]],[]]