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

Convert TezosKitError to enum type #185

Merged
merged 9 commits into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from 6 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
10 changes: 5 additions & 5 deletions Extensions/PromiseKit/TezosNode/TezosNodeClient+Promises.swift
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ extension TezosNodeClient {
)
case .failure(let error):
return Promise { seal in
seal.reject(TezosKitError(kind: .transactionFormationFailure, underlyingError: error.underlyingError))
seal.reject(TezosKitError.transactionFormationFailure(underlyingError: error))
}
}
}
Expand Down Expand Up @@ -339,7 +339,7 @@ extension TezosNodeClient {
)
case .failure(let error):
return Promise { seal in
seal.reject(TezosKitError(kind: .transactionFormationFailure, underlyingError: error.underlyingError))
seal.reject(TezosKitError.transactionFormationFailure(underlyingError: error))
}
}
}
Expand Down Expand Up @@ -374,7 +374,7 @@ extension TezosNodeClient {
)
case .failure(let error):
return Promise { seal in
seal.reject(TezosKitError(kind: .transactionFormationFailure, underlyingError: error.underlyingError))
seal.reject(TezosKitError.transactionFormationFailure(underlyingError: error))
}
}
}
Expand Down Expand Up @@ -405,7 +405,7 @@ extension TezosNodeClient {
)
case .failure(let error):
return Promise { seal in
seal.reject(TezosKitError(kind: .transactionFormationFailure, underlyingError: error.underlyingError))
seal.reject(TezosKitError.transactionFormationFailure(underlyingError: error))
}
}
}
Expand Down Expand Up @@ -437,7 +437,7 @@ extension TezosNodeClient {
)
case .failure(let error):
return Promise { seal in
seal.reject(TezosKitError(kind: .transactionFormationFailure, underlyingError: error.underlyingError))
seal.reject(TezosKitError.transactionFormationFailure(underlyingError: error))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ extension NetworkClientTest {
headerFields: nil
)
fakeURLSession.data = "Result".data(using: .utf8)
fakeURLSession.error = TezosKitError(kind: .unknown, underlyingError: nil)
fakeURLSession.error = TezosKitError.unknown(description: nil)

let expectation = XCTestExpectation(description: "Promise is resolved")
let rpc = RPC(endpoint: "/test", responseAdapterClass: StringResponseAdapter.self)
Expand Down
16 changes: 8 additions & 8 deletions Tests/UnitTests/TezosKit/ConseilClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,27 +97,27 @@ final class ConseilClientTests: XCTestCase {

func testCombineResults_aNil() {
let a: Result<[Transaction], TezosKitError>? = nil
let b: Result<[Transaction], TezosKitError>? = .failure(TezosKitError(kind: .unknown))
let b: Result<[Transaction], TezosKitError>? = .failure(.unknown(description: nil))
XCTAssertNil(ConseilClient.combine(a, b))
}

func testCombineResults_bNil() {
let a: Result<[Transaction], TezosKitError>? = .failure(TezosKitError(kind: .unknown))
let a: Result<[Transaction], TezosKitError>? = .failure(.unknown(description: nil))
let b: Result<[Transaction], TezosKitError>? = nil
XCTAssertNil(ConseilClient.combine(a, b))
}

func testCombineResults_bothNoNil() {
let a: Result<[Transaction], TezosKitError>? = .failure(TezosKitError(kind: .unknown))
let b: Result<[Transaction], TezosKitError>? = .failure(TezosKitError(kind: .unknown))
let a: Result<[Transaction], TezosKitError>? = .failure(.unknown(description: nil))
let b: Result<[Transaction], TezosKitError>? = .failure(.unknown(description: nil))
XCTAssertNotNil(ConseilClient.combine(a, b))
}

func testCombineResults_bothFailure() {
let errorA = TezosKitError(kind: .unexpectedResponse)
let errorA = TezosKitError.unexpectedResponse(description: "unepected response")
let a: Result<[Transaction], TezosKitError> = .failure(errorA)

let errorB = TezosKitError(kind: .invalidURL)
let errorB = TezosKitError.invalidURL
let b: Result<[Transaction], TezosKitError> = .failure(errorB)

guard let result = ConseilClient.combine(a, b) else {
Expand All @@ -133,7 +133,7 @@ final class ConseilClientTests: XCTestCase {
}

func testCombineResults_aFailure() {
let errorA = TezosKitError(kind: .unexpectedResponse)
let errorA = TezosKitError.unexpectedResponse(description: "unepected response")
let a: Result<[Transaction], TezosKitError> = .failure(errorA)

let b: Result<[Transaction], TezosKitError> = .success([.testTransaction])
Expand All @@ -153,7 +153,7 @@ final class ConseilClientTests: XCTestCase {
func testCombineResults_bFailure() {
let a: Result<[Transaction], TezosKitError> = .success([.testTransaction])

let errorB = TezosKitError(kind: .invalidURL)
let errorB = TezosKitError.invalidURL
let b: Result<[Transaction], TezosKitError> = .failure(errorB)

guard let result = ConseilClient.combine(a, b) else {
Expand Down
2 changes: 1 addition & 1 deletion Tests/UnitTests/TezosKit/ForgingServiceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class ForgingServiceTests: XCTestCase {
case .success:
XCTFail()
case .failure(let error):
XCTAssertEqual(error.kind, .localForgingNotSupportedForOperation)
XCTAssertEqual(error, .localForgingNotSupportedForOperation)
}
forgeCompletionExpectation.fulfill()
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/UnitTests/TezosKit/NetworkClientTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class NetworkClientTest: XCTestCase {
headerFields: nil
)
fakeURLSession.data = "Result".data(using: .utf8)
fakeURLSession.error = TezosKitError(kind: .unknown, underlyingError: nil)
fakeURLSession.error = TezosKitError.unknown(description: nil)

let expectation = XCTestExpectation(description: "Completion is Called")
let rpc = RPC(endpoint: "/test", responseAdapterClass: StringResponseAdapter.self)
Expand Down
19 changes: 14 additions & 5 deletions Tests/UnitTests/TezosKit/PreapplicationServiceTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ final class PreapplicationServiceTest: XCTestCase {
private static let preapplyEndpoint = "/chains/main/blocks/" + .testBranch + "/helpers/preapply/operations"

// swiftlint:disable line_length
private static let invalidPreapplicationResponseErrorID = "proto.003-PsddFKi3.contract.balance_too_low"
private static let invalidPreapplicationResponse =
"[{\"contents\":[{\"kind\":\"transaction\",\"source\":\"tz1XVJ8bZUXs7r5NV8dHvuiBhzECvLRLR3jW\",\"fee\":\"1272\",\"counter\":\"30802\",\"gas_limit\":\"10100\",\"storage_limit\":\"257\",\"amount\":\"10000000000000\",\"destination\":\"tz3WXYtyDUNL91qfiCJtVUX746QpNv5i5ve5\",\"metadata\":{\"balance_updates\":[{\"kind\":\"contract\",\"contract\":\"tz1XVJ8bZUXs7r5NV8dHvuiBhzECvLRLR3jW\",\"change\":\"-1272\"},{\"kind\":\"freezer\",\"category\":\"fees\",\"delegate\":\"tz1Ke2h7sDdakHJQh8WX4Z372du1KChsksyU\",\"level\":125,\"change\":\"1272\"}],\"operation_result\":{\"status\":\"failed\",\"errors\":[{\"kind\":\"temporary\",\"id\":\"proto.003-PsddFKi3.contract.balance_too_low\",\"contract\":\"tz1XVJ8bZUXs7r5NV8dHvuiBhzECvLRLR3jW\",\"balance\":\"98751713\",\"amount\":\"10000000000000\"}]}}}],\"signature\":\"edsigu16pv1NUsXuJkwWDAqvFDbhcsRAHbdxbYJcN7AShN4yDspRmsP5kgbzs2osTHGGDkyED3vjQFcbskv3BVESJ7tpchmbbop\"}]"
"[{\"contents\":[{\"kind\":\"transaction\",\"source\":\"tz1XVJ8bZUXs7r5NV8dHvuiBhzECvLRLR3jW\",\"fee\":\"1272\",\"counter\":\"30802\",\"gas_limit\":\"10100\",\"storage_limit\":\"257\",\"amount\":\"10000000000000\",\"destination\":\"tz3WXYtyDUNL91qfiCJtVUX746QpNv5i5ve5\",\"metadata\":{\"balance_updates\":[{\"kind\":\"contract\",\"contract\":\"tz1XVJ8bZUXs7r5NV8dHvuiBhzECvLRLR3jW\",\"change\":\"-1272\"},{\"kind\":\"freezer\",\"category\":\"fees\",\"delegate\":\"tz1Ke2h7sDdakHJQh8WX4Z372du1KChsksyU\",\"level\":125,\"change\":\"1272\"}],\"operation_result\":{\"status\":\"failed\",\"errors\":[{\"kind\":\"temporary\",\"id\":\"\(invalidPreapplicationResponseErrorID)\",\"contract\":\"tz1XVJ8bZUXs7r5NV8dHvuiBhzECvLRLR3jW\",\"balance\":\"98751713\",\"amount\":\"10000000000000\"}]}}}],\"signature\":\"edsigu16pv1NUsXuJkwWDAqvFDbhcsRAHbdxbYJcN7AShN4yDspRmsP5kgbzs2osTHGGDkyED3vjQFcbskv3BVESJ7tpchmbbop\"}]"
// swiftlint:enable line_length

func testPreapplicationValidOperation() {
Expand Down Expand Up @@ -49,7 +50,10 @@ final class PreapplicationServiceTest: XCTestCase {
XCTFail()
return
}
XCTAssertEqual(result.kind, .preapplicationError)
XCTAssertEqual(
result,
.preapplicationError(description: PreapplicationServiceTest.invalidPreapplicationResponseErrorID)
)
preapplicationCompletionExpectation.fulfill()
}

Expand All @@ -71,7 +75,7 @@ final class PreapplicationServiceTest: XCTestCase {
XCTFail()
return
}
XCTAssertEqual(result.kind, .unexpectedResponse)
XCTAssertEqual(result, .unexpectedResponse(description: "No data in response"))
preapplicationCompletionExpectation.fulfill()
}

Expand All @@ -92,7 +96,12 @@ final class PreapplicationServiceTest: XCTestCase {
input: PreapplicationServiceTest.invalidPreapplicationResponse.data(using: .utf8)!
)!
let error = PreapplicationService.preapplicationError(from: json)!
XCTAssertEqual(error.kind, .preapplicationError)
XCTAssert(error.underlyingError!.contains("contract.balance_too_low"))

switch error {
case .preapplicationError(let errorText):
XCTAssert(errorText.contains("contract.balance_too_low"))
default:
XCTFail("Wrong error type reported")
}
}
}
23 changes: 11 additions & 12 deletions Tests/UnitTests/TezosKit/RPCResponseHandlerTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ class RPCResponseHandlerTest: XCTestCase {

switch result {
case .failure(let tezosKitError):
XCTAssertEqual(tezosKitError.kind, .unexpectedRequestFormat)
XCTAssertEqual(tezosKitError.underlyingError, RPCResponseHandlerTest.testErrorString)
XCTAssertEqual(tezosKitError, .unexpectedRequestFormat(description: RPCResponseHandlerTest.testErrorString))
case .success:
XCTFail()
}
Expand All @@ -63,8 +62,7 @@ class RPCResponseHandlerTest: XCTestCase {

switch result {
case .failure(let tezosKitError):
XCTAssertEqual(tezosKitError.kind, .unexpectedResponse)
XCTAssertEqual(tezosKitError.underlyingError, RPCResponseHandlerTest.testErrorString)
XCTAssertEqual(tezosKitError, .unexpectedResponse(description: RPCResponseHandlerTest.testErrorString))
case .success:
XCTFail()
}
Expand All @@ -81,8 +79,7 @@ class RPCResponseHandlerTest: XCTestCase {

switch result {
case .failure(let tezosKitError):
XCTAssertEqual(tezosKitError.kind, .unknown)
XCTAssertEqual(tezosKitError.underlyingError, RPCResponseHandlerTest.testErrorString)
XCTAssertEqual(tezosKitError, .unknown(description: RPCResponseHandlerTest.testErrorString))
case .success:
XCTFail()
}
Expand All @@ -100,8 +97,12 @@ class RPCResponseHandlerTest: XCTestCase {

switch result {
case .failure(let tezosKitError):
XCTAssertEqual(tezosKitError.kind, .rpcError)
XCTAssertEqual(tezosKitError.underlyingError, testError.localizedDescription)
switch tezosKitError {
case .rpcError(let description):
XCTAssertEqual(description, testError.localizedDescription)
default:
XCTFail("Wrong error type reported.")
}
case .success:
XCTFail()
}
Expand Down Expand Up @@ -135,8 +136,7 @@ class RPCResponseHandlerTest: XCTestCase {

switch result {
case .failure(let tezosKitError):
XCTAssertEqual(tezosKitError.kind, .unexpectedResponse)
XCTAssertEqual(tezosKitError.underlyingError, nil)
XCTAssertEqual(tezosKitError, .unexpectedResponse(description: "No data in response"))
case .success:
XCTFail()
}
Expand All @@ -153,8 +153,7 @@ class RPCResponseHandlerTest: XCTestCase {

switch result {
case .failure(let tezosKitError):
XCTAssertEqual(tezosKitError.kind, .unexpectedResponse)
XCTAssertEqual(tezosKitError.underlyingError, nil)
XCTAssertEqual(tezosKitError, .unexpectedResponse(description: "No data in response"))
case .success:
XCTFail()
}
Expand Down
29 changes: 0 additions & 29 deletions Tests/UnitTests/TezosKit/TezosKitErrorCodesTest.swift

This file was deleted.

6 changes: 0 additions & 6 deletions TezosKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@
39956383AC208C08BA1F8D85 /* KeyChainWallet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2319DB8D9848B66780D89EBB /* KeyChainWallet.swift */; };
3AD54821F71295B8173C3241 /* ManagerContractClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = 606292A7C4D50AD99EDC7208 /* ManagerContractClient.swift */; };
3C99F5A58A32231BE0820A4A /* GetBallotsRPCTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6AF9ECBC79C00C0B082798EB /* GetBallotsRPCTest.swift */; };
3CC9387F30A2C0880378097A /* TezosKitErrorCodesTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = AF4254B915770CD924FBBE9D /* TezosKitErrorCodesTest.swift */; };
3D0DFC6B5FED520A51FFA5ED /* SigningService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2B1E035A6252AD79E6A4AAD1 /* SigningService.swift */; };
3DD35013DA97BA70B30E7ADF /* MnemonicKit.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 1DD4B1012288DD6F90E8C78A /* MnemonicKit.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
3E2839CA355D9C016F8D57E8 /* Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4515BCFCC3C373EAF8900454 /* Logger.swift */; };
Expand Down Expand Up @@ -298,7 +297,6 @@
99EF9D9DD18902B98C5B2739 /* ConseilNetwork.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11388A975CDB5601AFC99189 /* ConseilNetwork.swift */; };
9A1BE88DCC44E98E1D85C26C /* IntegerResponseAdapter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 109F98709DCC0A5351158718 /* IntegerResponseAdapter.swift */; };
9ACC5804F3B0BD26D25223CC /* JSONDictionaryResponseAdapterTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF3F1D59FA8A152B8A66043A /* JSONDictionaryResponseAdapterTest.swift */; };
9B22F93D4284980497E94D9E /* TezosKitErrorCodesTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = AF4254B915770CD924FBBE9D /* TezosKitErrorCodesTest.swift */; };
9B3002D8742780ABC34C5C10 /* SimulationService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0BFF651BFE4E4A3E90313FA9 /* SimulationService.swift */; };
9C490216EE45DD3B2D5FA043 /* OperationWithCounter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04781056C289A009489C072C /* OperationWithCounter.swift */; };
9D76FFEA43D41606A08098FB /* SmartContractInvocationOperationTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8938737BEDF3F8E643E42865 /* SmartContractInvocationOperationTest.swift */; };
Expand Down Expand Up @@ -777,7 +775,6 @@
AD2FD2E17433D630AA933B5B /* SignedProtocolOperationPayload.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignedProtocolOperationPayload.swift; sourceTree = "<group>"; };
AD5FAF254AF66B1A805ABF27 /* AbstractOperationTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AbstractOperationTest.swift; sourceTree = "<group>"; };
AE3CEDF87CB74674EF5E2BB5 /* BytesMichelsonParameter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BytesMichelsonParameter.swift; sourceTree = "<group>"; };
AF4254B915770CD924FBBE9D /* TezosKitErrorCodesTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TezosKitErrorCodesTest.swift; sourceTree = "<group>"; };
AFC77643CD2C374042E0B932 /* CryptoSwift.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = CryptoSwift.framework; sourceTree = "<group>"; };
B01627EE910CBE5821502281 /* GetAddressManagerKeyRPC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetAddressManagerKeyRPC.swift; sourceTree = "<group>"; };
B033280AD0AE71CF00F15555 /* PreapplyOperationRPC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreapplyOperationRPC.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1029,7 +1026,6 @@
E0B48169F083D2DAEB462840 /* SimulationServiceTest.swift */,
8938737BEDF3F8E643E42865 /* SmartContractInvocationOperationTest.swift */,
CE7B474664A9C0C8EA93EA08 /* StringResponseAdapterTest.swift */,
AF4254B915770CD924FBBE9D /* TezosKitErrorCodesTest.swift */,
A480E400F3C3D065639376F4 /* TezosNodeClientTests.swift */,
88F56EE382535F0A59A52E76 /* TezResponseAdapterTest.swift */,
77CF8E4119383EA36202FE25 /* TezTest.swift */,
Expand Down Expand Up @@ -1841,7 +1837,6 @@
6EA2C05ED0589F4D707AA291 /* TestObjects.swift in Sources */,
42942076138616B8E91C9280 /* TezResponseAdapterTest.swift in Sources */,
FEFBD0AE42D0285E2DBFAEDB /* TezTest.swift in Sources */,
3CC9387F30A2C0880378097A /* TezosKitErrorCodesTest.swift in Sources */,
59A2F834737A398C874C200B /* TezosNodeClientTests.swift in Sources */,
D351C031963368534FAEBC50 /* TokenContractClientTests.swift in Sources */,
DC9C8834E1D992263ED86CBA /* TransactionOperationTest.swift in Sources */,
Expand Down Expand Up @@ -2083,7 +2078,6 @@
381DBFEA8AB85D0A93AD40FE /* TestObjects.swift in Sources */,
C6AAFF78C788015D767EB6B8 /* TezResponseAdapterTest.swift in Sources */,
127F92545866411A37E2563A /* TezTest.swift in Sources */,
9B22F93D4284980497E94D9E /* TezosKitErrorCodesTest.swift in Sources */,
32144B5BCBA06B8114DF0F0D /* TezosNodeClientTests.swift in Sources */,
9832CCBED4906F7744DAF714 /* TokenContractClientTests.swift in Sources */,
DAB469501CBBA2BC3259F563 /* TransactionOperationTest.swift in Sources */,
Expand Down
53 changes: 11 additions & 42 deletions TezosKit/Common/TezosKitError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,15 @@

