error hint

This commit is contained in:
Sense T 2022-09-09 11:53:46 +00:00
parent ed15f041ce
commit ef798c1ac7
3 changed files with 27 additions and 4 deletions

11
errors.go Normal file
View File

@ -0,0 +1,11 @@
package tunnel
import "fmt"
type WrongDataFrameTypeError struct {
ShouldBe Type
}
func (w WrongDataFrameTypeError) Error() string {
return fmt.Sprintf("wrong dataframe type, should be %s(%d)", w.ShouldBe, w.ShouldBe)
}

View File

@ -1,7 +1,6 @@
package tunnel
import (
"errors"
"io"
)
@ -84,8 +83,7 @@ func (m *Manager) Connect() (*Connection, error) {
df = <-connection.RX
if df.Type != TypeConnected {
return nil, errors.New("failed to connect")
return nil, WrongDataFrameTypeError{ShouldBe: TypeConnected}
}
return connection, nil
}
@ -93,7 +91,7 @@ func (m *Manager) Connect() (*Connection, error) {
func (m *Manager) Accept() (*Connection, error) {
df := <-m.accept
if df.Type != TypeRequest {
return nil, errors.New("failed to accept")
return nil, WrongDataFrameTypeError{ShouldBe: TypeRequest}
}
connection := &Connection{
ID: df.ID,

View File

@ -9,3 +9,17 @@ func NewID() ID {
}
type Type uint8
func (t Type) String() string {
switch t {
case TypeNormal:
return "normal"
case TypeRequest:
return "request"
case TypeConnected:
return "connected"
case TypeClosed:
return "closed"
}
return "invalid"
}