Skip to content

Commit

Permalink
Swift: Add missing error cases
Browse files Browse the repository at this point in the history
And set up a script to catch them in the future.
  • Loading branch information
jrose-signal committed Apr 4, 2024
1 parent 02e03ee commit 53ba4c2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,8 @@ jobs:

- run: sudo apt-get update && sudo apt-get install protobuf-compiler

- run: swift/verify_error_codes.sh

- name: Build libsignal-ffi
run: swift/build_ffi.sh -d -v --verify-ffi

Expand Down
6 changes: 6 additions & 0 deletions swift/Sources/LibSignalClient/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public enum SignalError: Error {
case invalidMessage(String)
case invalidKey(String)
case invalidSignature(String)
case invalidAttestationData(String)
case fingerprintVersionMismatch(String)
case fingerprintParsingError(String)
case sealedSenderSelfSend(String)
Expand Down Expand Up @@ -55,6 +56,7 @@ public enum SignalError: Error {
case callbackError(String)
case webSocketError(String)
case connectionTimeoutError(String)
case connectionFailed(String)
case networkProtocolError(String)
case cdsiInvalidToken(String)
case rateLimitedError(retryAfter: TimeInterval, message: String)
Expand Down Expand Up @@ -108,6 +110,8 @@ internal func checkError(_ error: SignalFfiErrorRef?) throws {
throw SignalError.invalidKey(errStr)
case SignalErrorCodeInvalidSignature:
throw SignalError.invalidSignature(errStr)
case SignalErrorCodeInvalidAttestationData:
throw SignalError.invalidAttestationData(errStr)
case SignalErrorCodeFingerprintVersionMismatch:
throw SignalError.fingerprintVersionMismatch(errStr)
case SignalErrorCodeUntrustedIdentity:
Expand Down Expand Up @@ -172,6 +176,8 @@ internal func checkError(_ error: SignalFfiErrorRef?) throws {
throw SignalError.webSocketError(errStr)
case SignalErrorCodeConnectionTimedOut:
throw SignalError.connectionTimeoutError(errStr)
case SignalErrorCodeConnectionFailed:
throw SignalError.connectionFailed(errStr)
case SignalErrorCodeNetworkProtocol:
throw SignalError.networkProtocolError(errStr)
case SignalErrorCodeCdsiInvalidToken:
Expand Down
2 changes: 1 addition & 1 deletion swift/Tests/LibSignalClientTests/NetTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ final class NetTests: XCTestCase {
}
do {
try failWithError("AttestationDataError")
} catch SignalError.unknown(SignalErrorCodeInvalidAttestationData.rawValue, let message) {
} catch SignalError.invalidAttestationData(let message) {
XCTAssertEqual(message, "SGX operation failed: attestation data invalid: fake reason")
}
do {
Expand Down
22 changes: 22 additions & 0 deletions swift/verify_error_codes.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

#
# Copyright 2024 Signal Messenger, LLC.
# SPDX-License-Identifier: AGPL-3.0-only
#

set -euo pipefail

SCRIPT_DIR=$(dirname "$0")
cd "${SCRIPT_DIR}"/..
. bin/build_helpers.sh

# Get the list of *handled* error codes and the list of *declared* error codes and compare them.
# When modifying this script, be mindful of potential differences between GNU grep and BSD grep.
if ! diff -U 1 -L 'Codes handled in Error.swift' -L 'Codes declared in signal_ffi.h' \
<(grep -o -E 'case SignalErrorCode[^:]+' swift/Sources/LibSignalClient/Error.swift | cut -d' ' -f 2 | sort -u) \
<(grep -o -E '^ SignalErrorCode[^,]+' swift/Sources/SignalFfi/signal_ffi.h | grep -v 'UnknownError' | cut -d' ' -f 3 | sort -u)
then
printf '\n=== Make sure Error.swift is in sync with the error codes declared in Rust! ===\n\n' >&2
exit 1
fi

0 comments on commit 53ba4c2

Please sign in to comment.