Skip to content

Remove special casing for CustomNSError #276

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

Merged
merged 1 commit into from
Feb 18, 2021
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
13 changes: 0 additions & 13 deletions Sources/ArgumentParser/Usage/MessageInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,6 @@ enum MessageInfo {
self.init(error: CommandError(commandStack: [type.asCommand], parserError: e), type: type)
return

case let e as CustomNSError:
// Send CustomNSError back through the CommandError path
self.init(
error: CommandError(
commandStack: [type.asCommand],
parserError: .userValidationError(e)
),
type: type
)
return

default:
commandStack = [type.asCommand]
// if the error wasn't one of our two Error types, wrap it as a userValidationError
Expand Down Expand Up @@ -106,8 +95,6 @@ enum MessageInfo {
}
case let error as ExitCode:
self = .other(message: "", exitCode: error.rawValue)
case let error as CustomNSError:
self = .other(message: error.localizedDescription, exitCode: Int32(error.errorCode))
case let error as LocalizedError where error.errorDescription != nil:
self = .other(message: error.errorDescription!, exitCode: EXIT_FAILURE)
default:
Expand Down
48 changes: 1 addition & 47 deletions Tests/ArgumentParserUnitTests/ExitCodeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,55 +77,9 @@ extension ExitCodeTests {
}
}

// MARK: - CustomNSError tests
// MARK: - NSError tests

extension ExitCodeTests {
enum MyCustomNSError: CustomNSError {
case myFirstCase
case mySecondCase

var errorCode: Int {
switch self {
case .myFirstCase:
return 101
case .mySecondCase:
return 102
}
}

var errorUserInfo: [String : Any] {
switch self {
case .myFirstCase:
return [NSLocalizedDescriptionKey: "My first case localized description"]
case .mySecondCase:
return [:]
}
}
}

struct CheckFirstCustomNSErrorCommand: ParsableCommand {

@Option
var errorCase: Int

func run() throws {
switch errorCase {
case 101:
throw MyCustomNSError.myFirstCase
default:
throw MyCustomNSError.mySecondCase
}
}
}

func testCustomErrorCodeForTheFirstCase() {
XCTAssertEqual(CheckFirstCustomNSErrorCommand.exitCode(for: MyCustomNSError.myFirstCase), ExitCode(rawValue: 101))
}

func testCustomErrorCodeForTheSecondCase() {
XCTAssertEqual(CheckFirstCustomNSErrorCommand.exitCode(for: MyCustomNSError.mySecondCase), ExitCode(rawValue: 102))
}

func testNSErrorIsHandled() {
struct NSErrorCommand: ParsableCommand {
static let fileNotFoundNSError = NSError(domain: "", code: 1, userInfo: [NSLocalizedDescriptionKey: "The file “foo/bar” couldn’t be opened because there is no such file"])
Expand Down