import Foundation

public struct TezosKitError: Error {
/// Enumeration representing possible kinds of errors.
public enum ErrorKind: String {
case internalError
case invalidURL
case localForgingNotSupportedForOperation
case preapplicationError
case rpcError
case signingError
case transactionFormationFailure
case unexpectedRequestFormat
case unexpectedResponse
case unknown
}

/// The error code which occurred.
public let kind: ErrorKind

/// The underlying error returned from a subsystem, if one exists.
public let underlyingError: String?

public init(kind: ErrorKind, underlyingError: String? = nil) {
self.kind = kind
self.underlyingError = underlyingError
}
}

extension TezosKitError: LocalizedError {
public var errorDescription: String? {
let errorKindDesc = "TezosKitError " + kind.rawValue
if let underlyingError = self.underlyingError {
return underlyingError + " (" + errorKindDesc + ")"
} else {
return errorKindDesc
}
}
}

extension TezosKitError: Equatable {
public static func == (lhs: TezosKitError, rhs: TezosKitError) -> Bool {
return lhs.kind == rhs.kind && lhs.underlyingError == rhs.underlyingError
}
public indirect enum TezosKitError: Error, Equatable {
case internalError
case invalidURL
case localForgingNotSupportedForOperation
case preapplicationError(description: String)
case rpcError(description: String)
case signingError
case transactionFormationFailure(underlyingError: TezosKitError)
case unexpectedRequestFormat(description: String)
case unexpectedResponse(description: String)
case unknown(description: String?)
}
2 changes: 1 addition & 1 deletion TezosKit/Conseil/ConseilClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public class ConseilClient {

guard let combinedResult = ConseilClient.combine(receivedResult, sentResult) else {
self.callbackQueue.async {
completion(.failure(TezosKitError(kind: .unknown)))
completion(.failure(.unknown(description: nil)))
}
return
}
Expand Down
Loading