Skip to content
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

Refactor local exceptions in Swift #2378

Merged
merged 17 commits into from
Jul 3, 2024

Conversation

bernardnormier
Copy link
Member

The PR refactors the Ice local exceptions in Swift.

In particular:

  • the local exceptions have now read-only properties
  • LocalException has a now a message property
  • the C++-to-Swift local exception conversion is much simpler

@@ -14,6 +14,12 @@ toNSString(const std::string& s)
return [[NSString alloc] initWithUTF8String:s.c_str()];
}

inline NSString*
toNSString(const char* s)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to do const char* -> std::string -> NSString like we did previously.

#import "IceUtil.h"
#import "ImplicitContext.h"
#import "LocalExceptionFactory.h"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed Exception.h -> LocalExceptionFactory.h

category: String,
facet: String,
operation: String, file: String, line: Int32
let className = typeId.dropFirst(2).replacingOccurrences(of: "::", with: ".")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same logic as for localException below.


static func protocolException(_ reason: String, file: String, line: Int32) -> Error {
return ProtocolException(reason: reason, file: file, line: line)
let className = typeId.dropFirst(2).replacingOccurrences(of: "::", with: ".")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generate fully qualified class name from Slice type ID.

return if let localExceptionType = NSClassFromString(className) as? LocalException.Type {
localExceptionType.init(message: message, cxxDescription: cxxDescription, file: file, line: line)
} else {
CxxLocalException(typeId: typeId, message: message, cxxDescription: cxxDescription, file: file, line: line)
Copy link
Member Author

@bernardnormier bernardnormier Jun 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used for C++ local exceptions not mapped to Swift exceptions such as Ice::FileException.

/// - cxxDescription: The C++ exception description.
/// - file: The file where the exception was thrown.
/// - line: The line where the exception was thrown.
public required init(message: String, cxxDescription: String, file: String, line: Int32) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"required" is required for creating instances by name (see NSClassFromString code).

self.cxxDescription = cxxDescription
}

public func ice_id() -> String {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need a "virtual" (overridable) ice_id, so can't use an extension method.

@@ -8,10 +8,7 @@ class PropertiesI: LocalObject<ICEProperties>, Properties {
}

public func getIceProperty(_ key: String) throws -> String {
guard let value = handle.getIceProperty(key) else {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's more correct to throw the exception in the Objective-C++ code.

@@ -0,0 +1,35 @@
// Copyright (c) ZeroC, Inc.

/// Base class for Ice user exceptions.
Copy link
Member Author

@bernardnormier bernardnormier Jun 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR does not update user exceptions.

override open func ice_print() -> String {
return _FixedProxyExceptionDescription
/// Base class for Ice local exceptions.
open class LocalException: Exception, CustomStringConvertible {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be public class instead?

I kept open class since that what we have in 3.7.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public class seems more correct now. I suspect we needed open in 3.7 for local slice.

@bernardnormier bernardnormier requested review from externl and pepone July 1, 2024 13:19
swift/src/Ice/CurrentExtensions.swift Outdated Show resolved Hide resolved
swift/src/Ice/LocalExceptions.swift Outdated Show resolved Hide resolved
swift/src/Ice/LocalExceptions.swift Outdated Show resolved Hide resolved
swift/src/Ice/LocalExceptions.swift Outdated Show resolved Hide resolved
/// - badTypeId: The type ID of the user exception carried by the reply.
/// - file: The file where the exception was thrown.
/// - line: The line where the exception was thrown.
public convenience init(badTypeId: String, file: String = #fileID, line: Int32 = #line) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it correct to use #fileID as a default, won't this give us the LocalExceptions.swift file ID?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this works fine in Swift.

override open func ice_print() -> String {
return _FixedProxyExceptionDescription
/// Base class for Ice local exceptions.
open class LocalException: Exception, CustomStringConvertible {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public class seems more correct now. I suspect we needed open in 3.7 for local slice.

@bernardnormier bernardnormier merged commit 7640183 into zeroc-ice:main Jul 3, 2024
17 checks passed
@bernardnormier bernardnormier deleted the exception3-swift branch December 9, 2024 17:09
InsertCreativityHere pushed a commit to InsertCreativityHere/compiler-comparison that referenced this pull request Jan 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants