Skip to content

Commit

Permalink
fix(cognito): ambiguous error message when same trigger is added twice (
Browse files Browse the repository at this point in the history
#16917)

when the same trigger is added twice to the Cognito userpool, 
```ts
const fn = lambda.Function.fromFunctionArn(stack, 'Trigger', 'arn:aws:lambda:us-east-1:123456789012:function:CognitoFunction')

const userpool = new cognito.UserPool(stack, 'Userpool', { lambdaTriggers: { customMessage: fn } })

userpool.addTrigger(cognito.UserPoolOperation.CUSTOM_MESSAGE, fn)
```
throws error message:

`Error: A trigger for the operation [object Object] already exists.`

This PR fixes it as:

` Error: A trigger for the operation customMessage already exists.`

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
nom3ad committed Nov 5, 2021
1 parent 1aa1588 commit 4ae78b0
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-cognito/lib/user-pool.ts
Expand Up @@ -830,7 +830,7 @@ export class UserPool extends UserPoolBase {
*/
public addTrigger(operation: UserPoolOperation, fn: lambda.IFunction): void {
if (operation.operationName in this.triggers) {
throw new Error(`A trigger for the operation ${operation} already exists.`);
throw new Error(`A trigger for the operation ${operation.operationName} already exists.`);
}

this.addLambdaPermission(fn, operation.operationName);
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-cognito/test/user-pool.test.ts
Expand Up @@ -403,7 +403,7 @@ describe('User Pool', () => {

// WHEN
userpool.addTrigger(UserPoolOperation.CREATE_AUTH_CHALLENGE, fn1);
expect(() => userpool.addTrigger(UserPoolOperation.CREATE_AUTH_CHALLENGE, fn2)).toThrow(/already exists/);
expect(() => userpool.addTrigger(UserPoolOperation.CREATE_AUTH_CHALLENGE, fn2)).toThrow(/createAuthChallenge already exists/);
});

test('no username aliases specified', () => {
Expand Down

0 comments on commit 4ae78b0

Please sign in to comment.