1// Copyright (c) 2014 Couchbase, Inc.
2// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
3// except in compliance with the License. You may obtain a copy of the License at
4//   http://www.apache.org/licenses/LICENSE-2.0
5// Unless required by applicable law or agreed to in writing, software distributed under the
6// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
7// either express or implied. See the License for the specific language governing permissions
8// and limitations under the License.
9
10package indexer
11
12import (
13	"fmt"
14
15	"github.com/couchbase/indexing/secondary/common"
16)
17
18type errCode int16
19
20const (
21	ERROR_PANIC errCode = iota
22
23	//Slab Manager
24	ERROR_SLAB_INIT
25	ERROR_SLAB_BAD_ALLOC_REQUEST
26	ERROR_SLAB_INTERNAL_ALLOC_ERROR
27	ERROR_SLAB_MEM_LIMIT_EXCEED
28	ERROR_SLAB_INTERNAL_ERROR
29
30	//Stream Reader
31	ERROR_STREAM_INIT
32	ERROR_STREAM_READER_UNKNOWN_COMMAND
33	ERROR_STREAM_READER_UNKNOWN_ERROR
34	ERROR_STREAM_READER_PANIC
35	ERROR_STREAM_READER_STREAM_SHUTDOWN
36
37	//Mutation Manager
38	ERROR_MUT_MGR_INTERNAL_ERROR
39	ERROR_MUT_MGR_STREAM_ALREADY_OPEN
40	ERROR_MUT_MGR_STREAM_ALREADY_CLOSED
41	ERROR_MUT_MGR_UNKNOWN_COMMAND
42	ERROR_MUT_MGR_UNCLEAN_SHUTDOWN
43	ERROR_MUT_MGR_PANIC
44
45	//Mutation Queue
46	ERROR_MUTATION_QUEUE_INIT
47
48	//Timekeeper
49	ERROR_TK_UNKNOWN_STREAM
50
51	//KVSender
52	ERROR_KVSENDER_UNKNOWN_INDEX
53	ERROR_KVSENDER_STREAM_ALREADY_OPEN
54	ERROR_KVSENDER_STREAM_REQUEST_ERROR
55	ERROR_KV_SENDER_UNKNOWN_STREAM
56	ERROR_KV_SENDER_UNKNOWN_BUCKET
57	ERROR_KVSENDER_STREAM_ALREADY_CLOSED
58
59	//ScanCoordinator
60	ERROR_SCAN_COORD_UNKNOWN_COMMAND
61	ERROR_SCAN_COORD_INTERNAL_ERROR
62
63	//INDEXER
64	ERROR_INDEX_ALREADY_EXISTS
65	ERROR_INDEXER_INTERNAL_ERROR
66	ERROR_INDEX_BUILD_IN_PROGRESS
67	ERROR_INDEX_DROP_IN_PROGRESS
68	ERROR_INDEXER_UNKNOWN_INDEX
69	ERROR_INDEXER_UNKNOWN_BUCKET
70	ERROR_INDEXER_IN_RECOVERY
71	ERROR_INDEXER_NOT_ACTIVE
72	ERROR_INDEXER_REBALANCE_IN_PROGRESS
73
74	//STORAGE_MGR
75	ERROR_STORAGE_MGR_ROLLBACK_FAIL
76	ERROR_STORAGE_MGR_MERGE_SNAPSHOT_FAIL
77
78	//CLUSTER_MGR_AGENT
79	ERROR_CLUSTER_MGR_AGENT_INIT
80	ERROR_CLUSTER_MGR_CREATE_FAIL
81	ERROR_CLUSTER_MGR_DROP_FAIL
82
83	ERROR_INDEX_MANAGER_PANIC
84	ERROR_INDEX_MANAGER_CHANNEL_CLOSE
85
86	ERROR_SCAN_COORD_QUERYPORT_FAIL
87	ERROR_BUCKET_EPHEMERAL
88)
89
90type errSeverity int16
91
92const (
93	FATAL errSeverity = iota
94	NORMAL
95)
96
97type errCategory int16
98
99const (
100	MESSAGING errCategory = iota
101	STORAGE
102	MUTATION_QUEUE
103	TOPOLOGY
104	STREAM_READER
105	SLAB_MANAGER
106	MUTATION_MANAGER
107	TIMEKEEPER
108	SCAN_COORD
109	INDEXER
110	STORAGE_MGR
111	CLUSTER_MGR
112)
113
114type Error struct {
115	code     errCode
116	severity errSeverity
117	category errCategory
118	cause    error
119	msg      string
120}
121
122func (e Error) String() string {
123	return fmt.Sprintf("%v", e.cause)
124}
125
126func (e Error) convertError() common.IndexerErrCode {
127
128	switch e.code {
129	case ERROR_INDEX_ALREADY_EXISTS:
130		return common.IndexAlreadyExist
131	case ERROR_INDEXER_INTERNAL_ERROR:
132		return common.TransientError
133	case ERROR_INDEX_BUILD_IN_PROGRESS:
134		return common.IndexBuildInProgress
135	case ERROR_INDEX_DROP_IN_PROGRESS:
136		return common.DropIndexInProgress
137	case ERROR_INDEXER_UNKNOWN_INDEX:
138		return common.IndexNotExist
139	case ERROR_INDEXER_UNKNOWN_BUCKET:
140		return common.InvalidBucket
141	case ERROR_INDEXER_IN_RECOVERY:
142		return common.IndexerInRecovery
143	case ERROR_INDEXER_NOT_ACTIVE:
144		return common.IndexerNotActive
145	case ERROR_INDEXER_REBALANCE_IN_PROGRESS:
146		return common.RebalanceInProgress
147	case ERROR_BUCKET_EPHEMERAL:
148		return common.BucketEphemeral
149	}
150
151	return common.TransientError
152}
153