implement a string representation for crypto error

This commit is contained in:
Marten Seemann
2019-03-07 09:55:55 +09:00
parent ab47ba1021
commit e5303df419
4 changed files with 22 additions and 5 deletions

View File

@@ -19,7 +19,6 @@ const (
VersionNegotiationError ErrorCode = 0x9
ProtocolViolation ErrorCode = 0xa
InvalidMigration ErrorCode = 0xc
CryptoError ErrorCode = 0x100
)
func (e ErrorCode) Error() string {
@@ -52,9 +51,10 @@ func (e ErrorCode) String() string {
return "PROTOCOL_VIOLATION"
case InvalidMigration:
return "INVALID_MIGRATION"
case CryptoError:
return "CRYPTO_ERROR"
default:
return fmt.Sprintf("unknown error code: %d", e)
if e >= 0x100 && e < 0x200 {
return fmt.Sprintf("CRYPTO_ERROR %d", e-0x100)
}
return fmt.Sprintf("unknown error code: %#x", uint16(e))
}
}

View File

@@ -35,6 +35,6 @@ var _ = Describe("error codes", func() {
})
It("has a string representation for unknown error codes", func() {
Expect(ErrorCode(1337).String()).To(Equal("unknown error code: 1337"))
Expect(ErrorCode(0x1337).String()).To(Equal("unknown error code: 0x1337"))
})
})

View File

@@ -30,6 +30,13 @@ func TimeoutError(errorMessage string) *QuicError {
}
}
// CryptoError create a new QuicError instance for a crypto error
func CryptoError(tlsAlert uint8) *QuicError {
return &QuicError{
ErrorCode: 0x100 + ErrorCode(tlsAlert),
}
}
func (e *QuicError) Error() string {
if len(e.ErrorMessage) == 0 {
return e.ErrorCode.String()

View File

@@ -25,11 +25,21 @@ var _ = Describe("QUIC Transport Errors", func() {
Expect(err.Error()).To(Equal("NO_ERROR: foobar"))
})
It("has a string representation for crypto errors", func() {
err := CryptoError(42)
Expect(err.Error()).To(Equal("CRYPTO_ERROR 42"))
})
Context("ErrorCode", func() {
It("works as error", func() {
var err error = StreamStateError
Expect(err).To(MatchError("STREAM_STATE_ERROR"))
})
It("recognizes crypto errors", func() {
err := ErrorCode(0x100 + 42)
Expect(err.Error()).To(Equal("CRYPTO_ERROR 42"))
})
})
Context("ToQuicError", func() {