Skip to content

Commit cc2f3e5

Browse files
committed
Adding error handling to Stripe examples charge customer
Change-Id: I077a3aabc74bd1e44301ada7e41e18d6e344b0d0
1 parent 6a4dd15 commit cc2f3e5

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

stripe/functions/index.js

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
1+
/**
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
'use strict';
17+
118
const functions = require('firebase-functions'),
2-
admin = require('firebase-admin');
19+
admin = require('firebase-admin'),
20+
logging = require('@google-cloud/logging')();
321

422
admin.initializeApp(functions.config().firebase);
523

624
const stripe = require('stripe')(functions.config().stripe.token),
725
currency = functions.config().stripe.currency || 'USD';
826

27+
// [START chargecustomer]
928
exports.createStripeCharge = functions.database.ref('/users/{userId}/charges/{id}').onWrite(event => {
1029
const val = event.data.val();
1130
if (val === null || val.id || val.error) return null;
@@ -18,10 +37,15 @@ exports.createStripeCharge = functions.database.ref('/users/{userId}/charges/{id
1837
}).then(response => {
1938
return event.data.ref.set(response);
2039
}, error => {
21-
return event.data.ref.child('error').set(error.message);
40+
// We want to capture errors and render them in a user-friendly way, while
41+
// still logging an exception with Stackdriver
42+
return event.data.ref.child('error').set(userFacingMessage(error)).then(() => {
43+
return reportError(error, {user: event.params.userId});
44+
});
2245
}
2346
);
2447
});
48+
// [END chargecustomer]]
2549

2650
exports.createStripeCustomer = functions.auth.user().onCreate(event => {
2751
const data = event.data;
@@ -41,3 +65,45 @@ exports.cleanupUser = functions.auth.user().onDelete(event => {
4165
return admin.database().ref(`/stripe_customers/${event.data.uid}`).remove();
4266
});
4367
});
68+
69+
// [START reporterror]
70+
function reportError(err, context = {}) {
71+
// This is the name of the StackDriver log stream that will receive the log
72+
// entry. This name can be any valid log stream name, but must contain "err"
73+
// in order for the error to be picked up by StackDriver Error Reporting.
74+
const logName = 'errors';
75+
const log = logging.log(logName);
76+
77+
const metadata = {
78+
// https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/MonitoredResource
79+
resource: {
80+
type: 'cloud_function',
81+
labels: { function_name: process.env.FUNCTION_NAME }
82+
}
83+
};
84+
85+
// https://cloud.google.com/error-reporting/reference/rest/v1beta1/ErrorEvent
86+
const errorEvent = {
87+
message: err.stack,
88+
serviceContext: {
89+
service: process.env.FUNCTION_NAME,
90+
resourceType: 'cloud_function'
91+
},
92+
context: context
93+
};
94+
95+
// Write the error log entry
96+
return new Promise((resolve, reject) => {
97+
log.write(log.entry(metadata, errorEvent), error => {
98+
if (error) { reject(error); }
99+
resolve();
100+
});
101+
});
102+
}
103+
// [END reporterror]
104+
105+
function userFacingMessage(error) {
106+
return error.type ? error.message : 'An error occurred, developers have been alerted';
107+
}
108+
109+

stripe/functions/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "functions",
33
"description": "Firebase Functions",
44
"dependencies": {
5+
"@google-cloud/logging": "^0.7.1",
56
"firebase-admin": "^4.0.5",
67
"firebase-functions": "https://storage.googleapis.com/firebase-preview-drop/node/firebase-functions/firebase-functions-preview.latest.tar.gz",
78
"stripe": "^4.15.0"

0 commit comments

Comments
 (0)