1// Copyright (c) 2014 Couchbase, Inc.
2
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
4// except in compliance with the License. You may obtain a copy of the License at
5//   http://www.apache.org/licenses/LICENSE-2.0
6// Unless required by applicable law or agreed to in writing, software distributed under the
7// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
8// either express or implied. See the License for the specific language governing permissions
9// and limitations under the License.
10package manager
11
12import (
13	"fmt"
14)
15
16// The error handling has the same setup as the indexer.
17// This is to help with consolidation on a common error facility
18// at a later point.
19
20type errCode int16
21
22const (
23	// generic (0 - 50)
24	ERROR_PANIC     errCode = 0
25	ERROR_ARGUMENTS         = 1
26
27	// MetadataRepo (51-100)
28	ERROR_META_WRONG_KEY          = 51
29	ERROR_META_IDX_DEFN_EXIST     = 52
30	ERROR_META_IDX_DEFN_NOT_EXIST = 53
31	ERROR_META_FAIL_TO_PARSE_INT  = 54
32
33	// Event Manager (101-150)
34	ERROR_EVT_DUPLICATE_NOTIFIER = 101
35
36	// Index Manager (151-200)
37	ERROR_MGR_DDL_CREATE_IDX = 151
38	ERROR_MGR_DDL_DROP_IDX   = 152
39
40	// Coordinator (201-250)
41	ERROR_COOR_LISTENER_FAIL = 201
42	ERROR_COOR_ELECTION_FAIL = 202
43
44	// Watcher (251 - 300)
45	ERROR_WATCH_NO_ADDR_AVAIL = 251
46
47	// Stream (301-350)
48	ERROR_STREAM_INVALID_ARGUMENT   = 301
49	ERROR_STREAM_NOT_OPEN           = 302
50	ERROR_STREAM_REQUEST_ERROR      = 303
51	ERROR_STREAM_WRONG_VBUCKET      = 304
52	ERROR_STREAM_INVALID_TIMESTAMP  = 305
53	ERROR_STREAM_PROJECTOR_TIMEOUT  = 306
54	ERROR_STREAM_INVALID_KVADDRS    = 307
55	ERROR_STREAM_STREAM_END         = 308
56	ERROR_STREAM_FEEDER             = 309
57	ERROR_STREAM_INCONSISTENT_VBMAP = 310
58)
59
60type errSeverity int16
61
62const (
63	FATAL errSeverity = iota
64	NORMAL
65)
66
67type errCategory int16
68
69const (
70	GENERIC errCategory = iota
71	COORDINATOR
72	INDEX_MANAGER
73	METADATA_REPO
74	REQUEST_HANDLER
75	EVENT_MANAGER
76	WATCHER
77	STREAM
78)
79
80type Error struct {
81	code     errCode
82	severity errSeverity
83	category errCategory
84	cause    error
85	msg      string
86}
87
88func NewError(code errCode, severity errSeverity, category errCategory, cause error, msg string) Error {
89	return Error{code: code,
90		severity: severity,
91		category: category,
92		cause:    cause,
93		msg:      msg}
94}
95
96func NewError4(code errCode, severity errSeverity, category errCategory, msg string) Error {
97	return Error{code: code,
98		severity: severity,
99		category: category,
100		cause:    nil,
101		msg:      msg}
102}
103
104func NewError3(code errCode, severity errSeverity, category errCategory) Error {
105	return Error{code: code,
106		severity: severity,
107		category: category,
108		cause:    nil,
109		msg:      ""}
110}
111
112func NewError2(code errCode, category errCategory) Error {
113	return Error{code: code,
114		severity: NORMAL,
115		category: category,
116		cause:    nil,
117		msg:      ""}
118}
119
120func (e Error) Error() string {
121	return fmt.Sprintf("Error :: code= %d, severity= %s, category= %s, reason= %s, cause= %s",
122		e.code, severity(e.severity), category(e.category), e.msg, e.cause)
123}
124
125func category(category errCategory) string {
126	switch category {
127	case GENERIC:
128		return "Generic"
129	case COORDINATOR:
130		return "Coordinator"
131	case INDEX_MANAGER:
132		return "Index Manager"
133	case METADATA_REPO:
134		return "Metadata Repo"
135	case REQUEST_HANDLER:
136		return "Request Handler"
137	case EVENT_MANAGER:
138		return "Event Manager"
139	case WATCHER:
140		return "Watcher"
141	case STREAM:
142		return "Stream"
143	}
144	return ""
145}
146
147func severity(severity errSeverity) string {
148	switch severity {
149	case NORMAL:
150		return "Normal"
151	case FATAL:
152		return "Fatal"
153	}
154	return ""
155}
156