forked from quic-go/quic-go
use the string representation of the TLS alert for crypto errors
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
package qerr
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/marten-seemann/qtls"
|
||||
)
|
||||
|
||||
// ErrorCode can be used as a normal error without reason.
|
||||
type ErrorCode uint16
|
||||
@@ -21,7 +25,14 @@ const (
|
||||
InvalidMigration ErrorCode = 0xc
|
||||
)
|
||||
|
||||
func (e ErrorCode) isCryptoError() bool {
|
||||
return e >= 0x100 && e < 0x200
|
||||
}
|
||||
|
||||
func (e ErrorCode) Error() string {
|
||||
if e.isCryptoError() {
|
||||
return fmt.Sprintf("%s: %s", e.String(), qtls.Alert(e-0x100).Error())
|
||||
}
|
||||
return e.String()
|
||||
}
|
||||
|
||||
@@ -52,8 +63,8 @@ func (e ErrorCode) String() string {
|
||||
case InvalidMigration:
|
||||
return "INVALID_MIGRATION"
|
||||
default:
|
||||
if e >= 0x100 && e < 0x200 {
|
||||
return fmt.Sprintf("CRYPTO_ERROR %d", e-0x100)
|
||||
if e.isCryptoError() {
|
||||
return "CRYPTO_ERROR"
|
||||
}
|
||||
return fmt.Sprintf("unknown error code: %#x", uint16(e))
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ func CryptoError(tlsAlert uint8) *QuicError {
|
||||
|
||||
func (e *QuicError) Error() string {
|
||||
if len(e.ErrorMessage) == 0 {
|
||||
return e.ErrorCode.String()
|
||||
return e.ErrorCode.Error()
|
||||
}
|
||||
return fmt.Sprintf("%s: %s", e.ErrorCode.String(), e.ErrorMessage)
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ var _ = Describe("QUIC Transport Errors", func() {
|
||||
|
||||
It("has a string representation for crypto errors", func() {
|
||||
err := CryptoError(42)
|
||||
Expect(err.Error()).To(Equal("CRYPTO_ERROR 42"))
|
||||
Expect(err.Error()).To(Equal("CRYPTO_ERROR: tls: bad certificate"))
|
||||
})
|
||||
|
||||
Context("ErrorCode", func() {
|
||||
@@ -38,7 +38,7 @@ var _ = Describe("QUIC Transport Errors", func() {
|
||||
|
||||
It("recognizes crypto errors", func() {
|
||||
err := ErrorCode(0x100 + 42)
|
||||
Expect(err.Error()).To(Equal("CRYPTO_ERROR 42"))
|
||||
Expect(err.Error()).To(Equal("CRYPTO_ERROR: tls: bad certificate"))
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user