forked from ngaut/unistore
/
error.go
116 lines (96 loc) · 3.21 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2019-present PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package raftstore
import (
"fmt"
"github.com/pingcap/errors"
"github.com/pingcap/kvproto/pkg/errorpb"
"github.com/pingcap/kvproto/pkg/metapb"
)
type ErrNotLeader struct {
RegionId uint64
Leader *metapb.Peer
}
func (e *ErrNotLeader) Error() string {
return fmt.Sprintf("region %v is not leader", e.RegionId)
}
type ErrRegionNotFound struct {
RegionId uint64
}
func (e *ErrRegionNotFound) Error() string {
return fmt.Sprintf("region %v is not found", e.RegionId)
}
type ErrKeyNotInRegion struct {
Key []byte
Region *metapb.Region
}
func (e *ErrKeyNotInRegion) Error() string {
return fmt.Sprintf("key %v is not in region %v", e.Key, e.Region)
}
type ErrEpochNotMatch struct {
Message string
Regions []*metapb.Region
}
func (e *ErrEpochNotMatch) Error() string {
return fmt.Sprintf("epoch not match, error msg %v, regions %v", e.Message, e.Regions)
}
type ErrServerIsBusy struct {
Reason string
BackoffMs uint64
}
func (e *ErrServerIsBusy) Error() string {
return fmt.Sprintf("server is busy, reason %v, backoff ms %v", e.Reason, e.BackoffMs)
}
type ErrStaleCommand struct{}
func (e *ErrStaleCommand) Error() string {
return fmt.Sprintf("stale command")
}
type ErrStoreNotMatch struct {
RequestStoreId uint64
ActualStoreId uint64
}
func (e *ErrStoreNotMatch) Error() string {
return fmt.Sprintf("store not match, request store id is %v, but actual store id is %v", e.RequestStoreId, e.ActualStoreId)
}
type ErrRaftEntryTooLarge struct {
RegionId uint64
EntrySize uint64
}
func (e *ErrRaftEntryTooLarge) Error() string {
return fmt.Sprintf("raft entry too large, region_id: %v, len: %v", e.RegionId, e.EntrySize)
}
func RaftstoreErrToPbError(e error) *errorpb.Error {
ret := new(errorpb.Error)
switch err := errors.Cause(e).(type) {
case *ErrNotLeader:
ret.NotLeader = &errorpb.NotLeader{RegionId: err.RegionId, Leader: err.Leader}
case *ErrRegionNotFound:
ret.RegionNotFound = &errorpb.RegionNotFound{RegionId: err.RegionId}
case *ErrKeyNotInRegion:
ret.KeyNotInRegion = &errorpb.KeyNotInRegion{Key: err.Key, RegionId: err.Region.Id,
StartKey: err.Region.StartKey, EndKey: err.Region.EndKey}
case *ErrEpochNotMatch:
ret.EpochNotMatch = &errorpb.EpochNotMatch{CurrentRegions: err.Regions}
case *ErrServerIsBusy:
ret.ServerIsBusy = &errorpb.ServerIsBusy{Reason: err.Reason, BackoffMs: err.BackoffMs}
case *ErrStaleCommand:
ret.StaleCommand = &errorpb.StaleCommand{}
case *ErrStoreNotMatch:
ret.StoreNotMatch = &errorpb.StoreNotMatch{RequestStoreId: err.RequestStoreId, ActualStoreId: err.ActualStoreId}
case *ErrRaftEntryTooLarge:
ret.RaftEntryTooLarge = &errorpb.RaftEntryTooLarge{RegionId: err.RegionId, EntrySize: err.EntrySize}
default:
ret.Message = e.Error()
}
return ret
}