6b28f75435e3ed65ac74bb41a273d55fdcddc33c
[ric-plt/sdl.git] / include / private / error.hpp
1 /*
2    Copyright (c) 2018-2019 Nokia.
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15 */
16
17 #ifndef SHAREDDATALAYER_ERROR_HPP_
18 #define SHAREDDATALAYER_ERROR_HPP_
19
20 #include <system_error>
21
22 /* Error codes defined here are for SDL internal usage only.
23  * Error codes meant to be used by SDL clients are defined in sdl/errorqueries.hpp.
24  *
25  * Error code handling in SDL goes following way:
26  *
27  * - Implementation sets so called implementation specific error codes (e.g AsyncRedisCommandDispatcherErrorCode
28  *   and AsyncRedisConnection::ErrorCode).
29  * - All implementation specific error codes have mapping to InternalErrors. This mapping is handled by overriding
30  *   std::error_category default_error_condition function in all implementation specific error code categories.
31  * - All InternalErrors are mapped to shareddatalayer::Errors which SDL client can conveniently use in
32  *   its error handling implemention. This mapping is handled by overriding std::error_category equivalent function in
33  *   SharedDataLayerErrorCategory.
34  * - Async API error codes have corresponding exceptions in sync API. When modifying other one (async API error codes or
35  *   sync API exceptions) check the need for similar modification to other one also.
36  *
37  * When adding a new implementation specific error code, do also needed updates to mappings described above.
38  * At least mapping from new implementation specific error code error code to InternalError is always needed.
39  */
40
41 namespace shareddatalayer
42 {
43     enum class InternalError
44     {
45         SUCCESS = 0,
46         SDL_NOT_READY,
47         SDL_NOT_CONNECTED_TO_BACKEND,
48         SDL_ERROR_CODE_LOGIC_ERROR,
49         SDL_RECEIVED_INVALID_PARAMETER,
50         BACKEND_CONNECTION_LOST,
51         BACKEND_NOT_READY,
52         BACKEND_REJECTED_REQUEST,
53         BACKEND_ERROR
54     };
55
56     std::error_condition make_error_condition(shareddatalayer::InternalError ec);
57
58     namespace redis
59     {
60         enum class AsyncRedisCommandDispatcherErrorCode
61         {
62             SUCCESS = 0,
63             UNKNOWN_ERROR,
64             CONNECTION_LOST,
65             PROTOCOL_ERROR,
66             OUT_OF_MEMORY,
67             DATASET_LOADING,
68             NOT_CONNECTED,
69             IO_ERROR,
70             //Keep this always as last item. Used in unit tests to loop all enum values.
71             END_MARKER
72         };
73
74         std::error_code make_error_code(AsyncRedisCommandDispatcherErrorCode errorCode);
75         AsyncRedisCommandDispatcherErrorCode& operator++ (AsyncRedisCommandDispatcherErrorCode& ecEnum);
76     }
77 }
78
79 namespace std
80 {
81     template <>
82     struct is_error_condition_enum<shareddatalayer::InternalError> : public true_type { };
83
84     template <>
85     struct is_error_code_enum<shareddatalayer::redis::AsyncRedisCommandDispatcherErrorCode>: public true_type { };
86 }
87
88 #endif