|
| 1 | +/** |
| 2 | + * Copyright 2018, Google, Inc. |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +'use strict'; |
| 17 | + |
| 18 | +/** |
| 19 | + * HTTP Cloud Function that demonstrates |
| 20 | + * how to catch errors of different types. |
| 21 | + * |
| 22 | + * @param {Object} req Cloud Function request context. |
| 23 | + * @param {Object} req.body Cloud Function request context body. |
| 24 | + * @param {String} req.body.topic The Cloud Pub/Sub topic to publish to. |
| 25 | + * @param {Object} res Cloud Function response context. |
| 26 | + */ |
| 27 | +exports.errorTypes = (req, res) => { |
| 28 | + // [START functions_concepts_error_object] |
| 29 | + try { |
| 30 | + // Throw an Error object (to simulate a GCP API failure) |
| 31 | + throw new Error('Error object!'); |
| 32 | + } catch (err) { |
| 33 | + // err is already an Error object |
| 34 | + console.error(err); |
| 35 | + } |
| 36 | + // [END functions_concepts_error_object] |
| 37 | + |
| 38 | + const someCondition = !!req.body.throwAsString; |
| 39 | + |
| 40 | + /* eslint-disable no-throw-literal */ |
| 41 | + // [START functions_concepts_error_unknown] |
| 42 | + try { |
| 43 | + // Throw an unknown error type |
| 44 | + if (someCondition) { |
| 45 | + throw 'Error string!'; |
| 46 | + } else { |
| 47 | + throw new Error('Error object!'); |
| 48 | + } |
| 49 | + } catch (err) { |
| 50 | + // Determine the error type |
| 51 | + if (err instanceof Error) { |
| 52 | + console.error(err); |
| 53 | + } else { |
| 54 | + console.error(new Error(err)); |
| 55 | + } |
| 56 | + } |
| 57 | + // [END functions_concepts_error_unknown] |
| 58 | + /* eslint-enable no-throw-literal */ |
| 59 | + |
| 60 | + res.end(); |
| 61 | +}; |
| 62 | + |
| 63 | +// [START functions_concepts_stateless] |
| 64 | +// Global variable, but only shared within function instance. |
| 65 | +let count = 0; |
| 66 | + |
| 67 | +/** |
| 68 | + * HTTP Cloud Function that counts how many times |
| 69 | + * it is executed within a specific instance. |
| 70 | + * |
| 71 | + * @param {Object} req Cloud Function request context. |
| 72 | + * @param {Object} res Cloud Function response context. |
| 73 | + */ |
| 74 | +exports.executionCount = (req, res) => { |
| 75 | + count++; |
| 76 | + |
| 77 | + // Note: the total function invocation count across |
| 78 | + // all instances may not be equal to this value! |
| 79 | + res.send(`Instance execution count: ${count}`); |
| 80 | +}; |
| 81 | +// [END functions_concepts_stateless] |
| 82 | + |
| 83 | +// [START functions_concepts_after_response] |
| 84 | +/** |
| 85 | + * HTTP Cloud Function that may not completely |
| 86 | + * execute due to early HTTP response |
| 87 | + * |
| 88 | + * @param {Object} req Cloud Function request context. |
| 89 | + * @param {Object} res Cloud Function response context. |
| 90 | + */ |
| 91 | +exports.afterResponse = (req, res) => { |
| 92 | + res.end(); |
| 93 | + |
| 94 | + // This statement may not execute |
| 95 | + console.log('Function complete!'); |
| 96 | +}; |
| 97 | +// [END functions_concepts_after_response] |
| 98 | + |
| 99 | +// [START functions_concepts_after_timeout] |
| 100 | +/** |
| 101 | + * HTTP Cloud Function that may not completely |
| 102 | + * execute due to function execution timeout |
| 103 | + * |
| 104 | + * @param {Object} req Cloud Function request context. |
| 105 | + * @param {Object} res Cloud Function response context. |
| 106 | + */ |
| 107 | +exports.afterTimeout = (req, res) => { |
| 108 | + setTimeout(() => { |
| 109 | + // May not execute if function's timeout is <2 minutes |
| 110 | + console.log('Function running...'); |
| 111 | + res.end(); |
| 112 | + }, 120000); // 2 minute delay |
| 113 | +}; |
| 114 | +// [END functions_concepts_after_timeout] |
| 115 | + |
| 116 | +// [START functions_concepts_filesystem] |
| 117 | +const fs = require('fs'); |
| 118 | + |
| 119 | +/** |
| 120 | + * HTTP Cloud Function that lists files in the function directory |
| 121 | + * |
| 122 | + * @param {Object} req Cloud Function request context. |
| 123 | + * @param {Object} res Cloud Function response context. |
| 124 | + */ |
| 125 | +exports.listFiles = (req, res) => { |
| 126 | + fs.readdir(__dirname, (err, files) => { |
| 127 | + if (err) { |
| 128 | + console.error(err); |
| 129 | + res.sendStatus(500); |
| 130 | + } else { |
| 131 | + console.log('Files', files); |
| 132 | + res.sendStatus(200); |
| 133 | + } |
| 134 | + }); |
| 135 | +}; |
| 136 | +// [END functions_concepts_filesystem] |
| 137 | + |
| 138 | +// [START functions_concepts_modules] |
| 139 | +const path = require('path'); |
| 140 | +const loadedModule = require(path.join(__dirname, 'loadable.js')); |
| 141 | + |
| 142 | +/** |
| 143 | + * HTTP Cloud Function that runs a function loaded from another Node.js file |
| 144 | + * |
| 145 | + * @param {Object} req Cloud Function request context. |
| 146 | + * @param {Object} res Cloud Function response context. |
| 147 | + */ |
| 148 | +exports.runLoadedModule = (req, res) => { |
| 149 | + console.log(`Loaded function from file ${loadedModule.getFileName()}`); |
| 150 | + res.end(); |
| 151 | +}; |
| 152 | +// [END functions_concepts_modules] |
| 153 | + |
| 154 | +// [START functions_concepts_requests] |
| 155 | +const request = require('request'); |
| 156 | + |
| 157 | +/** |
| 158 | + * HTTP Cloud Function that makes an HTTP request |
| 159 | + * |
| 160 | + * @param {Object} req Cloud Function request context. |
| 161 | + * @param {Object} res Cloud Function response context. |
| 162 | + */ |
| 163 | +exports.makeRequest = (req, res) => { |
| 164 | + // The URL to send the request to |
| 165 | + const url = 'https://example.com'; |
| 166 | + |
| 167 | + request(url, (err, response) => { |
| 168 | + if (!err && response.statusCode === 200) { |
| 169 | + res.sendStatus(200); |
| 170 | + } else { |
| 171 | + res.sendStatus(500); |
| 172 | + } |
| 173 | + }); |
| 174 | +}; |
| 175 | +// [END functions_concepts_requests] |
0 commit comments