Skip to content

Commit e5178a9

Browse files
authored
Remove special casing for CustomNSError (#276)
The error code in CustomNSError is actually _not_ appropriate to use as the command's exit code.
1 parent 5bfb39a commit e5178a9

File tree

2 files changed

+1
-60
lines changed

2 files changed

+1
-60
lines changed

Sources/ArgumentParser/Usage/MessageInfo.swift

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,6 @@ enum MessageInfo {
6262
self.init(error: CommandError(commandStack: [type.asCommand], parserError: e), type: type)
6363
return
6464

65-
case let e as CustomNSError:
66-
// Send CustomNSError back through the CommandError path
67-
self.init(
68-
error: CommandError(
69-
commandStack: [type.asCommand],
70-
parserError: .userValidationError(e)
71-
),
72-
type: type
73-
)
74-
return
75-
7665
default:
7766
commandStack = [type.asCommand]
7867
// if the error wasn't one of our two Error types, wrap it as a userValidationError
@@ -106,8 +95,6 @@ enum MessageInfo {
10695
}
10796
case let error as ExitCode:
10897
self = .other(message: "", exitCode: error.rawValue)
109-
case let error as CustomNSError:
110-
self = .other(message: error.localizedDescription, exitCode: Int32(error.errorCode))
11198
case let error as LocalizedError where error.errorDescription != nil:
11299
self = .other(message: error.errorDescription!, exitCode: EXIT_FAILURE)
113100
default:

Tests/ArgumentParserUnitTests/ExitCodeTests.swift

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -77,55 +77,9 @@ extension ExitCodeTests {
7777
}
7878
}
7979

80-
// MARK: - CustomNSError tests
80+
// MARK: - NSError tests
8181

8282
extension ExitCodeTests {
83-
enum MyCustomNSError: CustomNSError {
84-
case myFirstCase
85-
case mySecondCase
86-
87-
var errorCode: Int {
88-
switch self {
89-
case .myFirstCase:
90-
return 101
91-
case .mySecondCase:
92-
return 102
93-
}
94-
}
95-
96-
var errorUserInfo: [String : Any] {
97-
switch self {
98-
case .myFirstCase:
99-
return [NSLocalizedDescriptionKey: "My first case localized description"]
100-
case .mySecondCase:
101-
return [:]
102-
}
103-
}
104-
}
105-
106-
struct CheckFirstCustomNSErrorCommand: ParsableCommand {
107-
108-
@Option
109-
var errorCase: Int
110-
111-
func run() throws {
112-
switch errorCase {
113-
case 101:
114-
throw MyCustomNSError.myFirstCase
115-
default:
116-
throw MyCustomNSError.mySecondCase
117-
}
118-
}
119-
}
120-
121-
func testCustomErrorCodeForTheFirstCase() {
122-
XCTAssertEqual(CheckFirstCustomNSErrorCommand.exitCode(for: MyCustomNSError.myFirstCase), ExitCode(rawValue: 101))
123-
}
124-
125-
func testCustomErrorCodeForTheSecondCase() {
126-
XCTAssertEqual(CheckFirstCustomNSErrorCommand.exitCode(for: MyCustomNSError.mySecondCase), ExitCode(rawValue: 102))
127-
}
128-
12983
func testNSErrorIsHandled() {
13084
struct NSErrorCommand: ParsableCommand {
13185
static let fileNotFoundNSError = NSError(domain: "", code: 1, userInfo: [NSLocalizedDescriptionKey: "The file “foo/bar” couldn’t be opened because there is no such file"])

0 commit comments

Comments
 (0)