container_network/crawler/health/socket_error.go
2018-07-05 12:06:41 +09:00

59 lines
1.2 KiB
Go

package health
import (
"encoding/json"
"fmt"
)
type SocketErrorEnum int
const (
SocketErrorEnumUNKNOWN SocketErrorEnum = iota + 1
SocketErrorEnumHOSTUNREACH
SocketErrorEnumCONNREFUSED
SocketErrorEnumTIMOUT
)
var (
socketErrorEnumID = map[SocketErrorEnum]string{
SocketErrorEnumUNKNOWN: "UNKNOWN",
SocketErrorEnumHOSTUNREACH: "HOSTUNREACH",
SocketErrorEnumCONNREFUSED: "CONNREFUSED",
SocketErrorEnumTIMOUT: "TIMEOUT",
}
socketErrorEnumKey = map[string]SocketErrorEnum{
"UNKNOWN": SocketErrorEnumUNKNOWN,
"HOSTUNREACH": SocketErrorEnumHOSTUNREACH,
"CONNREFUSED": SocketErrorEnumCONNREFUSED,
"TIMEOUT": SocketErrorEnumTIMOUT,
}
)
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
}