You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by ni...@apache.org on 2021/11/07 02:57:32 UTC
[lucenenet] 05/09: SWEEP: Lucene.Net.Replicator: Reviewed and
updated exception catch and throw statements to match Lucene
This is an automated email from the ASF dual-hosted git repository.
nightowl888 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/lucenenet.git
commit e58266ae622de9ca5bec79518c7f9e00f514cd89
Author: Shad Storhaug <sh...@shadstorhaug.com>
AuthorDate: Sat Nov 6 02:11:44 2021 +0700
SWEEP: Lucene.Net.Replicator: Reviewed and updated exception catch and throw statements to match Lucene
---
src/Lucene.Net.Replicator/Http/HttpClientBase.cs | 2 +-
.../Http/ReplicationService.cs | 21 ++++---
.../IndexAndTaxonomyReplicationHandler.cs | 20 +++---
.../IndexReplicationHandler.cs | 12 ++--
src/Lucene.Net.Replicator/IndexRevision.cs | 2 +-
.../PerSessionDirectoryFactory.cs | 4 +-
src/Lucene.Net.Replicator/ReplicationClient.cs | 4 +-
.../IndexAndTaxonomyReplicationClientTest.cs | 10 ++-
.../IndexReplicationClientTest.cs | 16 +++--
.../Exceptions/ServletException.cs | 73 ++++++++++++++++++++++
10 files changed, 130 insertions(+), 34 deletions(-)
diff --git a/src/Lucene.Net.Replicator/Http/HttpClientBase.cs b/src/Lucene.Net.Replicator/Http/HttpClientBase.cs
index 733a700..9621fe9 100644
--- a/src/Lucene.Net.Replicator/Http/HttpClientBase.cs
+++ b/src/Lucene.Net.Replicator/Http/HttpClientBase.cs
@@ -458,7 +458,7 @@ namespace Lucene.Net.Replicator.Http
input.Dispose();
}
}
- catch (Exception ioe) when (ioe.IsIOException())
+ catch (Exception e) when (e.IsException())
{
// ignored on purpose
}
diff --git a/src/Lucene.Net.Replicator/Http/ReplicationService.cs b/src/Lucene.Net.Replicator/Http/ReplicationService.cs
index 8187362..015a5e9 100644
--- a/src/Lucene.Net.Replicator/Http/ReplicationService.cs
+++ b/src/Lucene.Net.Replicator/Http/ReplicationService.cs
@@ -1,10 +1,12 @@
using J2N.IO;
using J2N.Text;
+using Lucene.Net.Diagnostics;
using Lucene.Net.Replicator.Http.Abstractions;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
+using System.Net;
namespace Lucene.Net.Replicator.Http
{
@@ -120,7 +122,7 @@ namespace Lucene.Net.Replicator.Http
string param = request.QueryParam(paramName);
if (param == null)
{
- throw IllegalStateException.Create("Missing mandatory parameter: " + paramName);
+ throw ServletException.Create("Missing mandatory parameter: " + paramName);
}
return param;
}
@@ -136,17 +138,17 @@ namespace Lucene.Net.Replicator.Http
string[] pathElements = GetPathElements(request);
if (pathElements.Length != 2)
{
- throw IllegalStateException.Create("invalid path, must contain shard ID and action, e.g. */s1/update");
+ throw ServletException.Create("invalid path, must contain shard ID and action, e.g. */s1/update");
}
if (!Enum.TryParse(pathElements[ACTION_IDX], true, out ReplicationAction action))
{
- throw IllegalStateException.Create("Unsupported action provided: " + pathElements[ACTION_IDX]);
+ throw ServletException.Create("Unsupported action provided: " + pathElements[ACTION_IDX]);
}
if (!replicators.TryGetValue(pathElements[SHARD_IDX], out IReplicator replicator))
{
- throw IllegalStateException.Create("unrecognized shard ID " + pathElements[SHARD_IDX]);
+ throw ServletException.Create("unrecognized shard ID " + pathElements[SHARD_IDX]);
}
// SOLR-8933 Don't close this stream.
@@ -179,22 +181,25 @@ namespace Lucene.Net.Replicator.Http
token.Serialize(new DataOutputStream(response.Body));
}
break;
+
+ // LUCENENET specific:
default:
- throw new ArgumentOutOfRangeException();
+ if (Debugging.AssertsEnabled) Debugging.Assert(false, "Invalid ReplicationAction specified");
+ break;
}
}
catch (Exception e)
{
- response.StatusCode = 500;
+ response.StatusCode = (int)HttpStatusCode.InternalServerError; // propagate the failure
try
{
TextWriter writer = new StreamWriter(response.Body);
JsonSerializer serializer = JsonSerializer.Create(JSON_SERIALIZER_SETTINGS);
serializer.Serialize(writer, e, e.GetType());
}
- catch (Exception exception)
+ catch (Exception e2) when (e2.IsException())
{
- throw new IOException("Could not serialize", exception);
+ throw new IOException("Could not serialize", e2);
}
}
finally
diff --git a/src/Lucene.Net.Replicator/IndexAndTaxonomyReplicationHandler.cs b/src/Lucene.Net.Replicator/IndexAndTaxonomyReplicationHandler.cs
index 13a63e8..d5b2e64 100644
--- a/src/Lucene.Net.Replicator/IndexAndTaxonomyReplicationHandler.cs
+++ b/src/Lucene.Net.Replicator/IndexAndTaxonomyReplicationHandler.cs
@@ -156,16 +156,20 @@ namespace Lucene.Net.Replicator
// against those errors, app will probably hit them elsewhere.
IndexReplicationHandler.CleanupOldIndexFiles(indexDirectory, indexSegmentsFile);
IndexReplicationHandler.CleanupOldIndexFiles(taxonomyDirectory, taxonomySegmentsFile);
-
+
// successfully updated the index, notify the callback that the index is
// ready.
- if (callback != null) {
- try {
- callback.Invoke();
- } catch (Exception e) {
- throw new IOException(e.Message, e);
- }
- }
+ if (callback != null)
+ {
+ try
+ {
+ callback.Invoke();
+ }
+ catch (Exception e) when (e.IsException())
+ {
+ throw new IOException(e.ToString(), e);
+ }
+ }
}
// LUCENENET specific utility method
diff --git a/src/Lucene.Net.Replicator/IndexReplicationHandler.cs b/src/Lucene.Net.Replicator/IndexReplicationHandler.cs
index ba2f405..18c19ca 100644
--- a/src/Lucene.Net.Replicator/IndexReplicationHandler.cs
+++ b/src/Lucene.Net.Replicator/IndexReplicationHandler.cs
@@ -1,4 +1,4 @@
-using Lucene.Net.Index;
+using Lucene.Net.Index;
using Lucene.Net.Store;
using Lucene.Net.Util;
using System;
@@ -138,7 +138,7 @@ namespace Lucene.Net.Replicator
{
directory.DeleteFile(file);
}
- catch
+ catch (Exception t) when (t.IsThrowable())
{
// suppress any exception because if we're here, it means copy
// failed, and we must cleanup after ourselves.
@@ -182,7 +182,7 @@ namespace Lucene.Net.Replicator
{
directory.DeleteFile(file);
}
- catch
+ catch (Exception t) when (t.IsThrowable())
{
// suppress, it's just a best effort
}
@@ -191,7 +191,7 @@ namespace Lucene.Net.Replicator
}
}
- catch
+ catch (Exception t) when (t.IsThrowable())
{
// ignore any errors that happens during this state and only log it. this
// cleanup will have a chance to succeed the next time we get a new
@@ -230,7 +230,7 @@ namespace Lucene.Net.Replicator
{
directory.DeleteFile(IndexFileNames.SEGMENTS_GEN);
}
- catch
+ catch (Exception t) when (t.IsThrowable())
{
// suppress any errors while deleting this file.
}
@@ -326,7 +326,7 @@ namespace Lucene.Net.Replicator
{
callback.Invoke();
}
- catch (Exception e)
+ catch (Exception e) when (e.IsException())
{
throw new IOException(e.ToString(), e);
}
diff --git a/src/Lucene.Net.Replicator/IndexRevision.cs b/src/Lucene.Net.Replicator/IndexRevision.cs
index b3f33d0..69ba7b4 100644
--- a/src/Lucene.Net.Replicator/IndexRevision.cs
+++ b/src/Lucene.Net.Replicator/IndexRevision.cs
@@ -102,7 +102,7 @@ namespace Lucene.Net.Replicator
public IndexRevision(IndexWriter writer)
{
sdp = writer.Config.IndexDeletionPolicy as SnapshotDeletionPolicy;
- if (sdp == null)
+ if (sdp is null)
throw new ArgumentException("IndexWriter must be created with SnapshotDeletionPolicy", nameof(writer));
this.writer = writer;
diff --git a/src/Lucene.Net.Replicator/PerSessionDirectoryFactory.cs b/src/Lucene.Net.Replicator/PerSessionDirectoryFactory.cs
index 149ce87..431bb34 100644
--- a/src/Lucene.Net.Replicator/PerSessionDirectoryFactory.cs
+++ b/src/Lucene.Net.Replicator/PerSessionDirectoryFactory.cs
@@ -1,4 +1,4 @@
-using Lucene.Net.Store;
+using Lucene.Net.Store;
using System;
using System.IO;
using Directory = Lucene.Net.Store.Directory;
@@ -44,6 +44,8 @@ namespace Lucene.Net.Replicator
{
string sourceDirectory = Path.Combine(workingDirectory, sessionId, source);
System.IO.Directory.CreateDirectory(sourceDirectory);
+ if (!System.IO.Directory.Exists(sourceDirectory))
+ throw new IOException("failed to create source directory " + sourceDirectory);
return FSDirectory.Open(sourceDirectory);
}
diff --git a/src/Lucene.Net.Replicator/ReplicationClient.cs b/src/Lucene.Net.Replicator/ReplicationClient.cs
index be8842b..7604fa1 100644
--- a/src/Lucene.Net.Replicator/ReplicationClient.cs
+++ b/src/Lucene.Net.Replicator/ReplicationClient.cs
@@ -167,9 +167,9 @@ namespace Lucene.Net.Replicator
{
doUpdate();
}
- catch (Exception exception)
+ catch (Exception t) when (t.IsThrowable())
{
- handleException(exception);
+ handleException(t);
}
finally
{
diff --git a/src/Lucene.Net.Tests.Replicator/IndexAndTaxonomyReplicationClientTest.cs b/src/Lucene.Net.Tests.Replicator/IndexAndTaxonomyReplicationClientTest.cs
index 7e5569e..1c4c441 100644
--- a/src/Lucene.Net.Tests.Replicator/IndexAndTaxonomyReplicationClientTest.cs
+++ b/src/Lucene.Net.Tests.Replicator/IndexAndTaxonomyReplicationClientTest.cs
@@ -164,7 +164,7 @@ namespace Lucene.Net.Replicator
reader.Dispose();
}
}
- catch (Exception)
+ catch (Exception e) when (e.IsException())
{
// we can hit IndexNotFoundException or e.g. EOFException (on
// segments_N) because it is being copied at the same time it is read by
@@ -330,7 +330,7 @@ namespace Lucene.Net.Replicator
handler = new IndexAndTaxonomyReplicationHandler(handlerIndexDir, handlerTaxoDir, () =>
{
if (Random.NextDouble() < 0.2 && failures > 0)
- throw new Exception("random exception from callback");
+ throw RuntimeException.Create("random exception from callback");
return null;
});
client = new ReplicationClientAnonymousClass(this, replicator, handler, @in, failures);
@@ -440,7 +440,7 @@ namespace Lucene.Net.Replicator
protected override void HandleUpdateException(Exception exception)
{
- if (exception is IOException)
+ if (exception.IsIOException())
{
try
{
@@ -468,6 +468,10 @@ namespace Lucene.Net.Replicator
// category to all documents, there's nothing much more to validate
TestUtil.CheckIndex(test.handlerTaxoDir.Delegate);
}
+ catch (Exception e) when (e.IsIOException())
+ {
+ throw RuntimeException.Create(e);
+ }
finally
{
// count-down number of failures
diff --git a/src/Lucene.Net.Tests.Replicator/IndexReplicationClientTest.cs b/src/Lucene.Net.Tests.Replicator/IndexReplicationClientTest.cs
index 89e0412..c1f7dee 100644
--- a/src/Lucene.Net.Tests.Replicator/IndexReplicationClientTest.cs
+++ b/src/Lucene.Net.Tests.Replicator/IndexReplicationClientTest.cs
@@ -130,7 +130,7 @@ namespace Lucene.Net.Replicator
reader.Dispose();
}
}
- catch (Exception)
+ catch (Exception e) when (e.IsException())
{
// we can hit IndexNotFoundException or e.g. EOFException (on
// segments_N) because it is being copied at the same time it is read by
@@ -261,7 +261,7 @@ namespace Lucene.Net.Replicator
handler = new IndexReplicationHandler(handlerDir, () =>
{
if (Random.NextDouble() < 0.2 && failures > 0)
- throw new Exception("random exception from callback");
+ throw RuntimeException.Create("random exception from callback");
return null;
});
client = new ReplicationClientAnonymousClass(this, replicator, handler, sourceDirFactory, failures);
@@ -351,7 +351,7 @@ namespace Lucene.Net.Replicator
protected override void HandleUpdateException(Exception exception)
{
- if (exception is IOException)
+ if (exception.IsIOException())
{
if (Verbose)
{
@@ -375,6 +375,11 @@ namespace Lucene.Net.Replicator
// verify index consistency
TestUtil.CheckIndex(test.handlerDir.Delegate);
}
+ catch (Exception e) when (e.IsIOException())
+ {
+ // exceptions here are bad, don't ignore them
+ throw RuntimeException.Create(e);
+ }
finally
{
// count-down number of failures
@@ -395,7 +400,10 @@ namespace Lucene.Net.Replicator
}
else
{
- ExceptionDispatchInfo.Capture(exception).Throw(); // LUCENENET: Rethrow to preserve stack details from the original throw
+ if (exception.IsRuntimeException())
+ ExceptionDispatchInfo.Capture(exception).Throw(); // LUCENENET: Rethrow to preserve stack details from the original throw
+ else
+ throw RuntimeException.Create(exception);
}
}
}
diff --git a/src/Lucene.Net/Support/ExceptionHandling/Exceptions/ServletException.cs b/src/Lucene.Net/Support/ExceptionHandling/Exceptions/ServletException.cs
new file mode 100644
index 0000000..6659d32
--- /dev/null
+++ b/src/Lucene.Net/Support/ExceptionHandling/Exceptions/ServletException.cs
@@ -0,0 +1,73 @@
+using System;
+using System.Net.Http;
+using System.Runtime.CompilerServices;
+using System.Runtime.Serialization;
+
+namespace Lucene
+{
+ /// <summary>
+ /// The Java description is:
+ /// Defines a general exception a servlet can throw when it encounters difficulty.
+ /// <para/>
+ /// This is a Java compatibility exception, and should be thrown in
+ /// Lucene.NET everywhere Lucene throws it.
+ /// </summary>
+
+ // LUCENENET: It is no longer good practice to use binary serialization.
+ // See: https://github.com/dotnet/corefx/issues/23584#issuecomment-325724568
+#if FEATURE_SERIALIZABLE_EXCEPTIONS
+ [Serializable]
+#endif
+ internal class ServletException : Exception
+ {
+ [Obsolete("Use ServletException.Create() instead.", error: true)]
+ public ServletException()
+ {
+ }
+
+ [Obsolete("Use ServletException.Create() instead.", error: true)]
+ public ServletException(string message) : base(message)
+ {
+ }
+
+ [Obsolete("Use ServletException.Create() instead.", error: true)]
+ public ServletException(string message, Exception innerException) : base(message, innerException)
+ {
+ }
+
+ [Obsolete("Use ServletException.Create() instead.", error: true)]
+ public ServletException(Exception cause)
+ : base(cause?.ToString(), cause)
+ {
+ }
+
+#if FEATURE_SERIALIZABLE_EXCEPTIONS
+ /// <summary>
+ /// Initializes a new instance of this class with serialized data.
+ /// </summary>
+ /// <param name="info">The <see cref="SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
+ /// <param name="context">The <see cref="StreamingContext"/> that contains contextual information about the source or destination.</param>
+ protected ServletException(SerializationInfo info, StreamingContext context)
+ : base(info, context)
+ {
+ }
+#endif
+
+ // Static factory methods
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static Exception Create() => new HttpRequestException();
+
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static Exception Create(string message) => new HttpRequestException(message);
+
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static Exception Create(string message, Exception innerException) => new HttpRequestException(message, innerException);
+
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static Exception Create(Exception cause) => new HttpRequestException(cause.Message, cause);
+ }
+}