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

Rule Request: Untyped error in catch statement #2045

Closed
dirtydanee opened this Issue Feb 11, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@dirtydanee

dirtydanee commented Feb 11, 2018

New Issue Checklist

Rule Request

Description

When an error is being thrown from a do - catch, the type of the error being thrown is unknown. We might would like to handle some specific types of errors in a specific way, but most of the times the error is being logged to the console or reported to some third party error tracking system. In order to do that, we do not need to unwrap the error without the type itself, it is available for us in the catch statement.

The official Apple documentation on error handling is without a single example of unwrapping an error without specifying the type.

What would trigger?

enum Error: Swift.Error {
    case invalidOperation
    case failedOperation
}

func foo() throws {
    throw Error.invalidOperation
}

do {
    try foo()
} catch let error {
    print("Action failed. Error: \(error.localizedDescription)")
}

What would not trigger?

enum Error: Swift.Error {
    case invalidOperation
    case failedOperation
}

func foo() throws {
    throw Error.invalidOperation
}

Example 1:

do {
    try foo()
} catch Error.invalidOperation {
    print("Action failed with invalid operation")
} catch Error.failedOperation {
    print("Action failed with failed operation")
} catch {
    print("Action failed for unkown reason. Error: \(error.localizedDescription)")
}

Example 2:

do {
    try foo()
}  catch let error as Error {
   print("Action failed with invalid operation")
} catch {
    print("Action failed for unknown reason. Error: \(error.localizedDescription)")
}

Example 3:

do {
    try foo()
} catch Error.invalidOperation {
   print("Action failed with invalid operation")
}  catch let error as Error {
   print("Action failed with custom error")
} catch {
    print("Action failed for unknown reason. Error: \(error.localizedDescription)")
}

Configurable?

The rule should not be configurable.

Opt-in or enabled?

This rule could be a personal preference or coding style from various individuals, so it could be kept as opt-in.

@marcelofabri

This comment has been minimized.

Collaborator

marcelofabri commented Mar 17, 2018

Implemented in #2045 and #2101

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment