Skip to content

Commit

Permalink
fix: allow tree-shaking of errors
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Oct 13, 2021
1 parent 60d370d commit 0824301
Showing 1 changed file with 61 additions and 30 deletions.
91 changes: 61 additions & 30 deletions src/util/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ export class JOSEError extends Error {
/**
* A unique error code for the particular error subclass.
*/
static code = 'ERR_JOSE_GENERIC'
static get code(): string {
return 'ERR_JOSE_GENERIC'
}

/**
* A unique error code for the particular error subclass.
*/
code = JOSEError.code
code: string = 'ERR_JOSE_GENERIC'

constructor(message?: string) {
super(message)
Expand All @@ -24,9 +26,11 @@ export class JOSEError extends Error {
* An error subclass thrown when a JWT Claim Set member validation fails.
*/
export class JWTClaimValidationFailed extends JOSEError {
static code = 'ERR_JWT_CLAIM_VALIDATION_FAILED'
static get code(): 'ERR_JWT_CLAIM_VALIDATION_FAILED' {
return 'ERR_JWT_CLAIM_VALIDATION_FAILED'
}

code = JWTClaimValidationFailed.code
code = 'ERR_JWT_CLAIM_VALIDATION_FAILED'

/**
* The Claim for which the validation failed.
Expand All @@ -49,28 +53,34 @@ export class JWTClaimValidationFailed extends JOSEError {
* An error subclass thrown when a JOSE Algorithm is not allowed per developer preference.
*/
export class JOSEAlgNotAllowed extends JOSEError {
static code = 'ERR_JOSE_ALG_NOT_ALLOWED'
static get code(): 'ERR_JOSE_ALG_NOT_ALLOWED' {
return 'ERR_JOSE_ALG_NOT_ALLOWED'
}

code = JOSEAlgNotAllowed.code
code = 'ERR_JOSE_ALG_NOT_ALLOWED'
}

/**
* An error subclass thrown when a particular feature or algorithm is not supported by this
* implementation or JOSE in general.
*/
export class JOSENotSupported extends JOSEError {
static code = 'ERR_JOSE_NOT_SUPPORTED'
static get code(): 'ERR_JOSE_NOT_SUPPORTED' {
return 'ERR_JOSE_NOT_SUPPORTED'
}

code = JOSENotSupported.code
code = 'ERR_JOSE_NOT_SUPPORTED'
}

/**
* An error subclass thrown when a JWE ciphertext decryption fails.
*/
export class JWEDecryptionFailed extends JOSEError {
static code = 'ERR_JWE_DECRYPTION_FAILED'
static get code(): 'ERR_JWE_DECRYPTION_FAILED' {
return 'ERR_JWE_DECRYPTION_FAILED'
}

code = JWEDecryptionFailed.code
code = 'ERR_JWE_DECRYPTION_FAILED'

message = 'decryption operation failed'
}
Expand All @@ -79,54 +89,66 @@ export class JWEDecryptionFailed extends JOSEError {
* An error subclass thrown when a JWE is invalid.
*/
export class JWEInvalid extends JOSEError {
static code = 'ERR_JWE_INVALID'
static get code(): 'ERR_JWE_INVALID' {
return 'ERR_JWE_INVALID'
}

code = JWEInvalid.code
code = 'ERR_JWE_INVALID'
}

/**
* An error subclass thrown when a JWS is invalid.
*/
export class JWSInvalid extends JOSEError {
static code = 'ERR_JWS_INVALID'
static get code(): 'ERR_JWS_INVALID' {
return 'ERR_JWS_INVALID'
}

code = JWSInvalid.code
code = 'ERR_JWS_INVALID'
}

/**
* An error subclass thrown when a JWT is invalid.
*/
export class JWTInvalid extends JOSEError {
static code = 'ERR_JWT_INVALID'
static get code(): 'ERR_JWT_INVALID' {
return 'ERR_JWT_INVALID'
}

code = JWTInvalid.code
code = 'ERR_JWT_INVALID'
}

/**
* An error subclass thrown when a JWK is invalid.
*/
export class JWKInvalid extends JOSEError {
static code = 'ERR_JWK_INVALID'
static get code(): 'ERR_JWK_INVALID' {
return 'ERR_JWK_INVALID'
}

code = JWKInvalid.code
code = 'ERR_JWK_INVALID'
}

/**
* An error subclass thrown when a JWKS is invalid.
*/
export class JWKSInvalid extends JOSEError {
static code = 'ERR_JWKS_INVALID'
static get code(): 'ERR_JWKS_INVALID' {
return 'ERR_JWKS_INVALID'
}

code = JWKSInvalid.code
code = 'ERR_JWKS_INVALID'
}

/**
* An error subclass thrown when no keys match from a JWKS.
*/
export class JWKSNoMatchingKey extends JOSEError {
static code = 'ERR_JWKS_NO_MATCHING_KEY'
static get code(): 'ERR_JWKS_NO_MATCHING_KEY' {
return 'ERR_JWKS_NO_MATCHING_KEY'
}

code = JWKSNoMatchingKey.code
code = 'ERR_JWKS_NO_MATCHING_KEY'

message = 'no applicable key found in the JSON Web Key Set'
}
Expand All @@ -135,9 +157,11 @@ export class JWKSNoMatchingKey extends JOSEError {
* An error subclass thrown when multiple keys match from a JWKS.
*/
export class JWKSMultipleMatchingKeys extends JOSEError {
static code = 'ERR_JWKS_MULTIPLE_MATCHING_KEYS'
static get code(): 'ERR_JWKS_MULTIPLE_MATCHING_KEYS' {
return 'ERR_JWKS_MULTIPLE_MATCHING_KEYS'
}

code = JWKSMultipleMatchingKeys.code
code = 'ERR_JWKS_MULTIPLE_MATCHING_KEYS'

message = 'multiple matching keys found in the JSON Web Key Set'
}
Expand All @@ -146,9 +170,11 @@ export class JWKSMultipleMatchingKeys extends JOSEError {
* Timeout was reached when retrieving the JWKS response.
*/
export class JWKSTimeout extends JOSEError {
static code = 'ERR_JWKS_TIMEOUT'
static get code(): 'ERR_JWKS_TIMEOUT' {
return 'ERR_JWKS_TIMEOUT'
}

code = JWKSTimeout.code
code = 'ERR_JWKS_TIMEOUT'

message = 'request timed out'
}
Expand All @@ -157,18 +183,23 @@ export class JWKSTimeout extends JOSEError {
* An error subclass thrown when JWS signature verification fails.
*/
export class JWSSignatureVerificationFailed extends JOSEError {
static code = 'ERR_JWS_SIGNATURE_VERIFICATION_FAILED'
static get code(): 'ERR_JWS_SIGNATURE_VERIFICATION_FAILED' {
return 'ERR_JWS_SIGNATURE_VERIFICATION_FAILED'
}

code = JWSSignatureVerificationFailed.code
code = 'ERR_JWS_SIGNATURE_VERIFICATION_FAILED'

message = 'signature verification failed'
}

/**
* An error subclass thrown when a JWT is expired.
*/
// @ts-expect-error
export class JWTExpired extends JWTClaimValidationFailed {
static code = 'ERR_JWT_EXPIRED'
static get code(): 'ERR_JWT_EXPIRED' {
return 'ERR_JWT_EXPIRED'
}

code = JWTExpired.code
code = 'ERR_JWT_EXPIRED'
}

0 comments on commit 0824301

Please sign in to comment.