Skip to content

Commit 058f033

Browse files
authored
Add error handling to composer GCF trigger sample (#732)
* Add error handling to composer GCF trigger sample * Fix lint * Remove serial from test definition.
1 parent 0322c5d commit 058f033

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

functions/composer-storage-trigger/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ function authorizeIap (clientId, projectId, userAgent) {
7979
})
8080
.then(res => res.json())
8181
.then(function obtainAccessTokenCallback (tokenResponse) {
82+
if (tokenResponse.error) {
83+
return Promise.reject(tokenResponse.error);
84+
}
8285
var accessToken = tokenResponse.access_token;
8386
var iat = Math.floor(new Date().getTime() / 1000);
8487
var claims = {
@@ -104,6 +107,9 @@ function authorizeIap (clientId, projectId, userAgent) {
104107
})
105108
.then(res => res.json())
106109
.then(function signJsonClaimCallback (body) {
110+
if (body.error) {
111+
return Promise.reject(body.error);
112+
}
107113
// Request service account signature on header and claimset
108114
var jwtSignature = body.signature;
109115
jwt = [JWT_HEADER, jwtClaimset, jwtSignature].join('.');
@@ -118,6 +124,9 @@ function authorizeIap (clientId, projectId, userAgent) {
118124
})
119125
.then(res => res.json())
120126
.then(function returnJwt (body) {
127+
if (body.error) {
128+
return Promise.reject(body.error);
129+
}
121130
return {
122131
jwt: jwt,
123132
idToken: body.id_token

functions/composer-storage-trigger/package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@
1616
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
1717
},
1818
"devDependencies": {
19-
"@google-cloud/nodejs-repo-tools": "^2.2.5"
19+
"@google-cloud/nodejs-repo-tools": "^2.2.5",
20+
"ava": "0.25.0",
21+
"proxyquire": "2.0.0",
22+
"semistandard": "^12.0.1",
23+
"sinon": "4.4.2"
2024
},
2125
"scripts": {
22-
"lint": "repo-tools lint"
26+
"lint": "repo-tools lint",
27+
"test": "ava -T 20s --verbose test/*.test.js"
2328
}
2429
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Copyright 2018 Google LLC
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+
const proxyquire = require(`proxyquire`).noCallThru();
19+
const sinon = require(`sinon`);
20+
const test = require(`ava`);
21+
22+
function getSample () {
23+
const bodyJson = {};
24+
const body = {
25+
json: sinon.stub().resolves(bodyJson)
26+
};
27+
const FetchMock = sinon.stub().resolves(body);
28+
29+
return {
30+
program: proxyquire(`../`, {
31+
'node-fetch': FetchMock
32+
}),
33+
mocks: {
34+
fetch: FetchMock,
35+
body: body,
36+
bodyJson: bodyJson
37+
}
38+
};
39+
}
40+
41+
test.cb(`Handles error in JSON body`, (t) => {
42+
const event = {
43+
data: {
44+
file: `some-file`
45+
}
46+
};
47+
const expectedMsg = `Something bad happened.`;
48+
const sample = getSample();
49+
sample.mocks.bodyJson.error = expectedMsg;
50+
51+
sample.program.triggerDag(event, (err, message) => {
52+
t.regex(err, /Something bad happened/);
53+
t.is(message, undefined);
54+
t.end();
55+
});
56+
});

0 commit comments

Comments
 (0)