container_network/crawler/health/socket_error.go

59 lines
1.2 KiB
Go
Raw Normal View History

2018-07-04 09:33:58 +00:00
package health
import (
"encoding/json"
"fmt"
)
type SocketErrorEnum int
const (
SocketErrorEnumUNKNOWN SocketErrorEnum = iota + 1
SocketErrorEnumHOSTUNREACH
SocketErrorEnumCONNREFUSED
2018-07-05 03:06:41 +00:00
SocketErrorEnumTIMOUT
2018-07-04 09:33:58 +00:00
)
var (
socketErrorEnumID = map[SocketErrorEnum]string{
SocketErrorEnumUNKNOWN: "UNKNOWN",
SocketErrorEnumHOSTUNREACH: "HOSTUNREACH",
SocketErrorEnumCONNREFUSED: "CONNREFUSED",
2018-07-05 03:06:41 +00:00
SocketErrorEnumTIMOUT: "TIMEOUT",
2018-07-04 09:33:58 +00:00
}
socketErrorEnumKey = map[string]SocketErrorEnum{
"UNKNOWN": SocketErrorEnumUNKNOWN,
"HOSTUNREACH": SocketErrorEnumHOSTUNREACH,
"CONNREFUSED": SocketErrorEnumCONNREFUSED,
2018-07-05 03:06:41 +00:00
"TIMEOUT": SocketErrorEnumTIMOUT,
2018-07-04 09:33:58 +00:00
}
)
func (e SocketErrorEnum) String() string {
return socketErrorEnumID[e]
}
func (e SocketErrorEnum) MarshalJSON() ([]byte, error) {
value, ok := socketErrorEnumID[e]
if !ok {
return nil, fmt.Errorf("Invalid EnumType[%s] value", e)
}
return json.Marshal(value)
}
func (e SocketErrorEnum) UnmarshalJSON(b []byte) error {
var s string
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
value, ok := socketErrorEnumKey[s]
if !ok {
return fmt.Errorf("Invalid EnumType[%s] value", s)
}
e = value
return nil
}