From bc42277feb73e819c91fe3fa29e6dc45364a7933 Mon Sep 17 00:00:00 2001 From: dali546 <35352237+dali546@users.noreply.github.com> Date: Sat, 4 May 2024 22:02:50 +0100 Subject: [PATCH] chore: refactor error handling --- .../aws-cdk-lib/aws-lambda/lib/function.ts | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/packages/aws-cdk-lib/aws-lambda/lib/function.ts b/packages/aws-cdk-lib/aws-lambda/lib/function.ts index 7757a1dedc01f..32e477f81f86b 100644 --- a/packages/aws-cdk-lib/aws-lambda/lib/function.ts +++ b/packages/aws-cdk-lib/aws-lambda/lib/function.ts @@ -1166,17 +1166,6 @@ export class Function extends FunctionBase { * function and undefined if not. */ private getLoggingConfig(props: FunctionProps): CfnFunction.LoggingConfigProperty | undefined { - if ((props.applicationLogLevel || props.applicationLogLevelV2 || props.systemLogLevel || props.systemLogLevelV2) - && props.logFormat !== LogFormat.JSON - && props.loggingFormat === undefined) { - throw new Error(`To use ApplicationLogLevel and/or SystemLogLevel you must set LogFormat to '${LogFormat.JSON}', got '${props.logFormat}'.`); - } - - if ((props.applicationLogLevel || props.applicationLogLevelV2 || props.systemLogLevel || props.systemLogLevelV2) - && props.loggingFormat !== LoggingFormat.JSON && props.logFormat === undefined) { - throw new Error(`To use ApplicationLogLevel and/or SystemLogLevel you must set LoggingFormat to '${LoggingFormat.JSON}', got '${props.loggingFormat}'.`); - } - if (props.logFormat && props.loggingFormat) { throw new Error('Only define LogFormat or LoggingFormat, not both.'); } @@ -1189,6 +1178,16 @@ export class Function extends FunctionBase { throw new Error('Only define systemLogLevel or systemLogLevelV2, not both.'); } + if (props.applicationLogLevel || props.applicationLogLevelV2 || props.systemLogLevel || props.systemLogLevelV2) { + if (props.logFormat !== LoggingFormat.JSON && props.loggingFormat === undefined) { + throw new Error(`To use ApplicationLogLevel and/or SystemLogLevel you must set LogFormat to '${LogFormat.JSON}', got '${props.logFormat}'.`); + } + + if (props.loggingFormat !== LoggingFormat.JSON && props.logFormat === undefined) { + throw new Error(`To use ApplicationLogLevel and/or SystemLogLevel you must set LoggingFormat to '${LoggingFormat.JSON}', got '${props.loggingFormat}'.`); + } + } + let loggingConfig: CfnFunction.LoggingConfigProperty; if (props.logFormat || props.logGroup || props.loggingFormat) { loggingConfig = {