forked from quic-go/quic-go
move error conversion from session to qerr.ToQuicError
This commit is contained in:
@@ -2,6 +2,8 @@ package qerr
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/lucas-clemente/quic-go/utils"
|
||||
)
|
||||
|
||||
// ErrorCode can be used as a normal error without reason.
|
||||
@@ -28,3 +30,16 @@ func Error(errorCode ErrorCode, errorMessage string) *QuicError {
|
||||
func (e *QuicError) Error() string {
|
||||
return fmt.Sprintf("%s: %s", e.ErrorCode.String(), e.ErrorMessage)
|
||||
}
|
||||
|
||||
// ToQuicError converts an arbitrary error to a QuicError. It leaves QuicErrors
|
||||
// unchanged, and properly handles `ErrorCode`s.
|
||||
func ToQuicError(err error) *QuicError {
|
||||
switch e := err.(type) {
|
||||
case *QuicError:
|
||||
return e
|
||||
case ErrorCode:
|
||||
return Error(e, "")
|
||||
}
|
||||
utils.Errorf("BUG: Unknown error encountered: %#v", err)
|
||||
return Error(InternalError, "")
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package qerr_test
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/lucas-clemente/quic-go/qerr"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
@@ -8,8 +10,33 @@ import (
|
||||
)
|
||||
|
||||
var _ = Describe("Quic error", func() {
|
||||
It("has a string representation", func() {
|
||||
err := qerr.Error(qerr.InternalError, "foobar")
|
||||
Expect(err.Error()).To(Equal("InternalError: foobar"))
|
||||
Context("QuicError", func() {
|
||||
It("has a string representation", func() {
|
||||
err := qerr.Error(qerr.DecryptionFailure, "foobar")
|
||||
Expect(err.Error()).To(Equal("DecryptionFailure: foobar"))
|
||||
})
|
||||
})
|
||||
|
||||
Context("ErrorCode", func() {
|
||||
It("works as error", func() {
|
||||
var err error = qerr.DecryptionFailure
|
||||
Expect(err).To(MatchError("DecryptionFailure"))
|
||||
})
|
||||
})
|
||||
|
||||
Context("ToQuicError", func() {
|
||||
It("leaves QuicError unchanged", func() {
|
||||
err := qerr.Error(qerr.DecryptionFailure, "foo")
|
||||
Expect(qerr.ToQuicError(err)).To(Equal(err))
|
||||
})
|
||||
|
||||
It("wraps ErrorCode properly", func() {
|
||||
var err error = qerr.DecryptionFailure
|
||||
Expect(qerr.ToQuicError(err)).To(Equal(qerr.Error(qerr.DecryptionFailure, "")))
|
||||
})
|
||||
|
||||
It("changes default errors to InternalError", func() {
|
||||
Expect(qerr.ToQuicError(io.EOF)).To(Equal(qerr.Error(qerr.InternalError, "")))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user