From 52526b2028602a556d170a4f0fd5cc40ef02ac30 Mon Sep 17 00:00:00 2001 From: Nemanja Stojanovic Date: Tue, 4 Aug 2020 10:07:12 +0200 Subject: [PATCH] Protect against undefined env vars While running sls offline on version 6.5, i came across a somewhat cryptic error: ``` Debug: internal, implementation, error TypeError: Cannot read property 'Fn::Join' of undefined at my/path/node_modules/serverless-offline/dist/utils/resolveJoins.js:18:28 at Array.forEach () at resolveJoins (my/path/node_modules/serverless-offline/dist/utils/resolveJoins.js:15:28) at new LambdaFunction (my/path/node_modules/serverless-offline/dist/lambda/LambdaFunction.js:154:56) at LambdaFunctionPool.get (my/path/node_modules/serverless-offline/dist/lambda/LambdaFunctionPool.js:93:24) at Lambda.get (my/path/node_modules/serverless-offline/dist/lambda/Lambda.js:60:88) at hapiHandler (my/path/node_modules/serverless-offline/dist/events/http/HttpServer.js:511:82) at module.exports.internals.Manager.execute (my/path/node_modules/@hapi/hapi/lib/toolkit.js:41:33) at Object.internals.handler (my/path/node_modules/@hapi/hapi/lib/handler.js:46:48) at exports.execute (my/path/node_modules/@hapi/hapi/lib/handler.js:31:36) at Request._lifecycle (my/path/node_modules/@hapi/hapi/lib/request.js:312:68) at runMicrotasks () at processTicksAndRejections (internal/process/task_queues.js:97:5) at Request._execute (my/path/node_modules/@hapi/hapi/lib/request.js:221:9) ``` Since this project doesn't use the join intrinsic function, the error wasn't particularly helpful in determining the problem. After a bit of search, I found the cause in [this PR](https://github.com/dherault/serverless-offline/pull/1032). The problem is that, if an environment variable is `undefined`, an exception is raised. A better approach might be to either ignore undefined env vars or log them with a warning. I chose the former right now but am open to change the code as needed. --- src/utils/resolveJoins.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/utils/resolveJoins.js b/src/utils/resolveJoins.js index 8b81b39f1..2380ab43a 100644 --- a/src/utils/resolveJoins.js +++ b/src/utils/resolveJoins.js @@ -8,6 +8,9 @@ export default function resolveJoins(environment) { Object.keys(environment).forEach((key) => { const value = environment[key] + if (!value) { + return; + } const joinArray = value['Fn::Join'] const isJoin = Boolean(joinArray)