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	"time"
15)
16
17// service level errors - errors that are created in the service package
18
19func NewServiceErrorReadonly(msg string) Error {
20	return &err{level: EXCEPTION, ICode: 1000, IKey: "service.io.readonly", InternalMsg: msg, InternalCaller: CallerN(1)}
21}
22
23func NewServiceErrorHTTPMethod(method string) Error {
24	return &err{level: EXCEPTION, ICode: 1010, IKey: "service.io.http.unsupported_method",
25		InternalMsg: fmt.Sprintf("Unsupported http method: %s", method), InternalCaller: CallerN(1)}
26}
27
28func NewServiceErrorNotImplemented(feature string, value string) Error {
29	return &err{level: EXCEPTION, ICode: 1020, IKey: "service.io.request.unimplemented",
30		InternalMsg: fmt.Sprintf("%s %s not yet implemented", value, feature), InternalCaller: CallerN(1)}
31}
32
33func NewServiceErrorUnrecognizedValue(feature string, value string) Error {
34	return &err{level: EXCEPTION, ICode: 1030, IKey: "service.io.request.unrecognized_value",
35		InternalMsg: fmt.Sprintf("Unknown %s value: %s", feature, value), InternalCaller: CallerN(1)}
36}
37
38func NewServiceErrorBadValue(e error, feature string) Error {
39	return &err{level: EXCEPTION, ICode: 1040, IKey: "service.io.request.bad_value", ICause: e,
40		InternalMsg: fmt.Sprintf("Error processing %s", feature), InternalCaller: CallerN(1)}
41}
42
43func NewServiceErrorMissingValue(feature string) Error {
44	return &err{level: EXCEPTION, ICode: 1050, IKey: "service.io.request.missing_value",
45		InternalMsg: fmt.Sprintf("No %s value", feature), InternalCaller: CallerN(1)}
46}
47
48const SERVICE_MULTIPLE_VALUES = 1060
49
50func NewServiceErrorMultipleValues(feature string) Error {
51	return &err{level: EXCEPTION, ICode: SERVICE_MULTIPLE_VALUES, IKey: "service.io.request.multiple_values",
52		InternalMsg: fmt.Sprintf("Multiple values for %s.", feature), InternalCaller: CallerN(1)}
53}
54
55func NewServiceErrorUnrecognizedParameter(parameter string) Error {
56	return &err{level: EXCEPTION, ICode: 1065, IKey: "service.io.request.unrecognized_parameter",
57		InternalMsg: fmt.Sprintf("Unrecognized parameter in request: %s", parameter), InternalCaller: CallerN(1)}
58}
59
60func NewServiceErrorTypeMismatch(feature string, expected string) Error {
61	return &err{level: EXCEPTION, ICode: 1070, IKey: "service.io.request.type_mismatch",
62		InternalMsg: fmt.Sprintf("%s has to be of type %s", feature, expected), InternalCaller: CallerN(1)}
63}
64
65func NewTimeoutError(timeout time.Duration) Error {
66	return &err{level: EXCEPTION, ICode: 1080, IKey: "timeout", InternalMsg: fmt.Sprintf("Timeout %v exceeded", timeout),
67		InternalCaller: CallerN(1), retry: true}
68}
69
70func NewServiceErrorInvalidJSON(e error) Error {
71	return &err{level: EXCEPTION, ICode: 1100, IKey: "service.io.response.invalid_json", ICause: e,
72		InternalMsg: "Invalid JSON in results", InternalCaller: CallerN(1)}
73}
74
75func NewServiceErrorClientID(id string) Error {
76	return &err{level: EXCEPTION, ICode: 1110, IKey: "service.io.response.client_id",
77		InternalMsg: "forbidden character (\\ or \") in client_context_id", InternalCaller: CallerN(1)}
78}
79
80func NewServiceErrorMediaType(mediaType string) Error {
81	return &err{level: EXCEPTION, ICode: 1120, IKey: "service.io.request.media_type",
82		InternalMsg: fmt.Sprintf("Unsupported media type: %s", mediaType), InternalCaller: CallerN(1)}
83}
84
85func NewServiceErrorHttpReq(id string) Error {
86	return &err{level: EXCEPTION, ICode: 1130, IKey: "service.io.request.type",
87		InternalMsg: fmt.Sprintf("Request %s is not a http request", id), InternalCaller: CallerN(1)}
88}
89
90func NewServiceErrorScanVectorBadLength(vec []interface{}) Error {
91	return &err{level: EXCEPTION, ICode: 1140, IKey: "service.io.request.scan_vector.length",
92		InternalMsg: fmt.Sprintf("Array %v should be of length 2", vec), InternalCaller: CallerN(1)}
93}
94
95func NewServiceErrorScanVectorBadSequenceNumber(seq interface{}) Error {
96	return &err{level: EXCEPTION, ICode: 1150, IKey: "service.io.request.scan_vector.sequence",
97		InternalMsg: fmt.Sprintf("Bad sequence number %v. Expected an unsigned 64-bit integer.", seq), InternalCaller: CallerN(1)}
98}
99
100func NewServiceErrorScanVectorBadUUID(uuid interface{}) Error {
101	return &err{level: EXCEPTION, ICode: 1155, IKey: "service.io.request.scan_vector.uuid",
102		InternalMsg: fmt.Sprintf("Bad UUID %v. Expected a string.", uuid), InternalCaller: CallerN(1)}
103}
104
105func NewServiceErrorDecodeNil() Error {
106	return &err{level: EXCEPTION, ICode: 1160, IKey: "service.io.request.nil",
107		InternalMsg: "Failed to decode nil value.", InternalCaller: CallerN(1)}
108}
109
110func NewServiceErrorHttpMethod(method string) Error {
111	return &err{level: EXCEPTION, ICode: 1170, IKey: "service.io.request.method",
112		InternalMsg: fmt.Sprintf("Unsupported method %s", method), InternalCaller: CallerN(1)}
113}
114