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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cpp/src/slice2swift/Gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,7 @@ Gen::TypesVisitor::visitEnum(const EnumPtr& p)
out << nl << "let rawValue: " << enumType << " = try read(enumMaxValue: " << p->maxValue() << ")";
out << nl << "guard let val = " << name << "(rawValue: rawValue) else";
out << sb;
out << nl << "throw " << getUnqualified("Ice.MarshalException", swiftModule) << "(reason: \"invalid enum value\")";
out << nl << "throw " << getUnqualified("Ice.MarshalException", swiftModule) << "(\"invalid enum value\")";
out << eb;
out << nl << "return val";
out << eb;
Expand Down
2 changes: 1 addition & 1 deletion swift/src/Ice/CommunicatorI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ class CommunicatorI: LocalObject<ICECommunicator>, Communicator {

func makeProxyImpl<ProxyImpl>(_ proxyString: String) throws -> ProxyImpl where ProxyImpl: ObjectPrxI {
guard let proxy: ProxyImpl = try stringToProxyImpl(proxyString) else {
throw ParseException(str: "invalid empty proxy string")
throw ParseException("invalid empty proxy string")
}
return proxy
}
Expand Down
4 changes: 2 additions & 2 deletions swift/src/Ice/Connection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extension InputStream {
public func read() throws -> CompressBatch {
let rawValue: UInt8 = try read(enumMaxValue: 2)
guard let val = CompressBatch(rawValue: rawValue) else {
throw MarshalException(reason: "invalid enum value")
throw MarshalException("invalid enum value")
}
return val
}
Expand Down Expand Up @@ -87,7 +87,7 @@ extension InputStream {
public func read() throws -> ConnectionClose {
let rawValue: UInt8 = try read(enumMaxValue: 2)
guard let val = ConnectionClose(rawValue: rawValue) else {
throw MarshalException(reason: "invalid enum value")
throw MarshalException("invalid enum value")
}
return val
}
Expand Down
40 changes: 19 additions & 21 deletions swift/src/Ice/CurrentExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,30 +79,33 @@ extension Current {
fatalError("Unexpected RequestFailedException subclass")
}

if rfe.id.name.isEmpty {
rfe.id = id
}
var id = rfe.id
var facet = rfe.facet
var operation = rfe.operation

if rfe.facet.isEmpty {
rfe.facet = facet
if id.name.isEmpty {
id = self.id
facet = self.facet
}

if rfe.operation.isEmpty {
rfe.operation = operation
if operation.isEmpty {
operation = self.operation
}

// We must call ice_print _after_ setting the properties above.
exceptionMessage = rfe.ice_print()
// [7..] to slice-off the "::Ice::" prefix
let typeName = String(exceptionId!.dropFirst(7))
exceptionMessage = RequestFailedException.makeMessage(
typeName: typeName, id: id, facet: facet, operation: operation)

if requestId != 0 {
ostr.write(replyStatus.rawValue)
ostr.write(rfe.id)
if rfe.facet.isEmpty {
ostr.write(id)
if facet.isEmpty {
ostr.write(size: 0)
} else {
ostr.write([rfe.facet])
ostr.write([facet])
}
ostr.write(rfe.operation)
ostr.write(operation)
}

case let ex as UserException:
Expand All @@ -120,28 +123,23 @@ extension Current {
case let ex as UnknownLocalException:
exceptionId = ex.ice_id()
replyStatus = .unknownLocalException
exceptionMessage = ex.unknown
exceptionMessage = ex.message

case let ex as UnknownUserException:
exceptionId = ex.ice_id()
replyStatus = .unknownUserException
exceptionMessage = ex.unknown
exceptionMessage = ex.message

case let ex as UnknownException:
exceptionId = ex.ice_id()
replyStatus = .unknownException
exceptionMessage = ex.unknown
exceptionMessage = ex.message

case let ex as LocalException:
exceptionId = ex.ice_id()
replyStatus = .unknownLocalException
exceptionMessage = "\(ex)"

case let ex as Exception:
exceptionId = ex.ice_id()
replyStatus = .unknownException
exceptionMessage = "\(ex)"

default:
replyStatus = .unknownException
exceptionId = "\(type(of: error))"
Expand Down
2 changes: 1 addition & 1 deletion swift/src/Ice/EndpointSelectionType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ extension InputStream {
public func read() throws -> EndpointSelectionType {
let rawValue: UInt8 = try read(enumMaxValue: 1)
guard let val = EndpointSelectionType(rawValue: rawValue) else {
throw MarshalException(reason: "invalid enum value")
throw MarshalException("invalid enum value")
}
return val
}
Expand Down
88 changes: 1 addition & 87 deletions swift/src/Ice/Exception.swift
Original file line number Diff line number Diff line change
@@ -1,90 +1,4 @@
// Copyright (c) ZeroC, Inc.

/// Base protocol for Ice exceptions.
public protocol Exception: Error {
/// Returns the type id of this exception.
///
/// - returns: `String` - The type id of this exception.
func ice_id() -> String
static func ice_staticId() -> String
}

extension Exception {
public func ice_id() -> String {
return type(of: self).ice_staticId()
}
}

/// Base class for Ice run-time exceptions.
open class LocalException: Exception, CustomStringConvertible {
public let file: String
public let line: Int32

public var description: String {
return "\(file): \(line): \(ice_id())\(ice_print())"
}

public init(file: String = #file, line: Int32 = #line) {
self.file = file
self.line = line
}

open class func ice_staticId() -> String {
return "::Ice::LocalException"
}

/// Returns a stringified description of this exception.
///
/// - returns: `String` - The exception description.
open func ice_print() -> String {
return ""
}
}

/// Base class for Ice user exceptions.
open class UserException: Exception {
public required init() {}

open func _iceReadImpl(from _: InputStream) throws {}
open func _iceWriteImpl(to _: OutputStream) {}

open func _usesClasses() -> Bool {
return false
}

/// Returns the Slice type ID of the exception.
///
/// - returns: `String` The Slice type ID.
open class func ice_staticId() -> String {
return "::Ice::UserException"
}

open func _iceRead(from istr: InputStream) throws {
istr.startException()
try _iceReadImpl(from: istr)
try istr.endException()
}

open func _iceWrite(to ostr: OutputStream) {
ostr.startException()
_iceWriteImpl(to: ostr)
ostr.endException()
}
}

/// Error used to wrap C++ std::exception errors.
public class RuntimeError: LocalException {
private let message: String

override public var description: String {
return message
}

override open class func ice_staticId() -> String {
return "::Ice::RuntimeError"
}

public init(_ message: String) {
self.message = message
}
}
public protocol Exception: Error {}
2 changes: 1 addition & 1 deletion swift/src/Ice/Initialize.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import IceImpl
//
let factoriesRegistered: Bool = {
ICEUtil.registerFactories(
exception: ExceptionFactory.self,
exception: LocalExceptionFactory.self,
connectionInfo: ConnectionInfoFactory.self,
endpointInfo: EndpointInfoFactory.self,
adminFacet: AdminFacetFactory.self)
Expand Down
Loading
Loading