Skip to content

Enabling pattern matching on JWTErrors#45

Merged
Andrew-Lees11 merged 1 commit intoKitura:masterfrom
wcrestfield:master
Mar 18, 2019
Merged

Enabling pattern matching on JWTErrors#45
Andrew-Lees11 merged 1 commit intoKitura:masterfrom
wcrestfield:master

Conversation

@wcrestfield
Copy link
Copy Markdown
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
Copy Markdown

CLAassistant commented Mar 14, 2019

CLA assistant check
All committers have signed the CLA.

@ianpartridge
Copy link
Copy Markdown
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
Copy Markdown
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