Skip to content

Commit

Permalink
Treat Encoder/DecoderFallbackExceptions as IOExceptions to match Java,
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirwin committed Jan 11, 2025
1 parent 955872b commit b61a322
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Reflection;
using System.Resources;
using System.Security;
using System.Text;
using Assert = Lucene.Net.TestFramework.Assert;

namespace Lucene.Net.Support.ExceptionHandling
Expand Down Expand Up @@ -184,6 +185,8 @@ private static IEnumerable<Type> LoadKnownErrorExceptionTypes()
typeof(UnauthorizedAccessException),
typeof(ObjectDisposedException),
typeof(Lucene.AlreadyClosedException),
typeof(EncoderFallbackException), // In Java, CharacterCodingException subclasses IOException
typeof(DecoderFallbackException),
}.Union(AllIOExceptionTypes)
// .NET Framework only - Subclasses UnauthorizedAccessException
.Union(new[] { PrivilegeNotHeldExceptionType });
Expand Down Expand Up @@ -221,8 +224,6 @@ private static IEnumerable<Type> LoadKnownErrorExceptionTypes()
// Subclasses
typeof(System.DuplicateWaitObjectException),
typeof(System.Globalization.CultureNotFoundException),
typeof(System.Text.DecoderFallbackException),
typeof(System.Text.EncoderFallbackException),
};

public static readonly IEnumerable<Type> KnownIllegalArgumentExceptionTypes_TestEnvironment = new Type[] {
Expand All @@ -234,8 +235,6 @@ private static IEnumerable<Type> LoadKnownErrorExceptionTypes()
// Subclasses
typeof(System.DuplicateWaitObjectException),
typeof(System.Globalization.CultureNotFoundException),
typeof(System.Text.DecoderFallbackException),
typeof(System.Text.EncoderFallbackException),
};

public static readonly IEnumerable<Type> KnownRuntimeExceptionTypes = LoadKnownRuntimeExceptionTypes();
Expand Down
16 changes: 11 additions & 5 deletions src/Lucene.Net/Support/ExceptionHandling/ExceptionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Resources;
using System.Runtime.CompilerServices;
using System.Security;
using System.Text;
using System.Threading;

namespace Lucene
Expand Down Expand Up @@ -213,8 +214,11 @@ public static bool IsIOException(this Exception e)
if (e is null || e.IsAlwaysIgnored()) return false;

return e is IOException ||
e.IsAlreadyClosedException() || // In Lucene, AlreadyClosedException subclass IOException instead of InvalidOperationException, so we need a special case here
e is UnauthorizedAccessException; // In Java, java.nio.file.AccessDeniedException subclasses IOException
e.IsAlreadyClosedException() || // In Lucene, AlreadyClosedException subclass IOException instead of InvalidOperationException, so we need a special case here
e is
UnauthorizedAccessException // In Java, java.nio.file.AccessDeniedException subclasses IOException
or DecoderFallbackException // In Java, CharacterCodingException subclasses IOException
or EncoderFallbackException;
}

/// <summary>
Expand Down Expand Up @@ -368,9 +372,11 @@ public static bool IsIllegalArgumentException(this Exception e)
// LUCENENET: In production, there is a chance that we will upgrade to ArgumentNullExcpetion or ArgumentOutOfRangeException
// and it is still important that those are caught. However, we have a copy of this method in the test environment
// where this is done more strictly to catch ArgumentException without its known subclasses so we can be more explicit in tests.
return e is ArgumentException;
//!(e is ArgumentNullException) && // Corresponds to NullPointerException, so we don't catch it here.
//!(e is ArgumentOutOfRangeException); // Corresponds to IndexOutOfBoundsException (and subclasses), so we don't catch it here.
return e is ArgumentException
and not DecoderFallbackException // In Java, CharacterCodingException subclasses IOException, not ArgumentException
and not EncoderFallbackException;
//!(e is ArgumentNullException) && // Corresponds to NullPointerException, so we don't catch it here.
//!(e is ArgumentOutOfRangeException); // Corresponds to IndexOutOfBoundsException (and subclasses), so we don't catch it here.
}

/// <summary>
Expand Down

0 comments on commit b61a322

Please sign in to comment.