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

[ABW-3454] iOS - Display proper error messages for Sargon errors #168

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 Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sargon"
version = "1.0.15"
version = "1.0.16"
edition = "2021"
build = "build.rs"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Foundation
import SargonUniFFI

public typealias SargonError = CommonError
Expand All @@ -15,3 +16,13 @@ extension SargonError: CustomStringConvertible {
errorMessage
}
}

extension SargonError: LocalizedError {
public var errorDescription: String? {
let errorCodeFormatted = "Error code: \(errorCode)"
let errorMessageFormatted: String? = isSafeToShowErrorMessageFromError(error: self) ? "Error message: \(errorMessage)" : nil
Copy link
Contributor

Choose a reason for hiding this comment

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

I would opt to show the full error message in Debug mode.

return [errorCodeFormatted, errorMessageFormatted]
.compactMap { $0 }
.joined(separator: "\n")
}
}
10 changes: 10 additions & 0 deletions apple/Tests/TestCases/Prelude/SargonErrorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,14 @@ final class SargonErrorTests: Test<SargonError> {
let sut = SUT.UnknownNetworkForId(badValue: 99)
XCTAssertEqual(sut.debugDescription, "10049: No network found with id: '99'")
}

func test_localized_description_for_sensitive_error() {
let sut = SUT.UnknownNetworkForId(badValue: 99)
XCTAssertEqual(sut.localizedDescription, "Error code: 10049")
}

func test_localized_description_for_non_sensitive_error() {
let sut = SUT.FailedToDeserializeJsonToValue(jsonByteCount: 100, typeName: "TypeName", serdeMessage: "serdeMessage")
XCTAssertEqual(sut.localizedDescription, "Error code: 10070\nError message: Failed deserialize JSON with #100 bytes to value of type TypeName with error: \"serdeMessage\"")
}
}
25 changes: 25 additions & 0 deletions src/core/error/common_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,13 +508,22 @@ impl CommonError {
pub fn error_code(&self) -> u32 {
unsafe { *<*const _>::from(self).cast::<u32>() }
}

pub fn is_safe_to_show_error_message(&self) -> bool {
matches!(self, CommonError::FailedToDeserializeJSONToValue { .. })
}
}

#[uniffi::export]
pub fn error_code_from_error(error: &CommonError) -> u32 {
error.error_code()
}

#[uniffi::export]
pub fn is_safe_to_show_error_message_from_error(error: &CommonError) -> bool {
error.is_safe_to_show_error_message()
}

#[cfg(test)]
mod tests {
use crate::prelude::*;
Expand All @@ -533,4 +542,20 @@ mod tests {
let sut = CommonError::UnknownNetworkForID { bad_value: 0 };
assert_eq!(error_code_from_error(&sut), 10049);
}

#[test]
fn is_safe_to_show_error_message() {
let sut = CommonError::FailedToDeserializeJSONToValue {
json_byte_count: 100,
type_name: "TypeName".to_string(),
serde_message: "message".to_string(),
};
assert!(is_safe_to_show_error_message_from_error(&sut));
}

#[test]
fn is_not_safe_to_show_error_message() {
let sut = CommonError::UnknownNetworkForID { bad_value: 0 };
assert!(!is_safe_to_show_error_message_from_error(&sut));
}
}
Loading