fix: replace panics with proper error handling and add comprehensive tests #62
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR addresses critical issues identified in the codebase evaluation:
Changes
Error Handling Improvements
panic("unsupported signingalgorithm for rsa key")with proper error returnpanic("unreachable")with proper error return for unsupported key typesTest Coverage Improvements
New tests added:
TestConfigWithContext()- tests the WithContext() method (was 0% coverage)TestSigningMethodWithInvalidKeyConfig()- tests error handling for invalid key configsTestSigningMethodFallbackToStandardJWT()- tests backward compatibility with standard JWTTestVerifyWithInvalidSignature()- tests signature tampering detectionTestVerifyWithNonExistentKey()- tests error handling for missing keysTestAlgMethod()- tests the Alg() method for all signing methodsComprehensive mockkms test suite:
TestNewMockKMS()- constructor testTestGenerateKey()- key generation for all types + error casesTestSignECDSA()- ECDSA signing and verification (ES256, ES384, ES512)TestSignRSAPKCS1()- RSA PKCS1 signing and verification (RS256, RS384, RS512)TestSignRSAPSS()- RSA PSS signing and verification (PS256, PS384, PS512)TestSignWithUnsupportedAlgorithm()- error handling for algorithm mismatchesTestSignWithUnsupportedMessageType()- error handling for unsupported message typesTestVerifyWithInvalidSignature()- signature validation edge casesTestGetPublicKey()- public key retrieval for all key typesTestGetPublicKeyNonExistent()- error handling for missing keysCoverage Results
Testing
All tests pass:
go test -v ./...Impact
🤖 Generated with Claude Code