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

Rule Request: Untyped error in catch statement #2045

Closed
2 tasks done
dirtydanee opened this issue Feb 11, 2018 · 1 comment
Closed
2 tasks done

Rule Request: Untyped error in catch statement #2045

dirtydanee opened this issue Feb 11, 2018 · 1 comment
Labels
rule-request Requests for a new rules.

Comments

@dirtydanee
Copy link
Contributor

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
Copy link
Collaborator

Implemented in #2045 and #2101

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule-request Requests for a new rules.
Projects
None yet
Development

No branches or pull requests

2 participants