File: error.go

package info (click to toggle)
golang-etcd 0.2.0~rc1%2Bgit20140717-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-backports, jessie-kfreebsd
  • size: 188 kB
  • ctags: 165
  • sloc: makefile: 5
file content (48 lines) | stat: -rw-r--r-- 898 bytes parent folder | download
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
41
42
43
44
45
46
47
48
package etcd

import (
	"encoding/json"
	"fmt"
)

const (
	ErrCodeEtcdNotReachable = 501
)

var (
	errorMap = map[int]string{
		ErrCodeEtcdNotReachable: "All the given peers are not reachable",
	}
)

type EtcdError struct {
	ErrorCode int    `json:"errorCode"`
	Message   string `json:"message"`
	Cause     string `json:"cause,omitempty"`
	Index     uint64 `json:"index"`
}

func (e EtcdError) Error() string {
	return fmt.Sprintf("%v: %v (%v) [%v]", e.ErrorCode, e.Message, e.Cause, e.Index)
}

func newError(errorCode int, cause string, index uint64) *EtcdError {
	return &EtcdError{
		ErrorCode: errorCode,
		Message:   errorMap[errorCode],
		Cause:     cause,
		Index:     index,
	}
}

func handleError(b []byte) error {
	etcdErr := new(EtcdError)

	err := json.Unmarshal(b, etcdErr)
	if err != nil {
		logger.Warningf("cannot unmarshal etcd error: %v", err)
		return err
	}

	return etcdErr
}