Skip to content

Commit d7b0077

Browse files
committed
Throw the proper errors in the code related to phone numbers.
1 parent d89142e commit d7b0077

File tree

7 files changed

+165
-19
lines changed

7 files changed

+165
-19
lines changed

TextInputKit.xcodeproj/project.pbxproj

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@
4848
7C2BC1601E29300300C731E3 /* PhoneNumberKit+Cache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C2BC15E1E29300300C731E3 /* PhoneNumberKit+Cache.swift */; };
4949
7C2BC1621E29315800C731E3 /* CachedPhoneNumberKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C2BC1611E29315800C731E3 /* CachedPhoneNumberKit.swift */; };
5050
7C2BC1631E29315800C731E3 /* CachedPhoneNumberKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C2BC1611E29315800C731E3 /* CachedPhoneNumberKit.swift */; };
51+
7C2BC1651E29531E00C731E3 /* PhoneNumberTextInputError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C2BC1641E29531E00C731E3 /* PhoneNumberTextInputError.swift */; };
52+
7C2BC1661E29531E00C731E3 /* PhoneNumberTextInputError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C2BC1641E29531E00C731E3 /* PhoneNumberTextInputError.swift */; };
53+
7C2BC1681E29556A00C731E3 /* TextInputKitError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C2BC1671E29556A00C731E3 /* TextInputKitError.swift */; };
54+
7C2BC1691E29556A00C731E3 /* TextInputKitError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C2BC1671E29556A00C731E3 /* TextInputKitError.swift */; };
55+
7C2BC16B1E295A9700C731E3 /* PhoneNumberKit+Framework.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C2BC16A1E295A9700C731E3 /* PhoneNumberKit+Framework.swift */; };
56+
7C2BC16C1E295A9700C731E3 /* PhoneNumberKit+Framework.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C2BC16A1E295A9700C731E3 /* PhoneNumberKit+Framework.swift */; };
5157
7C32AA331E07B70B004669A8 /* NSText+TextInputKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C32AA321E07B70B004669A8 /* NSText+TextInputKit.swift */; };
5258
7C32AA3D1E0822E7004669A8 /* NSWindow+TextInputKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C32AA3C1E0822E7004669A8 /* NSWindow+TextInputKit.swift */; };
5359
7C32AA5C1E0835E1004669A8 /* BankCardHolderNameTextInputFormatterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C32AA591E0834E4004669A8 /* BankCardHolderNameTextInputFormatterTests.swift */; };
@@ -249,6 +255,9 @@
249255
7C2BC1571E2929BD00C731E3 /* ObjectCache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ObjectCache.swift; sourceTree = "<group>"; };
250256
7C2BC15E1E29300300C731E3 /* PhoneNumberKit+Cache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "PhoneNumberKit+Cache.swift"; sourceTree = "<group>"; };
251257
7C2BC1611E29315800C731E3 /* CachedPhoneNumberKit.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CachedPhoneNumberKit.swift; sourceTree = "<group>"; };
258+
7C2BC1641E29531E00C731E3 /* PhoneNumberTextInputError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PhoneNumberTextInputError.swift; sourceTree = "<group>"; };
259+
7C2BC1671E29556A00C731E3 /* TextInputKitError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TextInputKitError.swift; sourceTree = "<group>"; };
260+
7C2BC16A1E295A9700C731E3 /* PhoneNumberKit+Framework.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "PhoneNumberKit+Framework.swift"; sourceTree = "<group>"; };
252261
7C2BD0371DE2C3A900A7A6C0 /* TextInputKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = TextInputKit.framework; sourceTree = BUILT_PRODUCTS_DIR; };
253262
7C2BD0441DE2C40800A7A6C0 /* TextInputKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = TextInputKit.framework; sourceTree = BUILT_PRODUCTS_DIR; };
254263
7C2BD04C1DE2C44C00A7A6C0 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@@ -389,14 +398,15 @@
389398
7C2BD04E1DE2C46500A7A6C0 /* Code */ = {
390399
isa = PBXGroup;
391400
children = (
401+
7CE691471DE81500003F32D2 /* TextInputKit.swift */,
402+
7C2BC1671E29556A00C731E3 /* TextInputKitError.swift */,
392403
7C19F1FC1E048C8600D2A68F /* AppKitExtensions */,
393404
7C2BC1541E2929BD00C731E3 /* Common */,
394405
7CE6918B1DE815FD003F32D2 /* SpecializedTextInput */,
395406
7CE6912F1DE81500003F32D2 /* SwiftExtensions */,
396407
7CE691321DE81500003F32D2 /* TextInputDriver */,
397408
7CE691381DE81500003F32D2 /* TextInputFormat */,
398409
7CE691401DE81500003F32D2 /* TextInputFormatter */,
399-
7CE691471DE81500003F32D2 /* TextInputKit.swift */,
400410
7CE691481DE81500003F32D2 /* TextInputSerializer */,
401411
7CE6914E1DE81500003F32D2 /* UIKitExtensions */,
402412
);
@@ -581,8 +591,10 @@
581591
7C2BC1611E29315800C731E3 /* CachedPhoneNumberKit.swift */,
582592
7CE691A31DE89D5A003F32D2 /* PhoneNumber.swift */,
583593
7C2BC15E1E29300300C731E3 /* PhoneNumberKit+Cache.swift */,
594+
7C2BC16A1E295A9700C731E3 /* PhoneNumberKit+Framework.swift */,
584595
7C2BC10C1E28B3C700C731E3 /* PhoneNumberKitLoader.swift */,
585596
7C2BC1091E282A3200C731E3 /* PhoneNumberKitTypes.swift */,
597+
7C2BC1641E29531E00C731E3 /* PhoneNumberTextInputError.swift */,
586598
7CE691B31DEAC595003F32D2 /* PhoneNumberTextInputFormatter.swift */,
587599
7CE691941DE8165F003F32D2 /* PhoneNumberTextInputOptions.swift */,
588600
7C2BC0F51E2642B100C731E3 /* PhoneNumberUtils.swift */,
@@ -804,6 +816,7 @@
804816
7C2BC1621E29315800C731E3 /* CachedPhoneNumberKit.swift in Sources */,
805817
7CE691691DE81500003F32D2 /* TextInputFormat+Map.swift in Sources */,
806818
7C0578CF1DFC69230071DA47 /* TextInputValidationResult+OptimizeChanges.swift in Sources */,
819+
7C2BC16B1E295A9700C731E3 /* PhoneNumberKit+Framework.swift in Sources */,
807820
7C3FE44B1E032E9C0094CE4D /* BankCardHolderNameTextInputOptions.swift in Sources */,
808821
7CE691871DE81500003F32D2 /* UITextField+TextInputKit.swift in Sources */,
809822
7C3FE4491E032E9C0094CE4D /* BankCardHolderNameTextInputFormatter.swift in Sources */,
@@ -817,8 +830,10 @@
817830
7CE692251DF4484E003F32D2 /* BankCardNumberTextInputFormatter.swift in Sources */,
818831
7CE691891DE81500003F32D2 /* UITextInput+TextInputKit.swift in Sources */,
819832
7C2BC10A1E282A3200C731E3 /* PhoneNumberKitTypes.swift in Sources */,
833+
7C2BC1651E29531E00C731E3 /* PhoneNumberTextInputError.swift in Sources */,
820834
7CE6918F1DE81625003F32D2 /* TextInputFormats+BankCardNumber.swift in Sources */,
821835
7CE691531DE81500003F32D2 /* Range+TextInputKit.swift in Sources */,
836+
7C2BC1681E29556A00C731E3 /* TextInputKitError.swift in Sources */,
822837
7C50F9401E12A7DC00D3AF09 /* TextInputFormats+BankCardExpiryDate.swift in Sources */,
823838
7CE6919E1DE89178003F32D2 /* BankCardNumber.swift in Sources */,
824839
7C2BC10D1E28B3C700C731E3 /* PhoneNumberKitLoader.swift in Sources */,
@@ -869,6 +884,7 @@
869884
files = (
870885
7CE691661DE81500003F32D2 /* TextInputFormat+Formatter.swift in Sources */,
871886
7C2BC10B1E282A3200C731E3 /* PhoneNumberKitTypes.swift in Sources */,
887+
7C2BC1661E29531E00C731E3 /* PhoneNumberTextInputError.swift in Sources */,
872888
7C2BC15B1E2929BD00C731E3 /* MutableBox.swift in Sources */,
873889
7C19F1FE1E048CBF00D2A68F /* NSTextField+TextInputKit.swift in Sources */,
874890
7CE6916C1DE81500003F32D2 /* TextInputFormats+Plain.swift in Sources */,
@@ -895,8 +911,10 @@
895911
7C50F9371E12A7DC00D3AF09 /* BankCardExpiryDateTextInputError.swift in Sources */,
896912
7C02BA1B1E23ED560023AC7E /* TextInputEventNotifier.swift in Sources */,
897913
7CE691B51DEAC595003F32D2 /* PhoneNumberTextInputFormatter.swift in Sources */,
914+
7C2BC1691E29556A00C731E3 /* TextInputKitError.swift in Sources */,
898915
7CE691641DE81500003F32D2 /* TextInputFormat+Filter.swift in Sources */,
899916
7C50F9411E12A7DC00D3AF09 /* TextInputFormats+BankCardExpiryDate.swift in Sources */,
917+
7C2BC16C1E295A9700C731E3 /* PhoneNumberKit+Framework.swift in Sources */,
900918
7CE692361DF44A62003F32D2 /* String+TextInputKit.swift in Sources */,
901919
7CE691801DE81500003F32D2 /* TextInputSerializer.swift in Sources */,
902920
7C72504D1E09B36800F916ED /* FormatterObjectValue.swift in Sources */,
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// PhoneNumberKit+Framework.swift
3+
// TextInputKit
4+
//
5+
// Created by Artem Starosvetskiy on 13/01/2017.
6+
// Copyright © 2017 Artem Starosvetskiy. All rights reserved.
7+
//
8+
9+
import Foundation
10+
import PhoneNumberKit
11+
12+
extension PhoneNumberKit {
13+
14+
static func checkThatFrameworkIsLoaded() throws {
15+
let isPhoneNumberKitFrameworkLoaded = (NSClassFromString("PhoneNumberKit.PhoneNumberKit") != nil)
16+
if !isPhoneNumberKitFrameworkLoaded {
17+
throw TextInputKitError.missingFramework(frameworkName: "PhoneNumberKit")
18+
}
19+
}
20+
21+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//
2+
// PhoneNumberTextInputError.swift
3+
// TextInputKit
4+
//
5+
// Created by Artem Starosvetskiy on 13/01/2017.
6+
// Copyright © 2017 Artem Starosvetskiy. All rights reserved.
7+
//
8+
9+
import PhoneNumberKit
10+
11+
public enum PhoneNumberTextInputError : Error {
12+
13+
public enum InvalidInputDetails {
14+
15+
case unknown
16+
17+
case invalidCountryCode
18+
19+
case tooShort
20+
21+
case tooLong
22+
23+
case nationalNumbersAreNotSupported
24+
25+
case numberExtensionAreNotSupported
26+
27+
}
28+
29+
case invalidInput(InvalidInputDetails)
30+
31+
}
32+
33+
extension PhoneNumberTextInputError : CustomStringConvertible {
34+
35+
public var description: String {
36+
switch self {
37+
case .invalidInput(let details):
38+
var description = "The string isn't representing a valid phone number."
39+
40+
switch details {
41+
case .invalidCountryCode:
42+
description += " The country code is invalid."
43+
44+
case .tooShort:
45+
description += " The number is too short."
46+
47+
case .tooLong:
48+
description += " The number is too long."
49+
50+
case .nationalNumbersAreNotSupported:
51+
description += " Only international numbers (starting with \"+\") are supported."
52+
53+
case .numberExtensionAreNotSupported:
54+
description += " Number extensions aren't supported."
55+
56+
default:
57+
break
58+
}
59+
60+
return description
61+
}
62+
}
63+
64+
}
65+
66+
extension PhoneNumberTextInputError {
67+
68+
static func from(_ error: PhoneNumberError) -> PhoneNumberTextInputError {
69+
switch error {
70+
case .invalidCountryCode:
71+
return .invalidInput(.invalidCountryCode)
72+
73+
case .tooShort:
74+
return .invalidInput(.tooShort)
75+
76+
case .tooLong:
77+
return .invalidInput(.tooLong)
78+
79+
default:
80+
return .invalidInput(.unknown)
81+
}
82+
}
83+
84+
}

TextInputKit/TextInputKit/Code/SpecializedTextInput/PhoneNumber/PhoneNumberTextInputFormatter.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,17 @@ final class PhoneNumberTextInputFormatter : TextInputFormatter {
6767

6868
private extension PhoneNumberTextInputFormatter {
6969

70+
enum ValidationError : Error {
71+
72+
case invalidPhoneNumber
73+
74+
}
75+
7076
func formattedString(_ string: String) throws -> String {
7177
let formattedString = partialFormatter.formatPartial(string)
7278

7379
if !Utils.isValidPhoneNumber(formattedString) {
74-
// TODO: Throw the proper error.
75-
throw TextInputKitError.unknown
80+
throw ValidationError.invalidPhoneNumber
7681
}
7782

7883
return formattedString

TextInputKit/TextInputKit/Code/SpecializedTextInput/PhoneNumber/TextInputFormats+PhoneNumber.swift

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,12 @@
66
// Copyright © 2016 Artem Starosvetskiy. All rights reserved.
77
//
88

9-
import Foundation
109
import PhoneNumberKit
1110

1211
public extension TextInputFormats {
1312

1413
static func phoneNumber(_ options: PhoneNumberTextInputOptions = .options()) throws -> TextInputFormat<PhoneNumber> {
15-
let isPhoneNumberKitFrameworkLoaded = (NSClassFromString("PhoneNumberKit.PhoneNumberKit") != nil)
16-
if !isPhoneNumberKitFrameworkLoaded {
17-
throw TextInputKitError.missingFramework("PhoneNumberKit")
18-
}
14+
try PhoneNumberKit.checkThatFrameworkIsLoaded()
1915

2016
let cachedPhoneNumberKit = PhoneNumberKit.cached
2117

@@ -24,9 +20,8 @@ public extension TextInputFormats {
2420
do {
2521
let phoneNumberKit = cachedPhoneNumberKit.instance
2622
return PhoneNumber(try phoneNumberKit.parse(text))
27-
} catch {
28-
// TODO: Throw the proper error.
29-
throw TextInputKitError.unknown
23+
} catch let error as PhoneNumberError {
24+
throw PhoneNumberTextInputError.from(error)
3025
}
3126
},
3227
reverse: { phoneNumber -> String in

TextInputKit/TextInputKit/Code/TextInputKit.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,6 @@
88

99
import Foundation
1010

11-
public enum TextInputKitError : Error {
12-
13-
case unknown
14-
15-
case missingFramework(String)
16-
17-
}
18-
1911
func abstractMethod() -> Never {
2012
fatalError("Abstract method.")
2113
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// TextInputKitError.swift
3+
// TextInputKit
4+
//
5+
// Created by Artem Starosvetskiy on 13/01/2017.
6+
// Copyright © 2017 Artem Starosvetskiy. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
public enum TextInputKitError : Error {
12+
13+
case unknown
14+
15+
case missingFramework(frameworkName: String)
16+
17+
}
18+
19+
extension TextInputKitError : CustomStringConvertible {
20+
21+
public var description: String {
22+
switch self {
23+
case .unknown:
24+
return "Unknown error."
25+
26+
case .missingFramework(let frameworkName):
27+
return "Framework \"\(frameworkName)\" isn't loaded."
28+
}
29+
}
30+
31+
}

0 commit comments

Comments
 (0)