network/hpds_err/errors.go

145 lines
3.3 KiB
Go
Raw Permalink Normal View History

2022-10-11 17:36:09 +08:00
package hpds_err
import (
"fmt"
2023-04-05 16:15:59 +08:00
quic "github.com/quic-go/quic-go"
2022-10-11 17:36:09 +08:00
)
// HpdsError hpds error
2023-04-05 16:15:59 +08:00
type HpdsError interface {
error
// ErrorCode getter method
ErrorCode() ErrorCode
}
type hpdsError struct {
2022-10-11 17:36:09 +08:00
errorCode ErrorCode
err error
}
// New create hpds error
2023-04-05 16:15:59 +08:00
func New(code ErrorCode, err error) HpdsError {
return &hpdsError{
2022-10-11 17:36:09 +08:00
errorCode: code,
err: err,
}
}
2023-04-05 16:15:59 +08:00
func (e *hpdsError) Error() string {
2022-10-11 17:36:09 +08:00
return fmt.Sprintf("%s error: message=%s", e.errorCode, e.err.Error())
}
2023-04-05 16:15:59 +08:00
// ErrorCode getter method
func (e *hpdsError) ErrorCode() ErrorCode {
return e.errorCode
}
2022-10-11 17:36:09 +08:00
// ErrorCode error code
type ErrorCode uint64
const (
// ErrorCodeClientAbort client abort
2023-04-05 16:15:59 +08:00
ErrorCodeClientAbort ErrorCode = 0xC7
2022-10-11 17:36:09 +08:00
// ErrorCodeUnknown unknown error
ErrorCodeUnknown ErrorCode = 0xC0
// ErrorCodeClosed net closed
ErrorCodeClosed ErrorCode = 0xC1
// ErrorCodeBeforeHandler before handler
ErrorCodeBeforeHandler ErrorCode = 0xC2
// ErrorCodeMainHandler main handler
ErrorCodeMainHandler ErrorCode = 0xC3
// ErrorCodeAfterHandler after handler
ErrorCodeAfterHandler ErrorCode = 0xC4
// ErrorCodeHandshake handshake frame
ErrorCodeHandshake ErrorCode = 0xC5
// ErrorCodeRejected server rejected
ErrorCodeRejected ErrorCode = 0xCC
// ErrorCodeGoaway goaway frame
ErrorCodeGoaway ErrorCode = 0xCF
// ErrorCodeData data frame
ErrorCodeData ErrorCode = 0xCE
// ErrorCodeUnknownClient unknown client error
ErrorCodeUnknownClient ErrorCode = 0xCD
// ErrorCodeDuplicateName unknown client error
ErrorCodeDuplicateName ErrorCode = 0xC6
2023-04-05 16:15:59 +08:00
ErrorCodeStartHandler ErrorCode = 0xC8
2022-10-11 17:36:09 +08:00
)
func (e ErrorCode) String() string {
switch e {
case ErrorCodeClientAbort:
return "ClientAbort"
case ErrorCodeUnknown:
return "UnknownError"
case ErrorCodeClosed:
return "NetClosed"
case ErrorCodeBeforeHandler:
return "BeforeHandler"
case ErrorCodeMainHandler:
return "MainHandler"
case ErrorCodeAfterHandler:
return "AfterHandler"
case ErrorCodeHandshake:
return "Handshake"
case ErrorCodeRejected:
return "Rejected"
case ErrorCodeGoaway:
return "Goaway"
case ErrorCodeData:
return "DataFrame"
case ErrorCodeUnknownClient:
return "UnknownClient"
case ErrorCodeDuplicateName:
return "DuplicateName"
2023-04-05 16:15:59 +08:00
case ErrorCodeStartHandler:
return "StartHandler"
2022-10-11 17:36:09 +08:00
default:
return "XXX"
}
}
// Is parse quic ApplicationErrorCode to hpds ErrorCode
2023-04-05 16:15:59 +08:00
func Is(he quic.ApplicationErrorCode, err ErrorCode) bool {
return uint64(he) == uint64(err)
2022-10-11 17:36:09 +08:00
}
// Parse parse quic ApplicationErrorCode
2023-04-05 16:15:59 +08:00
func Parse(err quic.ApplicationErrorCode) ErrorCode {
return ErrorCode(err)
2022-10-11 17:36:09 +08:00
}
// To convert hpds ErrorCode to quic ApplicationErrorCode
2023-04-05 16:15:59 +08:00
func (e ErrorCode) To() quic.ApplicationErrorCode {
return quic.ApplicationErrorCode(e)
2022-10-11 17:36:09 +08:00
}
// DuplicateNameError duplicate name(sfn)
type DuplicateNameError struct {
connId string
err error
}
// NewDuplicateNameError create a duplicate name error
func NewDuplicateNameError(connId string, err error) DuplicateNameError {
return DuplicateNameError{
connId: connId,
err: err,
}
}
// Error raw error
func (e DuplicateNameError) Error() string {
return e.err.Error()
}
2023-04-05 16:15:59 +08:00
// ErrorCode getter method
func (e DuplicateNameError) ErrorCode() ErrorCode {
return ErrorCodeDuplicateName
}
2022-10-11 17:36:09 +08:00
// ConnId duplicate connection ID
func (e DuplicateNameError) ConnId() string {
return e.connId
}