From 3e6f10ddfdf93147f3d488de2965f7e263bd1713 Mon Sep 17 00:00:00 2001 From: Luciano Mammino Date: Mon, 18 Dec 2023 19:43:46 +0100 Subject: [PATCH] fix(core): messages from `annotations.ts` can show up as `[object Object]` (#28414) **CDK Version**: 2.115.0 (build 58027ee) **Os**: macOS 14.2 (BuildVersion: 23C64) I have observed the following warning showing up in my console today when running `cdk`: > [Warning at /CdkStack/AuthorizerFunction] [object Object] I was able to track down where this message was generated and apply a patch to see the error in a more descriptive format. For the records the error in my case was: > addPermission() has no effect on a Lambda Function with region=${Token[TOKEN.23]}, account=${Token[TOKEN.24]}, in a Stack with region=${Token[AWS.Region.12]}, account=${Token[AWS.AccountId.8]}. Suppress this warning if this is is intentional, or pass sameEnvironment=true to fromFunctionAttributes() if you would like to add the permissions. [ack: UnclearLambdaEnvironment] The fix proposed here makes sure that if I am not sure this is the best way to fix this issue. The signature of the `addMessage` seems to expect a `string` for the `message` value, so maybe the error needs to be corrected downstream where the `addMessage` call is made (which judging from the stack trace seems to come from `aws-cdk-lib/aws-lambda/lib/function-base.js`). Thoughts? ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- packages/aws-cdk-lib/core/lib/annotations.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/aws-cdk-lib/core/lib/annotations.ts b/packages/aws-cdk-lib/core/lib/annotations.ts index 19e83f0ec43af..2d5d75dab3ef0 100644 --- a/packages/aws-cdk-lib/core/lib/annotations.ts +++ b/packages/aws-cdk-lib/core/lib/annotations.ts @@ -143,7 +143,8 @@ export class Annotations { private addMessage(level: string, message: string) { const isNew = !this.scope.node.metadata.find((x) => x.data === message); if (isNew) { - this.scope.node.addMetadata(level, message, { stackTrace: this.stackTraces }); + let normalizedMessage = typeof message === 'string' ? message : JSON.stringify(message); + this.scope.node.addMetadata(level, normalizedMessage, { stackTrace: this.stackTraces }); } } } @@ -259,4 +260,4 @@ function removeWarning(construct: IConstruct, id: string) { function ackTag(id: string) { return `[ack: ${id}]`; -} \ No newline at end of file +}