-
Notifications
You must be signed in to change notification settings - Fork 592
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consolidate/refactor local exceptions in C# #2338
Changes from 17 commits
686fd4a
aa1cba4
e1df52c
31fc9b3
860465f
df9c5f6
875fe1b
822d2db
a32b121
edd5586
d48a9da
8c12111
004157d
f79da3c
3b5fc1b
fd4e492
e161841
2b66086
316bb73
d89278e
29b026d
28cdd3c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -142,7 +142,7 @@ internal void destroy(int reason) | |
{ | ||
case ObjectAdapterDeactivated: | ||
{ | ||
setState(StateClosing, new ObjectAdapterDeactivatedException()); | ||
setState(StateClosing, new ObjectAdapterDeactivatedException(_adapter?.getName() ?? "")); | ||
break; | ||
} | ||
|
||
|
@@ -161,11 +161,18 @@ public void close(ConnectionClose mode) | |
{ | ||
if (mode == ConnectionClose.Forcefully) | ||
{ | ||
setState(StateClosed, new ConnectionManuallyClosedException(false)); | ||
setState(StateClosed, | ||
new ConnectionAbortedException( | ||
"Connection close forcefully by the application.", | ||
closedByApplication: true)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not currently - this exception is only thrown when the application aborts the connection "manually". We need t What about replacing ConnectionIdleException by ConnectionAbortedException with message + closedByApplication = false? ConnectionLostException (a SocketException) also means "connection aborted". Should we replace it by ConnectionAbortedException? All for a follow-up PR. |
||
} | ||
else if (mode == ConnectionClose.Gracefully) | ||
{ | ||
setState(StateClosing, new ConnectionManuallyClosedException(true)); | ||
setState( | ||
StateClosing, | ||
new ConnectionClosedException( | ||
"Connection close gracefully by the application.", | ||
closedByApplication: true)); | ||
} | ||
else | ||
{ | ||
|
@@ -179,7 +186,11 @@ public void close(ConnectionClose mode) | |
Monitor.Wait(this); | ||
} | ||
|
||
setState(StateClosing, new ConnectionManuallyClosedException(true)); | ||
setState( | ||
StateClosing, | ||
new ConnectionClosedException( | ||
"Connection close gracefully by the application.", | ||
closedByApplication: true)); | ||
} | ||
} | ||
} | ||
|
@@ -884,7 +895,7 @@ public override void message(ref ThreadPoolCurrent current) | |
// | ||
// This situation is possible for small UDP packets. | ||
// | ||
throw new IllegalMessageSizeException(); | ||
throw new MarshalException("Received Ice message with too few bytes in header."); | ||
} | ||
|
||
// Decode the header. | ||
|
@@ -897,22 +908,29 @@ public override void message(ref ThreadPoolCurrent current) | |
if (m[0] != Protocol.magic[0] || m[1] != Protocol.magic[1] || | ||
m[2] != Protocol.magic[2] || m[3] != Protocol.magic[3]) | ||
{ | ||
BadMagicException ex = new BadMagicException(); | ||
ex.badMagic = m; | ||
throw ex; | ||
throw new ProtocolException( | ||
$"Bad magic in message header: {m[0]:X2} {m[1]:X2} {m[2]:X2} {m[3]:X2}"); | ||
} | ||
|
||
ProtocolVersion pv = new ProtocolVersion(_readStream); | ||
Protocol.checkSupportedProtocol(pv); | ||
EncodingVersion ev = new EncodingVersion(_readStream); | ||
Protocol.checkSupportedProtocolEncoding(ev); | ||
var pv = new ProtocolVersion(_readStream); | ||
if (pv != Util.currentProtocol) | ||
{ | ||
throw new MarshalException( | ||
$"Invalid protocol version in message header: {pv.major}.{pv.minor}"); | ||
} | ||
var ev = new EncodingVersion(_readStream); | ||
if (ev != Util.currentProtocolEncoding) | ||
{ | ||
throw new MarshalException( | ||
$"Invalid protocol encoding version in message header: {ev.major}.{ev.minor}"); | ||
} | ||
|
||
_readStream.readByte(); // messageType | ||
_readStream.readByte(); // compress | ||
int size = _readStream.readInt(); | ||
if (size < Protocol.headerSize) | ||
{ | ||
throw new IllegalMessageSizeException(); | ||
throw new MarshalException($"Received Ice message with unexpected size {size}."); | ||
} | ||
|
||
// Resize the read buffer to the message size. | ||
|
@@ -1249,7 +1267,7 @@ private void finish() | |
// Trace the cause of unexpected connection closures | ||
// | ||
if (!(_exception is CloseConnectionException || | ||
_exception is ConnectionManuallyClosedException || | ||
_exception is ConnectionAbortedException || | ||
_exception is ConnectionClosedException || | ||
_exception is ConnectionIdleException || | ||
_exception is CommunicatorDestroyedException || | ||
|
@@ -1507,16 +1525,16 @@ internal void idleCheck(TimeSpan idleTimeout) | |
{ | ||
if (isActiveOrHolding()) | ||
{ | ||
int idleTimeoutInSeconds = (int)idleTimeout.TotalSeconds; | ||
|
||
if (_instance.traceLevels().network >= 1) | ||
{ | ||
int idleTimeoutInSeconds = (int)idleTimeout.TotalSeconds; | ||
|
||
_instance.initializationData().logger.trace( | ||
_instance.traceLevels().networkCat, | ||
$"connection aborted by the idle check because it did not receive any bytes for {idleTimeoutInSeconds}s\n{_transceiver.toDetailedString()}"); | ||
} | ||
|
||
setState(StateClosed, new ConnectionIdleException()); | ||
setState(StateClosed, new ConnectionIdleException($"Connection aborted by the idle check because it did not receive any bytes for {idleTimeoutInSeconds}s.")); | ||
} | ||
// else nothing to do | ||
} | ||
|
@@ -1590,7 +1608,7 @@ private void setState(int state, LocalException ex) | |
// Don't warn about certain expected exceptions. | ||
// | ||
if (!(_exception is CloseConnectionException || | ||
_exception is ConnectionManuallyClosedException || | ||
_exception is ConnectionAbortedException || | ||
_exception is ConnectionClosedException || | ||
_exception is ConnectionIdleException || | ||
_exception is CommunicatorDestroyedException || | ||
|
@@ -1753,7 +1771,7 @@ private void setState(int state) | |
if (_observer is not null && state == StateClosed && _exception is not null) | ||
{ | ||
if (!(_exception is CloseConnectionException || | ||
_exception is ConnectionManuallyClosedException || | ||
_exception is ConnectionAbortedException || | ||
_exception is ConnectionClosedException || | ||
_exception is ConnectionIdleException || | ||
_exception is CommunicatorDestroyedException || | ||
|
@@ -1921,27 +1939,34 @@ private bool validate(int operation) | |
if (m[0] != Protocol.magic[0] || m[1] != Protocol.magic[1] || | ||
m[2] != Protocol.magic[2] || m[3] != Protocol.magic[3]) | ||
{ | ||
BadMagicException ex = new BadMagicException(); | ||
ex.badMagic = m; | ||
throw ex; | ||
throw new ProtocolException( | ||
$"Bad magic in message header: {m[0]:X2} {m[1]:X2} {m[2]:X2} {m[3]:X2}"); | ||
} | ||
|
||
ProtocolVersion pv = new ProtocolVersion(_readStream); | ||
Protocol.checkSupportedProtocol(pv); | ||
|
||
EncodingVersion ev = new EncodingVersion(_readStream); | ||
Protocol.checkSupportedProtocolEncoding(ev); | ||
var pv = new ProtocolVersion(_readStream); | ||
if (pv != Util.currentProtocol) | ||
{ | ||
throw new MarshalException( | ||
$"Invalid protocol version in message header: {pv.major}.{pv.minor}"); | ||
} | ||
var ev = new EncodingVersion(_readStream); | ||
if (ev != Util.currentProtocolEncoding) | ||
{ | ||
throw new MarshalException( | ||
$"Invalid protocol encoding version in message header: {ev.major}.{ev.minor}"); | ||
} | ||
|
||
byte messageType = _readStream.readByte(); | ||
if (messageType != Protocol.validateConnectionMsg) | ||
{ | ||
throw new ConnectionNotValidatedException(); | ||
throw new ProtocolException( | ||
$"Received message of type {messageType} on connection that is not yet validated."); | ||
} | ||
_readStream.readByte(); // Ignore compression status for validate connection. | ||
int size = _readStream.readInt(); | ||
if (size != Protocol.headerSize) | ||
{ | ||
throw new IllegalMessageSizeException(); | ||
throw new MarshalException($"Received ValidateConnection message with unexpected size {size}."); | ||
} | ||
TraceUtil.traceRecv(_readStream, _logger, _traceLevels); | ||
} | ||
|
@@ -2283,9 +2308,7 @@ private int parseMessage(ref MessageInfo info) | |
else | ||
{ | ||
string lib = AssemblyUtil.isWindows ? "bzip2.dll" : "libbz2.so.1"; | ||
FeatureNotSupportedException ex = new FeatureNotSupportedException(); | ||
ex.unsupportedFeature = "Cannot uncompress compressed message: " + lib + " not found"; | ||
throw ex; | ||
throw new FeatureNotSupportedException($"Cannot uncompress compressed message: {lib} not found"); | ||
} | ||
} | ||
info.stream.pos(Protocol.headerSize); | ||
|
@@ -2359,12 +2382,12 @@ private int parseMessage(ref MessageInfo info) | |
else | ||
{ | ||
TraceUtil.traceRecv(info.stream, _logger, _traceLevels); | ||
info.requestCount = info.stream.readInt(); | ||
if (info.requestCount < 0) | ||
int requestCount = info.stream.readInt(); | ||
if (requestCount < 0) | ||
{ | ||
info.requestCount = 0; | ||
throw new UnmarshalOutOfBoundsException(); | ||
throw new MarshalException($"Received batch request with {requestCount} batches."); | ||
} | ||
info.requestCount = requestCount; | ||
info.adapter = _adapter; | ||
info.upcallCount += info.requestCount; | ||
|
||
|
@@ -2422,7 +2445,8 @@ private int parseMessage(ref MessageInfo info) | |
{ | ||
TraceUtil.trace("received unknown message\n(invalid, closing connection)", | ||
info.stream, _logger, _traceLevels); | ||
throw new UnknownMessageException(); | ||
|
||
throw new ProtocolException($"Received Ice protocol message with unknown type: {messageType}"); | ||
} | ||
} | ||
} | ||
|
@@ -2613,9 +2637,11 @@ private void inactivityCheck(System.Threading.Timer inactivityTimer) | |
|
||
if (_state == StateActive) | ||
{ | ||
// TODO: fix LocalException to accept a message | ||
// "connection closed because it remained inactive for longer than the inactivity timeout" | ||
setState(StateClosing, new ConnectionClosedException()); | ||
setState( | ||
StateClosing, | ||
new ConnectionClosedException( | ||
"Connection closed because it remained inactive for longer than the inactivity timeout.", | ||
closedByApplication: false)); | ||
} | ||
} | ||
// Else this timer was already canceled and disposed. Nothing to do. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also useful as a guide when updating other language mappings.