use the string representation of the TLS alert for crypto errors

This commit is contained in:
Marten Seemann
2019-03-07 11:28:30 +09:00
parent e5303df419
commit f9a21275a4
5 changed files with 20 additions and 9 deletions

View File

@@ -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))
}

View File

@@ -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)
}

View File

@@ -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"))
})
})