Skip to content

Commit

Permalink
Add localizedError support to swift errors.
Browse files Browse the repository at this point in the history
Also some extra error string semantics tests for kotlin.

Fixes #2110
  • Loading branch information
mhammond committed May 18, 2024
1 parent 874e656 commit e6b4858
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
6 changes: 6 additions & 0 deletions fixtures/error-types/tests/bindings/test.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ try {
assert(e.chain().size == 2)
assert(e.link(0U) == "because uniffi told me so")
}
try {
oops()
throw RuntimeException("Should have failed")
} catch (e: kotlin.Exception) {
assert(e.toString() == "because uniffi told me so\n\nCaused by:\n oops")
}

try {
oopsNowrap()
Expand Down
34 changes: 32 additions & 2 deletions fixtures/error-types/tests/bindings/test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,38 @@ do {
try oops()
fatalError("Should have thrown")
} catch let e as ErrorInterface {
assert(String(describing: e) == "because uniffi told me so\n\nCaused by:\n oops")
let msg = "because uniffi told me so\n\nCaused by:\n oops"
assert(String(describing: e) == msg)
assert(String(reflecting: e) == "ErrorInterface { e: \(msg) }")
}

do {
try oops()
fatalError("Should have thrown")
} catch {
let msg = "because uniffi told me so\n\nCaused by:\n oops"
assert(String(describing: error) == msg)
assert(String(reflecting: error) == "ErrorInterface { e: \(msg) }")
assert(error.localizedDescription == "ErrorInterface { e: \(msg) }")
}

do {
try oopsEnum()
fatalError("Should have thrown")
} catch let e as Error {
assert(e == Error.Oops)
assert(String(describing: e) == "Oops")
assert(String(reflecting: e) == "error_types.Error.Oops")
}
do {
try oopsEnum()
fatalError("Should have thrown")
} catch {
assert(String(describing: error) == "Oops")
assert(String(reflecting: error) == "error_types.Error.Oops")
assert(error.localizedDescription == "error_types.Error.Oops")
}

do {
try oopsNowrap()
fatalError("Should have thrown")
Expand All @@ -22,5 +52,5 @@ do {
let e = getError(message: "the error")
assert(String(describing: e) == "the error")
assert(String(reflecting: e) == "ErrorInterface { e: the error }")
assert(Error.self is Swift.Error.Type)
// assert(Error.self is Swift.Error.Type) -- always true!
assert(Error.self != Swift.Error.self)
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,8 @@ public struct {{ ffi_converter_name }}: FfiConverterRustBuffer {
{% if !contains_object_references %}
extension {{ type_name }}: Equatable, Hashable {}
{% endif %}
extension {{ type_name }}: Swift.Error { }
extension {{ type_name }}: Foundation.LocalizedError {
public var errorDescription: String? {
String(reflecting: self)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ public struct {{ ffi_converter_name }}: FfiConverter {

{# Objects as error #}
{%- if is_error %}

extension {{ type_name }}: Foundation.LocalizedError {
public var errorDescription: String? {
String(reflecting: self)
}
}

{# Due to some mismatches in the ffi converter mechanisms, errors are a RustBuffer holding a pointer #}
public struct {{ ffi_converter_name }}__as_error: FfiConverterRustBuffer {
public static func lift(_ buf: RustBuffer) throws -> {{ type_name }} {
Expand Down

0 comments on commit e6b4858

Please sign in to comment.