@@ -32,7 +32,8 @@ module.exports = (() => {
3232 * Configures and returns a log4js logger.
3333 *
3434 * @public
35- * @param {Object|String= } configuration - Configuration path (as string) or a configuration data (as object).
35+ * @static
36+ * @param {Object|String= } configuration - Configuration path (as string) or configuration data (as an object).
3637 * @returns {Object }
3738 */
3839 static getLogger ( configuration ) {
@@ -62,6 +63,7 @@ module.exports = (() => {
6263 * Returns secret value from AWS Secrets Manager.
6364 *
6465 * @public
66+ * @static
6567 * @async
6668 * @param {String } secretId
6769 * @return {Promise<String> }
@@ -74,6 +76,7 @@ module.exports = (() => {
7476 * Builds and returns a new {@link LambdaEventParser}.
7577 *
7678 * @public
79+ * @static
7780 * @param {Object } event
7881 * @returns {LambdaEventParser }
7982 */
@@ -85,6 +88,7 @@ module.exports = (() => {
8588 * Builds and returns a new {@link LambdaValidator}.
8689 *
8790 * @public
91+ * @static
8892 * @returns {LambdaValidator }
8993 */
9094 static getValidator ( ) {
@@ -95,6 +99,7 @@ module.exports = (() => {
9599 * Builds and returns a new {@link LambdaResponder}.
96100 *
97101 * @public
102+ * @static
98103 * @param {Function } callback
99104 * @returns {LambdaResponder }
100105 */
@@ -106,6 +111,7 @@ module.exports = (() => {
106111 * Builds and returns a new {@link LambdaStage}.
107112 *
108113 * @public
114+ * @static
109115 * @param {String } stage
110116 * @returns {LambdaStage }
111117 */
@@ -120,83 +126,90 @@ module.exports = (() => {
120126 * the processor, and responding with the processor's result.
121127 *
122128 * @public
129+ * @static
123130 * @async
124131 * @param {String } description - Human-readable description of the Lambda Function.
125132 * @param {Object } event - The actual "event" object passed to the Lambda Function by the AWS framework.
126133 * @param {Function } callback - The actual "callback" function passed to the Lambda Function by the AWS framework.
127134 * @param {Callbacks.LambdaProcessorCallback } processor - The processor that is invoked to perform the work.
128- * @returns {Promise }
135+ * @returns {Promise<*> }
129136 */
130137 static async process ( description , event , callback , processor ) {
131138 const context = { } ;
132139
133- return Promise . resolve ( context )
134- . then ( ( context ) => {
135- assert . argumentIsRequired ( description , 'description' , String ) ;
136- assert . argumentIsRequired ( processor , 'processor' , Function ) ;
140+ try {
141+ assert . argumentIsRequired ( description , 'description' , String ) ;
142+ assert . argumentIsRequired ( processor , 'processor' , Function ) ;
137143
138- context . parser = LambdaHelper . getEventParser ( event ) ;
139- context . responder = LambdaHelper . getResponder ( callback ) ;
144+ context . parser = LambdaHelper . getEventParser ( event ) ;
145+ context . responder = LambdaHelper . getResponder ( callback ) ;
140146
141- if ( context . parser . plainText ) {
142- context . responder . setPlainText ( ) ;
143- }
147+ if ( context . parser . plainText ) {
148+ context . responder . setPlainText ( ) ;
149+ }
144150
145- if ( eventLogger && eventLogger . isTraceEnabled ( ) ) {
146- eventLogger . trace ( JSON . stringify ( event , null , 2 ) ) ;
147- }
151+ if ( eventLogger && eventLogger . isTraceEnabled ( ) ) {
152+ eventLogger . trace ( JSON . stringify ( event , null , 2 ) ) ;
153+ }
148154
149- return context ;
150- } ) . then ( ( context ) => {
151- const validator = LambdaHelper . getValidator ( ) ;
152-
153- return validator . validate ( event )
154- . then ( ( valid ) => {
155- if ( valid ) {
156- return Promise . resolve ( context ) ;
157- } else {
158- return Promise . reject ( FailureReason . from ( LambdaFailureType . LAMBDA_INVOCATION_SUPPRESSED ) ) ;
159- }
160- } ) ;
161- } ) . then ( ( context ) => {
162- return Promise . resolve ( )
163- . then ( ( ) => {
164- return processor ( context . parser , context . responder ) ;
165- } ) . then ( ( response ) => {
166- context . responder . send ( response ) ;
167- } ) ;
168- } ) . catch ( ( e ) => {
169- let reason ;
170-
171- if ( e instanceof FailureReason ) {
172- reason = e ;
173-
174- if ( lambdaLogger ) {
175- if ( reason . getIsSevere ( ) ) {
176- lambdaLogger . error ( reason . format ( ) ) ;
177- } else {
178- lambdaLogger . warn ( reason . format ( ) ) ;
179- }
180- }
181- } else {
182- reason = new FailureReason ( { endpoint : { description } } ) ;
183- reason = reason . addItem ( FailureType . REQUEST_GENERAL_FAILURE ) ;
155+ const validator = LambdaHelper . getValidator ( ) ;
156+ const valid = await validator . validate ( event ) ;
157+
158+ if ( ! valid ) {
159+ throw FailureReason . from ( LambdaFailureType . LAMBDA_INVOCATION_SUPPRESSED ) ;
160+ }
161+
162+ const response = await processor ( context . parser , context . responder ) ;
163+
164+ return await context . responder . send ( response ) ;
165+ } catch ( e ) {
166+ let reason ;
184167
185- if ( lambdaLogger ) {
186- lambdaLogger . error ( e ) ;
168+ if ( e instanceof FailureReason ) {
169+ reason = e ;
170+
171+ if ( lambdaLogger ) {
172+ if ( reason . getIsSevere ( ) ) {
173+ lambdaLogger . error ( reason . format ( ) ) ;
174+ } else {
175+ lambdaLogger . warn ( reason . format ( ) ) ;
187176 }
188177 }
178+ } else {
179+ reason = new FailureReason ( { endpoint : { description } } ) ;
180+ reason = reason . addItem ( FailureType . REQUEST_GENERAL_FAILURE ) ;
189181
190- if ( eventLogger && ! eventLogger . isTraceEnabled ( ) ) {
191- eventLogger . warn ( JSON . stringify ( event , null , 2 ) ) ;
182+ if ( lambdaLogger ) {
183+ lambdaLogger . error ( e ) ;
192184 }
185+ }
186+
187+ if ( eventLogger && ! eventLogger . isTraceEnabled ( ) ) {
188+ eventLogger . warn ( JSON . stringify ( event , null , 2 ) ) ;
189+ }
190+
191+ return await context . responder . sendError ( reason , reason . getErrorCode ( ) ) ;
192+ }
193+ }
193194
194- context . responder . sendError ( reason , reason . getErrorCode ( ) ) ;
195- } ) ;
195+ /**
196+ * Starts a promise chain for the Lambda function, invoking the suppressor, then
197+ * the processor, and responding with the processor's result.
198+ *
199+ * @public
200+ * @static
201+ * @async
202+ * @param {String } description - Human-readable description of the Lambda Function.
203+ * @param {Object } event - The actual "event" object passed to the Lambda Function by the AWS framework.
204+ * @param {Callbacks.LambdaProcessorCallback } processor - The processor that is invoked to perform the work.
205+ * @returns {Promise<*> }
206+ */
207+ static async processAsync ( description , event , processor ) {
208+ return LambdaHelper . process ( description , event , ( ) => { } , processor ) ;
196209 }
197210
198211 toString ( ) {
199- return 'LambdaHelper' ;
212+ return '[ LambdaHelper ] ' ;
200213 }
201214 }
202215
0 commit comments