gcf-common-lib 0.25.36 → 0.25.37
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/index.js +12 -11
- package/src/index.ts +13 -11
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -66,26 +66,27 @@ class GcfCommon {
|
|
|
66
66
|
*/
|
|
67
67
|
static process(event, context, handler, timeout = 535) {
|
|
68
68
|
return __awaiter(this, void 0, void 0, function* () {
|
|
69
|
-
return Promise.race([(0, utils_1.timeoutAfter)(timeout), handler(event, context)])
|
|
69
|
+
return Promise.race([(0, utils_1.timeoutAfter)(timeout), (handler !== null && handler !== void 0 ? handler : (() => Promise.resolve(undefined)))(event, context)])
|
|
70
70
|
.then((res) => __awaiter(this, void 0, void 0, function* () {
|
|
71
71
|
// console.log('res:', res);
|
|
72
72
|
yield this.publish(event, context, res);
|
|
73
73
|
}))
|
|
74
74
|
.catch((err) => __awaiter(this, void 0, void 0, function* () {
|
|
75
|
-
|
|
76
|
-
const fname = (_b = (_a = process === null || process === void 0 ? void 0 : process.env) === null || _a === void 0 ? void 0 : _a.K_SERVICE) !== null && _b !== void 0 ? _b : 'UNKNOWN';
|
|
77
|
-
const response = {
|
|
78
|
-
error: {
|
|
79
|
-
name: err.name,
|
|
80
|
-
message: `GCF [${fname}]: ${err.message}`,
|
|
81
|
-
stack: err.stack,
|
|
82
|
-
},
|
|
83
|
-
};
|
|
84
|
-
yield this.publish(event, context, response, { error: '1' }).catch(noop_1.default);
|
|
75
|
+
yield this.publish(event, context, GcfCommon.buildResponse(err), { error: '1' }).catch(noop_1.default);
|
|
85
76
|
throw err;
|
|
86
77
|
}));
|
|
87
78
|
});
|
|
88
79
|
}
|
|
80
|
+
static buildResponse(error) {
|
|
81
|
+
var _a, _b;
|
|
82
|
+
return {
|
|
83
|
+
error: {
|
|
84
|
+
name: error.name,
|
|
85
|
+
message: `GCF [${(_b = (_a = process === null || process === void 0 ? void 0 : process.env) === null || _a === void 0 ? void 0 : _a.K_SERVICE) !== null && _b !== void 0 ? _b : 'UNKNOWN'}]: ${error.message}`,
|
|
86
|
+
stack: error.stack,
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
}
|
|
89
90
|
static publish(event, context, json, attributes) {
|
|
90
91
|
return __awaiter(this, void 0, void 0, function* () {
|
|
91
92
|
const { topic, exchange, queue, appId, env, requestId } = yield this.safeGetAttributes(event, context, [
|
package/src/index.ts
CHANGED
|
@@ -43,28 +43,30 @@ export class GcfCommon {
|
|
|
43
43
|
static async process<T extends TResponse>(
|
|
44
44
|
event: TEvent,
|
|
45
45
|
context: TContext,
|
|
46
|
-
handler
|
|
46
|
+
handler?: (event: TEvent, context: TContext) => Promise<T>,
|
|
47
47
|
timeout = 535,
|
|
48
48
|
) {
|
|
49
|
-
return Promise.race([timeoutAfter(timeout), handler(event, context)])
|
|
49
|
+
return Promise.race([timeoutAfter(timeout), (handler ?? (() => Promise.resolve(undefined)))(event, context)])
|
|
50
50
|
.then(async (res: T | undefined) => {
|
|
51
51
|
// console.log('res:', res);
|
|
52
52
|
await this.publish(event, context, res);
|
|
53
53
|
})
|
|
54
54
|
.catch(async (err: Error) => {
|
|
55
|
-
|
|
56
|
-
const response: TResponse = {
|
|
57
|
-
error: {
|
|
58
|
-
name: err.name,
|
|
59
|
-
message: `GCF [${fname}]: ${err.message}`,
|
|
60
|
-
stack: err.stack,
|
|
61
|
-
},
|
|
62
|
-
};
|
|
63
|
-
await this.publish(event, context, response, { error: '1' }).catch(noop);
|
|
55
|
+
await this.publish(event, context, GcfCommon.buildResponse(err), { error: '1' }).catch(noop);
|
|
64
56
|
throw err;
|
|
65
57
|
});
|
|
66
58
|
}
|
|
67
59
|
|
|
60
|
+
static buildResponse(error: Error) {
|
|
61
|
+
return {
|
|
62
|
+
error: {
|
|
63
|
+
name: error.name,
|
|
64
|
+
message: `GCF [${process?.env?.K_SERVICE ?? 'UNKNOWN'}]: ${error.message}`,
|
|
65
|
+
stack: error.stack,
|
|
66
|
+
},
|
|
67
|
+
} as TResponse;
|
|
68
|
+
}
|
|
69
|
+
|
|
68
70
|
static async publish(event: TEvent, context: TContext, json?: TResponse, attributes?: Dict<any>) {
|
|
69
71
|
const { topic, exchange, queue, appId, env, requestId } = await this.safeGetAttributes(event, context, [
|
|
70
72
|
'topic',
|