question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to force an exception

See original GitHub issue

I am using this library in my tests and so far it is great. However I am a unsure about how to use this to force an exception.

for example I am doing this

const {
  DynamoDBDocumentClient,
  UpdateCommand,
} = require('@aws-sdk/lib-dynamodb');
const {
  ConditionalCheckFailedException,
} = require('@aws-sdk/client-dynamodb');

const ddbMock = mockClient(DynamoDBDocumentClient);
ddbMock.on(UpdateCommand).rejects(ConditionalCheckFailedException);

However that does not throw the proper exception. Any ideas on what I need to do differently?

The code I am testing looks like this

if (error instanceof ConditionalCheckFailedException) {
      ...
}

For reference: https://aws.amazon.com/blogs/developer/service-error-handling-modular-aws-sdk-js/ https://docs.aws.amazon.com/aws-sdk-php/v3/api/class-Aws.DynamoDb.Exception.DynamoDbException.html#shape-conditionalcheckfailedexception

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
m-radzikowskicommented, Dec 5, 2022

The ConditionalCheckFailedException constructor requires the message and $metadata properties, so the correct way to create it without any would be:

    ddbMock
        .on(UpdateCommand)
        .rejects(new ConditionalCheckFailedException({message: '', $metadata: {}}));
1reaction
automartin5000commented, Nov 9, 2022

Actually, I was able to work around this with ddbMock.on(UpdateCommand).rejects(new ConditionalCheckFailedException('' as any));

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Throw Exceptions (The Java™ Tutorials > Essential ...
Most programs throw and catch objects that derive from the Exception class. An Exception indicates that a problem occurred, but it is not...
Read more >
How to Throw an Exception in Java | Webucator
Open your text editor and type in the following Java statements: Java Source for Throwing an Exception · Save your file as ThrowAnException.java...
Read more >
How can I raise an error in if-else function in java
Here you go: throw new java.lang.Error("this is very bad");. More idiomatic to throw a subclass of Exception. RuntimeException in particular is unchecked ...
Read more >
Creating and Throwing Exceptions | Microsoft Learn
Learn about creating and throwing exceptions. Exceptions are used to indicate that an error has occurred while running a program.
Read more >
How can an exception be thrown manually by a programmer ...
To throw an exception explicitly you need to instantiate the class of it and throw its object using the throw keyword.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found