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

Enabling pattern matching on JWTErrors #45

Merged
merged 1 commit into from
Mar 18, 2019

Conversation

wcrestfield
Copy link
Contributor

As is JWTErrors are can not be pattern matched with generic errors. This keeps us from catching specific JWTErrors.
Currently we must:

do {
	let jwt = try JWT<MyClaims>(jwtString: token,  verifier: verifier)
} catch let error as JWTError {
	switch error {
	case .invalidJWTString:
		handleInvalidJWT()
	case .failedVerification:
		handleFailedVerification()
	}
}

With this change, it allows the following:

do {
	let jwt = try JWT<MyClaims>(jwtString: token,  verifier: verifier)
} catch JWTError.invalidJWTString {
	handleInvalidJWT()
} catch JWTError.failedVerification {
	handleFailedVerification()
}

@CLAassistant
Copy link

CLAassistant commented Mar 14, 2019

CLA assistant check
All committers have signed the CLA.

@ianpartridge
Copy link
Contributor

Thanks for your PR. This looks like a good idea to me, we'll get it reviewed ASAP. It would be nice to add a test or two as well.

@Andrew-Lees11
Copy link
Contributor

This looks good to me as well. Just a quick note that in your new code example it would have to be:

do {
	let jwt = try JWT<MyClaims>(jwtString: token,  verifier: verifier)
} catch JWTError.invalidJWTString {
	handleInvalidJWT()
} catch JWTError.failedVerification {
	handleFailedVerification()
} catch {
       handleOtherError()
}

So that the catch is exhaustive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants