-
Notifications
You must be signed in to change notification settings - Fork 27
/
error.go
40 lines (34 loc) · 1.27 KB
/
error.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package balancer
import (
"errors"
"net/http"
)
// Known errors.
var (
ErrNotSupportedMethod = errors.New("not supported LB method")
ErrNotSupportedProto = errors.New("not supported protocol")
ErrVirtualServerNameEmpty = errors.New("virtual server name is not specified")
ErrVirtualServerAddressEmpty = errors.New("virtual server address is not specified")
ErrVirtualServerNameExisted = errors.New("virtual server name existed")
ErrVirtualServerAddressExisted = errors.New("virtual server address existed")
ErrVirtualServerNotFound = errors.New("virtual server not found")
)
type balancerError struct {
StatusCode int
ErrMsg string
}
func (e *balancerError) Error() string {
return e.ErrMsg
}
// Known balancerError.
var (
ErrBadRequest = &balancerError{http.StatusBadRequest, "Reqeust Error"}
ErrHostNotMatch = &balancerError{http.StatusBadRequest, "Host Not Match"}
ErrPeerNotFound = &balancerError{http.StatusBadGateway, "Peer Not Found"}
ErrInternalBalancer = &balancerError{http.StatusInternalServerError, "Balancer Internal Error"}
)
// WriteError writes balancerError to http.ResponseWriter.
func WriteError(w http.ResponseWriter, err *balancerError) {
w.WriteHeader(err.StatusCode)
w.Write([]byte(err.ErrMsg))
}