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 errors
11
12import (
13	"fmt"
14)
15
16// admin level errors - errors that are created in the clustering and accounting packages
17
18func NewAdminConnectionError(e error, msg string) Error {
19	return &err{level: EXCEPTION, ICode: 2000, IKey: "admin.clustering.connection_error", ICause: e,
20		InternalMsg: "Error connecting to " + msg, InternalCaller: CallerN(1)}
21}
22
23func NewAdminInvalidURL(component string, url string) Error {
24	return &err{level: EXCEPTION, ICode: 2010, IKey: "admin.invalid_url",
25		InternalMsg: fmt.Sprintf("Invalid %s url: %s", component, url), InternalCaller: CallerN(1)}
26}
27
28func NewAdminDecodingError(e error) Error {
29	return &err{level: EXCEPTION, ICode: 2020, IKey: "admin.json_decoding_error", ICause: e,
30		InternalMsg: "Error in JSON decoding", InternalCaller: CallerN(1)}
31}
32
33func NewAdminEncodingError(e error) Error {
34	return &err{level: EXCEPTION, ICode: 2030, IKey: "admin.json_encoding_error", ICause: e,
35		InternalMsg: "Error in JSON encoding", InternalCaller: CallerN(1)}
36}
37
38func NewAdminUnknownSettingError(setting string) Error {
39	return &err{level: EXCEPTION, ICode: 2031, IKey: "admin.unknown_setting",
40		InternalMsg: fmt.Sprintf("Unknown setting: %s", setting), InternalCaller: CallerN(1)}
41}
42
43func NewAdminSettingTypeError(setting string, value interface{}) Error {
44	return &err{level: EXCEPTION, ICode: 2032, IKey: "admin.setting_type_error",
45		InternalMsg: fmt.Sprintf("Incorrect value %v for setting: %s", value, setting), InternalCaller: CallerN(1)}
46}
47
48func NewAdminGetClusterError(e error, msg string) Error {
49	return &err{level: EXCEPTION, ICode: 2040, IKey: "admin.clustering.get_cluster_error", ICause: e,
50		InternalMsg: "Error retrieving cluster " + msg, InternalCaller: CallerN(1)}
51}
52
53func NewAdminAddClusterError(e error, msg string) Error {
54	return &err{level: EXCEPTION, ICode: 2050, IKey: "admin.clustering.add_cluster_error", ICause: e,
55		InternalMsg: "Error adding cluster " + msg, InternalCaller: CallerN(1)}
56}
57
58func NewAdminRemoveClusterError(e error, msg string) Error {
59	return &err{level: EXCEPTION, ICode: 2060, IKey: "admin.clustering.remove_cluster_error", ICause: e,
60		InternalMsg: "Error removing cluster " + msg, InternalCaller: CallerN(1)}
61}
62
63func NewAdminGetNodeError(e error, msg string) Error {
64	return &err{level: EXCEPTION, ICode: 2070, IKey: "admin.clustering.get_node_error", ICause: e,
65		InternalMsg: "Error retrieving node " + msg, InternalCaller: CallerN(1)}
66}
67
68func NewAdminNoNodeError(msg string) Error {
69	return &err{level: EXCEPTION, ICode: 2080, IKey: "admin.clustering.no_such_node",
70		InternalMsg: "No such  node " + msg, InternalCaller: CallerN(1)}
71}
72
73func NewAdminAddNodeError(e error, msg string) Error {
74	return &err{level: EXCEPTION, ICode: 2090, IKey: "admin.clustering.add_node_error", ICause: e,
75		InternalMsg: "Error adding node " + msg, InternalCaller: CallerN(1)}
76}
77
78func NewAdminRemoveNodeError(e error, msg string) Error {
79	return &err{level: EXCEPTION, ICode: 2100, IKey: "admin.clustering.remove_node_error", ICause: e,
80		InternalMsg: "Error removing node " + msg, InternalCaller: CallerN(1)}
81}
82
83func NewAdminMakeMetricError(e error, msg string) Error {
84	return &err{level: EXCEPTION, ICode: 2110, IKey: "admin.accounting.metric.create", ICause: e,
85		InternalMsg: "Error creating metric " + msg, InternalCaller: CallerN(1)}
86}
87
88const ADMIN_AUTH_ERROR = 2120
89
90func NewAdminAuthError(e error, msg string) Error {
91	return &err{level: EXCEPTION, ICode: ADMIN_AUTH_ERROR, IKey: "admin.clustering.authorize", ICause: e,
92		InternalMsg: "Error authorizing against cluster " + msg, InternalCaller: CallerN(1)}
93}
94
95const ADMIN_ENDPOINT_ERROR = 2130
96
97func NewAdminEndpointError(e error, msg string) Error {
98	return &err{level: EXCEPTION, ICode: ADMIN_ENDPOINT_ERROR, IKey: "admin.service.HttpEndpoint", ICause: e,
99		InternalMsg: msg, InternalCaller: CallerN(1)}
100}
101
102const ADMIN_SSL_NOT_ENABLED = 2140
103
104func NewAdminNotSSLEnabledError() Error {
105	return &err{level: EXCEPTION, ICode: ADMIN_SSL_NOT_ENABLED, IKey: "admin.service.ssl_cert",
106		InternalMsg: "server is not ssl enabled", InternalCaller: CallerN(1)}
107}
108
109const ADMIN_CREDS_ERROR = 2150
110
111func NewAdminCredsError(creds string, e error) Error {
112	return &err{level: EXCEPTION, ICode: ADMIN_CREDS_ERROR, IKey: "admin.accounting.bad_creds", ICause: e,
113		InternalMsg: "Not a proper creds JSON array of user/pass structures: " + creds, InternalCaller: CallerN(1)}
114}
115
116// completed requests qualifier settings
117func NewCompletedQualifierExists(what string) Error {
118	return &err{level: EXCEPTION, ICode: 2160, IKey: "admin.accounting.completed.already_exists",
119		InternalMsg: "Completed requests qualifier already set: " + what, InternalCaller: CallerN(1)}
120}
121
122func NewCompletedQualifierUnknown(what string) Error {
123	return &err{level: EXCEPTION, ICode: 2170, IKey: "admin.accounting.completed.unknown",
124		InternalMsg: "Completed requests qualifier unknown: " + what, InternalCaller: CallerN(1)}
125}
126
127const ADMIN_QUALIFIER_NOT_SET = 2180
128
129func NewCompletedQualifierNotFound(what string, cond interface{}) Error {
130	var condString string
131
132	if cond != nil {
133		condString = fmt.Sprintf(" %v", cond)
134	}
135	return &err{level: EXCEPTION, ICode: ADMIN_QUALIFIER_NOT_SET, IKey: "admin.accounting.completed.not_found",
136		InternalMsg: "Completed requests qualifier not set: " + what + condString, InternalCaller: CallerN(1)}
137}
138
139const ADMIN_QUALIFIER_NOT_UNIQUE = 2190
140
141func NewCompletedQualifierNotUnique(what string) Error {
142	return &err{level: EXCEPTION, ICode: ADMIN_QUALIFIER_NOT_UNIQUE, IKey: "admin.accounting.completed.not_unique",
143		InternalMsg: "Completed requests qualifier can only be deployed once: " + what, InternalCaller: CallerN(1)}
144}
145
146func NewCompletedQualifierInvalidArgument(what string, cond interface{}) Error {
147	var condString string
148
149	if cond != nil {
150		condString = fmt.Sprintf(" %v", cond)
151	}
152	return &err{level: EXCEPTION, ICode: 2200, IKey: "admin.accounting.completed.invalid",
153		InternalMsg: "Completed requests qualifier " + what + " cannot accept argument " + condString, InternalCaller: CallerN(1)}
154}
155
156func NewAdminBadServicePort(port string) Error {
157	return &err{level: EXCEPTION, ICode: 2210, IKey: "admin.clustering.bad_port",
158		InternalMsg: "Invalid service port: " + port, InternalCaller: CallerN(1)}
159}
160
161func NewAdminBodyError(e error) Error {
162	return &err{level: EXCEPTION, ICode: 2220, IKey: "admin.accounting.bad_body", ICause: e,
163		InternalMsg: "Error getting request body", InternalCaller: CallerN(1)}
164}
165