|
| 1 | +// Copyright 2016, Google, Inc. |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +'use strict'; |
| 15 | + |
| 16 | +// [START setup] |
| 17 | +var gcloud = require('gcloud'); |
| 18 | + |
| 19 | +// Get a reference to the StackDriver Logging component |
| 20 | +var logging = gcloud.logging(); |
| 21 | +// [END setup] |
| 22 | + |
| 23 | +// [START reportDetailedError] |
| 24 | +var reportDetailedError = require('./report'); |
| 25 | +// [END reportDetailedError] |
| 26 | + |
| 27 | +// [START helloSimpleErrorReport] |
| 28 | +/** |
| 29 | + * Report an error to StackDriver Error Reporting. Writes the minimum data |
| 30 | + * required for the error to be picked up by StackDriver Error Reporting. |
| 31 | + * |
| 32 | + * @param {Error} err The Error object to report. |
| 33 | + * @param {Function} callback Callback function. |
| 34 | + */ |
| 35 | +function reportError (err, callback) { |
| 36 | + // This is the name of the StackDriver log stream that will receive the log |
| 37 | + // entry. This name can be any valid log stream name, but must contain "err" |
| 38 | + // in order for the error to be picked up by StackDriver Error Reporting. |
| 39 | + var logName = 'errors'; |
| 40 | + var log = logging.log(logName); |
| 41 | + |
| 42 | + // https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/MonitoredResource |
| 43 | + var monitoredResource = { |
| 44 | + type: 'cloud_function', |
| 45 | + labels: { |
| 46 | + function_name: process.env.FUNCTION_NAME |
| 47 | + } |
| 48 | + }; |
| 49 | + |
| 50 | + // https://cloud.google.com/error-reporting/reference/rest/v1beta1/ErrorEvent |
| 51 | + var errorEvent = { |
| 52 | + message: err.stack, |
| 53 | + serviceContext: { |
| 54 | + service: 'cloud_function:' + process.env.FUNCTION_NAME, |
| 55 | + version: require('./package.json').version || 'unknown' |
| 56 | + } |
| 57 | + }; |
| 58 | + |
| 59 | + // Write the error log entry |
| 60 | + log.write(log.entry(monitoredResource, errorEvent), callback); |
| 61 | +} |
| 62 | +// [END helloSimpleErrorReport] |
| 63 | + |
| 64 | +// [START helloSimpleError] |
| 65 | +/** |
| 66 | + * HTTP Cloud Function. |
| 67 | + * |
| 68 | + * @param {Object} req Cloud Function request object. |
| 69 | + * @param {Object} res Cloud Function response object. |
| 70 | + */ |
| 71 | +exports.helloSimpleError = function helloSimpleError (req, res) { |
| 72 | + try { |
| 73 | + if (req.method !== 'GET') { |
| 74 | + var error = new Error('Only GET requests are accepted!'); |
| 75 | + error.code = 405; |
| 76 | + throw error; |
| 77 | + } |
| 78 | + // All is good, respond to the HTTP request |
| 79 | + return res.send('Hello World!'); |
| 80 | + } catch (err) { |
| 81 | + // Report the error |
| 82 | + return reportError(err, function () { |
| 83 | + // Now respond to the HTTP request |
| 84 | + return res.status(error.code || 500).send(err.message); |
| 85 | + }); |
| 86 | + } |
| 87 | +}; |
| 88 | +// [END helloSimpleError] |
| 89 | + |
| 90 | +// [START helloHttpError] |
| 91 | +/** |
| 92 | + * HTTP Cloud Function. |
| 93 | + * |
| 94 | + * @param {Object} req Cloud Function request object. |
| 95 | + * @param {Object} res Cloud Function response object. |
| 96 | + */ |
| 97 | +exports.helloHttpError = function helloHttpError (req, res) { |
| 98 | + try { |
| 99 | + if (req.method !== 'POST' && req.method !== 'GET') { |
| 100 | + var error = new Error('Only POST and GET requests are accepted!'); |
| 101 | + error.code = 405; |
| 102 | + throw error; |
| 103 | + } |
| 104 | + // All is good, respond to the HTTP request |
| 105 | + return res.send('Hello ' + (req.body.message || 'World') + '!'); |
| 106 | + } catch (err) { |
| 107 | + // Set the response status code before reporting the error |
| 108 | + res.status(err.code || 500); |
| 109 | + // Report the error |
| 110 | + return reportDetailedError(err, req, res, function () { |
| 111 | + // Now respond to the HTTP request |
| 112 | + return res.send(err.message); |
| 113 | + }); |
| 114 | + } |
| 115 | +}; |
| 116 | +// [END helloHttpError] |
| 117 | + |
| 118 | +// [START helloBackgroundError] |
| 119 | +/** |
| 120 | + * Background Cloud Function. |
| 121 | + * |
| 122 | + * @param {Object} context Cloud Function context object. |
| 123 | + * @param {Object} data Request data, provided by a trigger. |
| 124 | + * @param {string} data.message Message, provided by the trigger. |
| 125 | + */ |
| 126 | +exports.helloBackgroundError = function helloBackgroundError (context, data) { |
| 127 | + try { |
| 128 | + if (!data.message) { |
| 129 | + throw new Error('"message" is required!'); |
| 130 | + } |
| 131 | + // All is good, respond with a message |
| 132 | + return context.success('Hello World!'); |
| 133 | + } catch (err) { |
| 134 | + // Report the error |
| 135 | + return reportDetailedError(err, function () { |
| 136 | + // Now finish mark the execution failure |
| 137 | + return context.failure(err.message); |
| 138 | + }); |
| 139 | + } |
| 140 | +}; |
| 141 | +// [END helloBackgroundError] |
0 commit comments