1d61bd6e7STrond Norbye /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2d61bd6e7STrond Norbye /*
3d61bd6e7STrond Norbye  *     Copyright 2016 Couchbase, Inc.
4d61bd6e7STrond Norbye  *
5d61bd6e7STrond Norbye  *   Licensed under the Apache License, Version 2.0 (the "License");
6d61bd6e7STrond Norbye  *   you may not use this file except in compliance with the License.
7d61bd6e7STrond Norbye  *   You may obtain a copy of the License at
8d61bd6e7STrond Norbye  *
9d61bd6e7STrond Norbye  *       http://www.apache.org/licenses/LICENSE-2.0
10d61bd6e7STrond Norbye  *
11d61bd6e7STrond Norbye  *   Unless required by applicable law or agreed to in writing, software
12d61bd6e7STrond Norbye  *   distributed under the License is distributed on an "AS IS" BASIS,
13d61bd6e7STrond Norbye  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14d61bd6e7STrond Norbye  *   See the License for the specific language governing permissions and
15d61bd6e7STrond Norbye  *   limitations under the License.
16d61bd6e7STrond Norbye  */
17d61bd6e7STrond Norbye #pragma once
18d61bd6e7STrond Norbye 
19d61bd6e7STrond Norbye #include <memcached/engine_utilities_visibility.h>
20d61bd6e7STrond Norbye #include <platform/platform.h>
21*7fb6fad2SDave Rigby 
22*7fb6fad2SDave Rigby #include <iosfwd>
23d61bd6e7STrond Norbye #include <system_error>
24d61bd6e7STrond Norbye 
2546d5c978STrond Norbye #ifdef _MSC_VER
2646d5c978STrond Norbye // We need to add the right __declspec magic to the system types
2746d5c978STrond Norbye // to avoid msvc to spit out warnings
2846d5c978STrond Norbye class ENGINE_UTILITIES_PUBLIC_API std::system_error;
2946d5c978STrond Norbye #endif
3046d5c978STrond Norbye 
31d61bd6e7STrond Norbye namespace cb {
32d61bd6e7STrond Norbye /**
33d61bd6e7STrond Norbye  * The Error enum contains all of the various error codes the engines
34d61bd6e7STrond Norbye  * may return. They may be used together with the cb::engine_category()
35d61bd6e7STrond Norbye  * in std::system_error exceptions (where you can fetch the code, and the
36d61bd6e7STrond Norbye  * textual description for the given error).
37d61bd6e7STrond Norbye  */
38d61bd6e7STrond Norbye enum class engine_errc {
39d61bd6e7STrond Norbye     /** The command executed successfully */
40d61bd6e7STrond Norbye     success = 0x00,
41d61bd6e7STrond Norbye     /** The key does not exists */
42d61bd6e7STrond Norbye     no_such_key = 0x01,
43d61bd6e7STrond Norbye     /** The key already exists */
44d61bd6e7STrond Norbye     key_already_exists = 0x02,
45d61bd6e7STrond Norbye     /** Could not allocate memory */
46d61bd6e7STrond Norbye     no_memory = 0x03,
47d61bd6e7STrond Norbye     /** The item was not stored */
48d61bd6e7STrond Norbye     not_stored = 0x04,
49d61bd6e7STrond Norbye     /** Invalid arguments */
50d61bd6e7STrond Norbye     invalid_arguments = 0x05,
51d61bd6e7STrond Norbye     /** The engine does not support this */
52d61bd6e7STrond Norbye     not_supported = 0x06,
53d61bd6e7STrond Norbye     /** This would cause the engine to block */
54d61bd6e7STrond Norbye     would_block = 0x07,
55d61bd6e7STrond Norbye     /** The data is too big for the engine */
56d61bd6e7STrond Norbye     too_big = 0x08,
57d61bd6e7STrond Norbye     /** The engine want more data if the frontend have more data available. */
58d61bd6e7STrond Norbye     want_more = 0x09,
59d61bd6e7STrond Norbye     /** Tell the server to disconnect this client */
60d61bd6e7STrond Norbye     disconnect = 0x0a,
61d61bd6e7STrond Norbye     /** Access control violations */
62d61bd6e7STrond Norbye     no_access = 0x0b,
63d61bd6e7STrond Norbye     /** < This vbucket doesn't belong to me */
64d61bd6e7STrond Norbye     not_my_vbucket = 0x0c,
65d61bd6e7STrond Norbye     /** Temporary failure, please try again later */
66d61bd6e7STrond Norbye     temporary_failure = 0x0d,
67d61bd6e7STrond Norbye     /** Value outside legal range */
68d61bd6e7STrond Norbye     out_of_range = 0x0e,
69d61bd6e7STrond Norbye     /** Roll back to a previous version */
70d61bd6e7STrond Norbye     rollback = 0x0f,
71d61bd6e7STrond Norbye     /** The connection isn't bound to an engine */
72d61bd6e7STrond Norbye     no_bucket = 0x10,
73d61bd6e7STrond Norbye     /** Can't serve the request.. busy */
74d61bd6e7STrond Norbye     too_busy = 0x11,
75d61bd6e7STrond Norbye     /** Auth data stale */
76d61bd6e7STrond Norbye     authentication_stale = 0x12,
77d61bd6e7STrond Norbye     /**
78d61bd6e7STrond Norbye      * The value stored in the document is incompatible with the
79d61bd6e7STrond Norbye      * requested operation.
80d61bd6e7STrond Norbye      */
81d61bd6e7STrond Norbye     delta_badval = 0x13,
82463b4257STrond Norbye     /**
83463b4257STrond Norbye      * The requested resource is locked
84463b4257STrond Norbye      */
85463b4257STrond Norbye     locked = 0x14,
86463b4257STrond Norbye     /**
87463b4257STrond Norbye      * The requested resource is locked, but the engine used to
88463b4257STrond Norbye      * report it as a tmpfail (we need to be able to separate this
89463b4257STrond Norbye      * in the core so that old clients can get TMPFAIL back instead
90463b4257STrond Norbye      * of EEXISTS
91463b4257STrond Norbye      */
92463b4257STrond Norbye     locked_tmpfail = 0x15,
93e9b529a1SJim Walker 
94e9b529a1SJim Walker     /**
95e9b529a1SJim Walker      * The request has no collection or an unknown collection.
96e9b529a1SJim Walker      */
97e9b529a1SJim Walker     unknown_collection = 0x16,
98e9b529a1SJim Walker 
998f9005d0SJim Walker     /**
1008f9005d0SJim Walker      * A command with a predicate has been failed because of the predicate
1018f9005d0SJim Walker      */
1028f9005d0SJim Walker     predicate_failed = 0x17,
1038f9005d0SJim Walker 
1049628a0d6SJim Walker     /**
1059628a0d6SJim Walker      * The request cannot complete until a collections manifest has been set
1069628a0d6SJim Walker      */
1079628a0d6SJim Walker     no_collections_manifest = 0x18,
1089628a0d6SJim Walker 
109d61bd6e7STrond Norbye     /** Generic failue. */
110d61bd6e7STrond Norbye     failed = 0xff
111d61bd6e7STrond Norbye };
112d61bd6e7STrond Norbye 
113d61bd6e7STrond Norbye /**
114d61bd6e7STrond Norbye  * Get the error category object used to map from numeric values to
115d61bd6e7STrond Norbye  * a textual representation of the error code.
116d61bd6e7STrond Norbye  *
117d61bd6e7STrond Norbye  * @return The one and only instance of the error object
118d61bd6e7STrond Norbye  */
120d61bd6e7STrond Norbye const std::error_category& engine_error_category() NOEXCEPT;
121d61bd6e7STrond Norbye 
122d61bd6e7STrond Norbye class ENGINE_UTILITIES_PUBLIC_API engine_error : public std::system_error {
123d61bd6e7STrond Norbye public:
engine_error(engine_errc ev,const std::string & what_arg)124d61bd6e7STrond Norbye     engine_error(engine_errc ev, const std::string& what_arg)
125d61bd6e7STrond Norbye         : system_error(int(ev), engine_error_category(), what_arg) {}
126d61bd6e7STrond Norbye 
engine_error(engine_errc ev,const char * what_arg)127d61bd6e7STrond Norbye     engine_error(engine_errc ev, const char* what_arg)
128d61bd6e7STrond Norbye         : system_error(int(ev), engine_error_category(), what_arg) {}
129d61bd6e7STrond Norbye };
130d61bd6e7STrond Norbye 
make_error_condition(engine_errc e)131d61bd6e7STrond Norbye static inline std::error_condition make_error_condition(engine_errc e) {
132d61bd6e7STrond Norbye     return std::error_condition(int(e), engine_error_category());
133d61bd6e7STrond Norbye }
134d61bd6e7STrond Norbye 
1368fa52e42STrond Norbye std::string to_string(engine_errc ev);
1378fa52e42STrond Norbye 
138d4c15b06SDave Rigby // GoogleTest printing function.
140d4c15b06SDave Rigby void PrintTo(engine_errc ev, ::std::ostream* os);
141d4c15b06SDave Rigby 
142d4c15b06SDave Rigby } // namespace cb
143d61bd6e7STrond Norbye 
144f581b888Solivermd // For checkeqfn
146f581b888Solivermd std::ostream& operator<<(std::ostream& os, cb::engine_errc ec);
148d61bd6e7STrond Norbye // For backwards compatibility with the old memcached source code we need
149d61bd6e7STrond Norbye // to keep the old constants around
150d61bd6e7STrond Norbye typedef enum {
151d61bd6e7STrond Norbye     ENGINE_SUCCESS = int(cb::engine_errc::success),
152d61bd6e7STrond Norbye     ENGINE_KEY_ENOENT = int(cb::engine_errc::no_such_key),
153d61bd6e7STrond Norbye     ENGINE_KEY_EEXISTS = int(cb::engine_errc::key_already_exists),
154d61bd6e7STrond Norbye     ENGINE_ENOMEM = int(cb::engine_errc::no_memory),
155d61bd6e7STrond Norbye     ENGINE_NOT_STORED = int(cb::engine_errc::not_stored),
156d61bd6e7STrond Norbye     ENGINE_EINVAL = int(cb::engine_errc::invalid_arguments),
157d61bd6e7STrond Norbye     ENGINE_ENOTSUP = int(cb::engine_errc::not_supported),
158d61bd6e7STrond Norbye     ENGINE_EWOULDBLOCK = int(cb::engine_errc::would_block),
159d61bd6e7STrond Norbye     ENGINE_E2BIG = int(cb::engine_errc::too_big),
160d61bd6e7STrond Norbye     ENGINE_WANT_MORE = int(cb::engine_errc::want_more),
161d61bd6e7STrond Norbye     ENGINE_DISCONNECT = int(cb::engine_errc::disconnect),
162d61bd6e7STrond Norbye     ENGINE_EACCESS = int(cb::engine_errc::no_access),
163d61bd6e7STrond Norbye     ENGINE_NOT_MY_VBUCKET = int(cb::engine_errc::not_my_vbucket),
164d61bd6e7STrond Norbye     ENGINE_TMPFAIL = int(cb::engine_errc::temporary_failure),
165d61bd6e7STrond Norbye     ENGINE_ERANGE = int(cb::engine_errc::out_of_range),
166d61bd6e7STrond Norbye     ENGINE_ROLLBACK = int(cb::engine_errc::rollback),
167d61bd6e7STrond Norbye     ENGINE_NO_BUCKET = int(cb::engine_errc::no_bucket),
168d61bd6e7STrond Norbye     ENGINE_EBUSY = int(cb::engine_errc::too_busy),
169d61bd6e7STrond Norbye     ENGINE_AUTH_STALE = int(cb::engine_errc::authentication_stale),
170d61bd6e7STrond Norbye     ENGINE_DELTA_BADVAL = int(cb::engine_errc::delta_badval),
171463b4257STrond Norbye     ENGINE_LOCKED = int(cb::engine_errc::locked),
172463b4257STrond Norbye     ENGINE_LOCKED_TMPFAIL = int(cb::engine_errc::locked_tmpfail),
173e9b529a1SJim Walker     ENGINE_UNKNOWN_COLLECTION = int(cb::engine_errc::unknown_collection),
1748f9005d0SJim Walker     ENGINE_FAILED = int(cb::engine_errc::failed),
1758f9005d0SJim Walker     ENGINE_PREDICATE_FAILED = int(cb::engine_errc::predicate_failed)
176d61bd6e7STrond Norbye } ENGINE_ERROR_CODE;
177d61bd6e7STrond Norbye 
178d61bd6e7STrond Norbye namespace std {
179d61bd6e7STrond Norbye 
180d61bd6e7STrond Norbye template <>
181d61bd6e7STrond Norbye struct is_error_condition_enum<cb::engine_errc> : public true_type { };
182d61bd6e7STrond Norbye 
183d61bd6e7STrond Norbye }
184c39d5104SDave Rigby 
185c39d5104SDave Rigby namespace cb {
186c39d5104SDave Rigby // Backward compatibility - convert ENGINE_ERROR_CODE to engine_errc.
188c39d5104SDave Rigby cb::engine_errc to_engine_errc(ENGINE_ERROR_CODE eec);
189c39d5104SDave Rigby }