@@ -72,6 +72,14 @@ describe("loggerMiddleware", () => {
72
72
expect ( response ) . toStrictEqual ( mockResponse ) ;
73
73
} ) ;
74
74
75
+ it ( "rejects without logging if context.logger doesn't have error function" , async ( ) => {
76
+ const nextError = new Error ( "next error" ) ;
77
+ mockNext . mockRejectedValueOnce ( nextError ) ;
78
+ const logger = { } as Logger ;
79
+ const response = await expect ( loggerMiddleware ( ) ( mockNext , { logger } ) ( mockArgs ) ) . rejects . toThrow ( nextError ) ;
80
+ expect ( mockNext ) . toHaveBeenCalledTimes ( 1 ) ;
81
+ } ) ;
82
+
75
83
describe ( "logs if context.logger has info function" , ( ) => {
76
84
it ( "success case with clientName, commandName, input, metadata" , async ( ) => {
77
85
mockNext . mockResolvedValueOnce ( mockResponse ) ;
@@ -149,4 +157,76 @@ describe("loggerMiddleware", () => {
149
157
} ) ;
150
158
} ) ;
151
159
} ) ;
160
+
161
+ describe ( "logs if context.logger has error function" , ( ) => {
162
+ it ( "next throws synchronously" , async ( ) => {
163
+ const { $metadata } = mockOutput ;
164
+ const nextError = new Error ( "example error" ) ;
165
+ Object . assign ( nextError , { $metadata } ) ;
166
+ mockNext . mockImplementationOnce ( ( ) => {
167
+ throw nextError ;
168
+ } ) ;
169
+
170
+ const clientName = "mockClientName" ;
171
+ const commandName = "mockCommandName" ;
172
+
173
+ const logger = { error : jest . fn ( ) } as unknown as Logger ;
174
+ const inputFilterSensitiveLog = jest . fn ( ) . mockImplementationOnce ( ( input ) => input ) ;
175
+ const outputFilterSensitiveLog = jest . fn ( ) . mockImplementationOnce ( ( output ) => output ) ;
176
+
177
+ const context = {
178
+ clientName,
179
+ commandName,
180
+ logger,
181
+ inputFilterSensitiveLog,
182
+ outputFilterSensitiveLog,
183
+ } ;
184
+
185
+ await expect ( loggerMiddleware ( ) ( mockNext , context ) ( mockArgs ) ) . rejects . toThrow ( nextError ) ;
186
+ expect ( mockNext ) . toHaveBeenCalledTimes ( 1 ) ;
187
+
188
+ expect ( logger . error ) . toHaveBeenCalledTimes ( 1 ) ;
189
+ expect ( logger . error ) . toHaveBeenCalledWith ( {
190
+ clientName,
191
+ commandName,
192
+ input : mockArgs . input ,
193
+ error : nextError ,
194
+ metadata : $metadata ,
195
+ } ) ;
196
+ } ) ;
197
+
198
+ it ( "next rejects" , async ( ) => {
199
+ const { $metadata } = mockOutput ;
200
+ const nextError = new Error ( "example error" ) ;
201
+ Object . assign ( nextError , { $metadata } ) ;
202
+ mockNext . mockRejectedValueOnce ( nextError ) ;
203
+
204
+ const clientName = "mockClientName" ;
205
+ const commandName = "mockCommandName" ;
206
+
207
+ const logger = { error : jest . fn ( ) } as unknown as Logger ;
208
+ const inputFilterSensitiveLog = jest . fn ( ) . mockImplementationOnce ( ( input ) => input ) ;
209
+ const outputFilterSensitiveLog = jest . fn ( ) . mockImplementationOnce ( ( output ) => output ) ;
210
+
211
+ const context = {
212
+ clientName,
213
+ commandName,
214
+ logger,
215
+ inputFilterSensitiveLog,
216
+ outputFilterSensitiveLog,
217
+ } ;
218
+
219
+ await expect ( loggerMiddleware ( ) ( mockNext , context ) ( mockArgs ) ) . rejects . toThrow ( nextError ) ;
220
+ expect ( mockNext ) . toHaveBeenCalledTimes ( 1 ) ;
221
+
222
+ expect ( logger . error ) . toHaveBeenCalledTimes ( 1 ) ;
223
+ expect ( logger . error ) . toHaveBeenCalledWith ( {
224
+ clientName,
225
+ commandName,
226
+ input : mockArgs . input ,
227
+ error : nextError ,
228
+ metadata : $metadata ,
229
+ } ) ;
230
+ } ) ;
231
+ } ) ;
152
232
} ) ;
0 commit comments