1//  Copyright (c) 2018 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
16const (
17	//FTS errors
18	FTS_MISSING_PORT = 10003
19	NODE_ACCESS_ERR  = 10004
20	NODE_SERVICE_ERR = 10005
21)
22
23func NewFTSMissingPortErr(e string) Error {
24	return &err{level: EXCEPTION, ICode: FTS_MISSING_PORT, IKey: "fts.url.format.error", ICause: fmt.Errorf("%v", e),
25		InternalMsg:    fmt.Sprintf("Missing or Incorrect port in input url."),
26		InternalCaller: CallerN(1)}
27}
28
29func NewNodeInfoAccessErr(e string) Error {
30	return &err{level: EXCEPTION, ICode: NODE_ACCESS_ERR, IKey: "node.access.error", ICause: fmt.Errorf("%v", e),
31		InternalMsg:    fmt.Sprintf("Issue with accessing node information for rest endpoint %v", e),
32		InternalCaller: CallerN(1)}
33}
34
35func NewNodeServiceErr(e string) Error {
36	return &err{level: EXCEPTION, ICode: NODE_SERVICE_ERR, IKey: "node.service.error", ICause: fmt.Errorf("%v", e),
37		InternalMsg:    fmt.Sprintf("No FTS node in server %v", e),
38		InternalCaller: CallerN(1)}
39}
40
41func NewFunctionsNotSupported() Error {
42	return &err{level: EXCEPTION, ICode: 10100, IKey: "function.CE.error",
43		InternalMsg:    fmt.Sprintf("Functions are only supported in Enterprise Edition"),
44		InternalCaller: CallerN(1)}
45}
46
47func NewMissingFunctionError(f string) Error {
48	return &err{level: EXCEPTION, ICode: 10101, IKey: "function.missing.error",
49		InternalMsg:    fmt.Sprintf("Function not found %v", f),
50		InternalCaller: CallerN(1)}
51}
52
53func NewDuplicateFunctionError(f string) Error {
54	return &err{level: EXCEPTION, ICode: 10102, IKey: "function.duplicate.error", ICause: fmt.Errorf("%v", f),
55		InternalMsg:    fmt.Sprintf("Function already exists %v", f),
56		InternalCaller: CallerN(1)}
57}
58
59func NewInternalFunctionError(e error, f string) Error {
60	return &err{level: EXCEPTION, ICode: 10103, IKey: "function.internal.error", ICause: e,
61		InternalMsg:    fmt.Sprintf("Operation on function %v encountered an unexpected error %v. Please collect the failing statement and contact support", f, e),
62		InternalCaller: CallerN(1)}
63}
64
65func NewArgumentsMismatchError(f string) Error {
66	return &err{level: EXCEPTION, ICode: 10104, IKey: "function.mismatching.error", ICause: fmt.Errorf("%v", f),
67		InternalMsg:    fmt.Sprintf("Incorrect number of arguments supplied to function %v", f),
68		InternalCaller: CallerN(1)}
69}
70
71func NewInvalidFunctionNameError(name string) Error {
72	return &err{level: EXCEPTION, ICode: 10105, IKey: "function.name.error",
73		InternalMsg:    fmt.Sprintf("Invalid function name %v", name),
74		InternalCaller: CallerN(1)}
75}
76
77func NewMetaKVError(where string, what error) Error {
78	return &err{level: EXCEPTION, ICode: 10106, IKey: "function.storage.error", ICause: what,
79		InternalMsg:    fmt.Sprintf("Could not access function definition for %v because %v", where, what),
80		InternalCaller: CallerN(1)}
81}
82
83// same number and key as above, not an error
84func NewMetaKVChangeCounterError(what error) Error {
85	return &err{level: EXCEPTION, ICode: 10106, IKey: "function.storage.error", ICause: what,
86		InternalMsg:    fmt.Sprintf("Could not access functions change counter because %v", what),
87		InternalCaller: CallerN(1)}
88}
89
90// same number and key as above, not an error
91func NewMetaKVIndexError(what error) Error {
92	return &err{level: EXCEPTION, ICode: 10106, IKey: "function.storage.error", ICause: what,
93		InternalMsg:    fmt.Sprintf("Could not access functions definitions because %v", what),
94		InternalCaller: CallerN(1)}
95}
96
97func NewFunctionEncodingError(what string, name string, reason error) Error {
98	return &err{level: EXCEPTION, ICode: 10107, IKey: "function.encoding.error", ICause: reason,
99		InternalMsg:    fmt.Sprintf("Could not %v function definition for %v because %v", what, name, reason),
100		InternalCaller: CallerN(1)}
101}
102
103func NewFunctionsDisabledError(what string) Error {
104	return &err{level: EXCEPTION, ICode: 10108, IKey: "function.golang.disabled.error",
105		InternalMsg: fmt.Sprintf("%v functions are disabled", what), InternalCaller: CallerN(1)}
106}
107
108func NewFunctionExecutionError(what string, name string, reason error) Error {
109	return &err{level: EXCEPTION, ICode: 10109, IKey: "function.execution.error", ICause: reason,
110		InternalMsg:    fmt.Sprintf("Error executing function %v %v: %v", name, what, reason),
111		InternalCaller: CallerN(1)}
112}
113