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.

The OpenXmlPartContainer.DeletePart will throw exception when the id not found

See original GitHub issue

Before submitting an issue, please fill this out

Is this a:

  • Issue with the OpenXml library
  • Question on library usage

Description

As we can see the OpenXmlPartContainer.DeletePart can delete the part, but this API will throw ArgumentOutOfRangeException when the part with the specified id does not exist.

Because the DeletePart call the DeletePartCore which will call the GetPartById to get the part.

https://github.com/OfficeDev/Open-XML-SDK/blob/0f1764a866e36d615e22d247e780fb7675046e9a/src/DocumentFormat.OpenXml/Packaging/OpenXmlPartContainer.cs#L1762

But the GetPartById will throw ArgumentOutOfRangeException when the part with the specified id does not exist.

https://github.com/OfficeDev/Open-XML-SDK/blob/0f1764a866e36d615e22d247e780fb7675046e9a/src/DocumentFormat.OpenXml/Packaging/OpenXmlPartContainer.cs#L529-L547

Yes, it means that the branch will never enter when the id not found

https://github.com/OfficeDev/Open-XML-SDK/blob/0f1764a866e36d615e22d247e780fb7675046e9a/src/DocumentFormat.OpenXml/Packaging/OpenXmlPartContainer.cs#L1764-L1768

Information

  • .NET Target: All
  • DocumentFormat.OpenXml Version: 2.10.1

Repro

public static void Delete(this string id, OpenXmlPart part)
{
     part.DeletePart(id);
}

Observed

The part.DeletePart will throw the exception when the part with the specified id does not exist.

Expected

As the other API design, the delete method should not throw exception the element does not exist.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
stoiveypcommented, Jun 18, 2020

@twsouthwick I’d be interested in picking this up, seems like a straight forward change to start with

1reaction
lindexicommented, May 13, 2020

@twsouthwick After https://github.com/OfficeDev/Open-XML-SDK/pull/714 merged, we can use this API to check whether the OpenXmlPartContainer contain the part with the specified id. So, there are ways to avoid this problem.

if (container.TryGetPartById(id, out _)
{
        part.DeletePart(id);
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

The OpenXmlPartContainer.DeletePart will throw ...
DeletePart can delete the part, but this API will throw ArgumentOutOfRangeException when the part with the specified id does not exist.
Read more >
OpenXmlPartContainer.DeletePart Method
This method also returns false if the part was not found. Exceptions. ArgumentNullException. Thrown when "id" is null reference. Applies to. DocumentFormat.
Read more >
How to I tell a Springdata-repository's delete method to not ...
We should not throw not found exception when the id not found, since there maybe a cheat to check whether this ID exists...
Read more >
16 Handling Exceptions Using SOAP Faults
This chapter describes how to handle exceptions that occur when a message is being processed using Simple Object Access Protocol (SOAP) faults for...
Read more >
Should a function throw exceptions based on its expected ...
To try to delete a post with an invalid postId does not make sense, so an exception should be thrown. My colleague argues...
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