[AI Task] [Tizen.Network.Connection] ThrowConnectionException: switch expression with default arm#7612
[AI Task] [Tizen.Network.Connection] ThrowConnectionException: switch expression with default arm#7612JoonghyunCho wants to merge 2 commits into
Conversation
… expression with default arm
The legacy 14-case `switch` statement in `ConnectionErrorFactory.ThrowConnectionException` had no `default` branch, so any unrecognized native error code caused the method to silently return — letting callers continue past a failed native call as if the call had succeeded.
This change converts the body into a `throw <expr> switch { ... }` expression and adds an explicit default arm that throws `InvalidOperationException` with the unknown errno embedded in the message. The compiler now requires the discard arm, preventing the same silent-failure bug from regressing.
All 15 known cases preserve their existing exception type and message (matching the original switch verbatim). All in-tree callers gate on `(ConnectionError)ret != ConnectionError.None` before invoking this method, so the default arm is reachable only for genuinely unknown error codes — which is the intended fix.
Fixes #7591
There was a problem hiding this comment.
Code Review
This pull request refactors the ThrowConnectionException method in ConnectionError.cs to use a switch expression and introduces a default case for unknown error codes. The review feedback highlights an opportunity to improve consistency by using string interpolation and TrimEnd() for all error cases that include the optional message, preventing trailing spaces when the message is empty.
| ConnectionError.AddressFamilyNotSupported => new InvalidOperationException("Address Family Not Supported"), | ||
| ConnectionError.AlreadyExists => new InvalidOperationException("Already Exists"), | ||
| ConnectionError.DhcpFailed => new InvalidOperationException("DHCP Failed"), | ||
| ConnectionError.EndOfIteration => new InvalidOperationException("End Of Iteration"), | ||
| ConnectionError.InvalidKey => new InvalidOperationException("Invalid Key"), | ||
| ConnectionError.InvalidOperation => new InvalidOperationException("Invalid Operation " + message), | ||
| ConnectionError.InvalidParameter => new ArgumentException("Invalid Parameter"), | ||
| ConnectionError.NoConnection => new InvalidOperationException("No Connection"), | ||
| ConnectionError.NoReply => new InvalidOperationException("No Reply"), | ||
| ConnectionError.NotSupported => new NotSupportedException("Unsupported feature " + message), | ||
| ConnectionError.NowInProgress => new InvalidOperationException("Now In Progress"), | ||
| ConnectionError.OperationAborted => new InvalidOperationException("Operation Aborted"), | ||
| ConnectionError.OperationFailed => new InvalidOperationException("Operation Failed"), | ||
| ConnectionError.OutOfMemoryError => new OutOfMemoryException("Out Of Memory Error"), | ||
| ConnectionError.PermissionDenied => new UnauthorizedAccessException("Permission Denied " + message), | ||
| _ => new InvalidOperationException( | ||
| $"Unknown ConnectionError({errno}) {message}".TrimEnd()) |
There was a problem hiding this comment.
The new default case (_) correctly uses TrimEnd() to handle the optional message parameter without leaving a trailing space. However, other cases like InvalidOperation, NotSupported, and PermissionDenied still use string concatenation ("... " + message), which results in a trailing space when message is empty. Additionally, InvalidParameter currently ignores the message entirely. For consistency and better error reporting, consider using the same interpolation pattern for all cases that can include additional context.
ConnectionError.AddressFamilyNotSupported => new InvalidOperationException("Address Family Not Supported"),
ConnectionError.AlreadyExists => new InvalidOperationException("Already Exists"),
ConnectionError.DhcpFailed => new InvalidOperationException("DHCP Failed"),
ConnectionError.EndOfIteration => new InvalidOperationException("End Of Iteration"),
ConnectionError.InvalidKey => new InvalidOperationException("Invalid Key"),
ConnectionError.InvalidOperation => new InvalidOperationException($"Invalid Operation {message}".TrimEnd()),
ConnectionError.InvalidParameter => new ArgumentException($"Invalid Parameter {message}".TrimEnd()),
ConnectionError.NoConnection => new InvalidOperationException("No Connection"),
ConnectionError.NoReply => new InvalidOperationException("No Reply"),
ConnectionError.NotSupported => new NotSupportedException($"Unsupported feature {message}".TrimEnd()),
ConnectionError.NowInProgress => new InvalidOperationException("Now In Progress"),
ConnectionError.OperationAborted => new InvalidOperationException("Operation Aborted"),
ConnectionError.OperationFailed => new InvalidOperationException("Operation Failed"),
ConnectionError.OutOfMemoryError => new OutOfMemoryException("Out Of Memory Error"),
ConnectionError.PermissionDenied => new UnauthorizedAccessException($"Permission Denied {message}".TrimEnd()),
_ => new InvalidOperationException($"Unknown ConnectionError({errno}) {message}".TrimEnd())There was a problem hiding this comment.
🤖 [AI Review]
Addressed in f87ee3b. Applied the suggested interpolation with .TrimEnd() to InvalidOperation, InvalidParameter, NotSupported, and PermissionDenied for consistent error formatting.
|
🤖 [AI Review] Reviewed — no findings. Scope checked:
No 🔴 critical issues, no 🟡 suggestions to flag. Automated review — final merge decision rests with human reviewers. |
Apply consistent message interpolation with TrimEnd() to InvalidOperation, InvalidParameter, NotSupported, and PermissionDenied cases so the optional message argument is included without producing a trailing space when empty. Applied-Human-Comments: 3178012843
Summary
ConnectionErrorFactory.ThrowConnectionExceptionhad a 14-caseswitchstatement with nodefaultbranch. Any unrecognized native error code (e.g. a future native lib addition) caused the method to silently return, letting callers continue past a failed native call as if the call had succeeded — typically resulting in a downstream NRE or a wrong "successful" result.This PR converts the body to a
throw <expr> switch { ... }expression with an explicit default arm. The compiler now enforces exhaustiveness, preventing the silent-failure bug from regressing.Changes
src/Tizen.Network.Connection/Tizen.Network.Connection/ConnectionError.cs—ThrowConnectionExceptionrewritten as a switch expression with a_ => new InvalidOperationException($"Unknown ConnectionError({errno}) {message}".TrimEnd())default arm. All 15 known cases preserve their original exception type and message verbatim.Mode
Refactoring (with a deliberate, scoped behavior change for the unknown-errno case)
Behavior change scope
InvalidOperationExceptionwith the errno embedded in the message. This is the intended fix — the issue documents that downstream code in this assembly already breaks on the silent-return path (NRE / wrong result), so making the failure visible is strictly an improvement.ConnectionError.None(success): never reaches this method in any in-tree caller. All ~30 call sites are guarded byif ((ConnectionError)ret != ConnectionError.None)before invoking. Verified by grep: no caller passes0/ConnectionError.None. The twoCheck*helpers also guard on the specific error before calling.API Compatibility
internal static)Verification
dotnet build src/Tizen.Network.Connection/Tizen.Network.Connection.csproj— 0 errors; pre-existing unrelated warnings only)Fixes #7591