summaryrefslogtreecommitdiff
path: root/swift/src/Ice/Exception.swift
blob: 474c9fa7e343460b8a4bca0842a99c0ef6909ec9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//

/// 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
}

public extension Exception {
    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: Int

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

    public init(file: String = #file, line: Int = #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(preserve: false)
    }

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

    /// Returns the sliced data if the exception has a preserved-slice base class and has been sliced during
    /// un-marshaling, nil is returned otherwise.
    ///
    /// - returns: `SlicedData?` - The sliced data or nil.
    open func ice_getSlicedData() -> SlicedData? {
        return nil
    }
}

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

    public override var description: String {
        return message
    }

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

    public init(_ message: String) {
        self.message = message
    }
}