Skip to content

Commit c6aea9b

Browse files
authored
Add a snippet for error logging (#1081)
1 parent 9779c58 commit c6aea9b

File tree

4 files changed

+59
-40
lines changed

4 files changed

+59
-40
lines changed

2nd-gen/monitor-cloud-logging/functions/index.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ exports.getInspirationalQuote = onRequest(async (request, response) => {
5151
const today = new Date();
5252
const quoteOfTheMonthRef = db
5353
.collection("quotes")
54-
.doc(today.getFullYear())
54+
.doc(`${today.getFullYear()}`)
5555
.collection("months")
56-
.doc(today.getMonth());
56+
.doc(`${today.getMonth()}`);
5757

5858
const DEFAULT_QUOTE =
5959
"You miss 100% of the shots you don't take. -Wayne Gretzky";
@@ -79,10 +79,12 @@ exports.getInspirationalQuote = onRequest(async (request, response) => {
7979

8080
quote = DEFAULT_QUOTE;
8181
}
82-
} catch (e) {
83-
// Attach stack trace to errors
82+
// [START logError]
83+
} catch (err) {
84+
// Attach an error object as the second argument
8485
error("Unable to read quote from Firestore, sending default instead",
85-
{message: e.stack});
86+
err);
87+
// [END logError]
8688

8789
quote = DEFAULT_QUOTE;
8890
}

2nd-gen/monitor-cloud-logging/functions/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"shell": "firebase functions:shell",
88
"start": "npm run shell",
99
"deploy": "firebase deploy --only functions",
10-
"logs": "firebase functions:log"
10+
"logs": "firebase functions:log",
11+
"compile": "cp ../../../tsconfig.template.json ./tsconfig-compile.json && tsc --project tsconfig-compile.json"
1112
},
1213
"engines": {
1314
"node": "16"

pnpm-lock.yaml

Lines changed: 49 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ packages:
55
- 2nd-gen/custom-events/functions
66
- 2nd-gen/delete-unused-accounts-cron/functions
77
- 2nd-gen/instrument-with-opentelemetry/functions
8+
- 2nd-gen/monitor-cloud-logging/functions
89
- 2nd-gen/pubsub-helloworld/functions
910
- 2nd-gen/remote-config-diff/functions
1011
- 2nd-gen/taskqueues-backup-images/functions

0 commit comments

Comments
 (0)