CPPKiteConnect
exceptions.hpp
1 /*
2  * Licensed under the MIT License <http://opensource.org/licenses/MIT>.
3  * SPDX-License-Identifier: MIT
4  *
5  * Copyright (c) 2020-2022 Bhumit Attarde
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
20  * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
22  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
23  * USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 
26 #pragma once
27 
28 #include <exception>
29 #include <string>
30 #include <utility>
31 
32 namespace kiteconnect {
33 
34 using std::string;
35 
50 class kiteppException : public std::exception {
51 
52  public:
53  kiteppException(int Code_, string Message_)
54  : Code(Code_), Message(std::move(Message_)) {};
55 
61  [[nodiscard]] const char* what() const noexcept override = 0;
62 
68  [[nodiscard]] int code() const noexcept { return Code; };
69 
75  [[nodiscard]] const char* message() const noexcept {
76  return Message.c_str();
77  };
78 
79  private:
80  int Code = 0;
81  string Message;
82 };
83 
86 
87  public:
88  tokenException(int code, string message)
89  : kiteppException(code, std::move(message)) {};
90 
92  [[nodiscard]] const char* what() const noexcept override {
93  return "TokenException was thrown by Kite";
94  };
95 };
96 
99 
100  public:
101  userException(int code, string message)
102  : kiteppException(code, std::move(message)) {};
103 
105  [[nodiscard]] const char* what() const noexcept override {
106  return "UserException was thrown by Kite";
107  };
108 };
109 
112 
113  public:
114  orderException(int code, string message)
115  : kiteppException(code, std::move(message)) {};
116 
118  [[nodiscard]] const char* what() const noexcept override {
119  return "OrderException was thrown by Kite";
120  };
121 };
122 
125 
126  public:
127  inputException(int code, string message)
128  : kiteppException(code, std::move(message)) {};
129 
131  [[nodiscard]] const char* what() const noexcept override {
132  return "InputException was thrown by Kite";
133  };
134 };
135 
141 
142  public:
143  networkException(int code, string message)
144  : kiteppException(code, std::move(message)) {};
145 
147  [[nodiscard]] const char* what() const noexcept override {
148  return "NetworkException was thrown by Kite";
149  };
150 };
151 
154 
155  public:
156  dataException(int code, string message)
157  : kiteppException(code, std::move(message)) {};
158 
160  [[nodiscard]] const char* what() const noexcept override {
161  return "DataException was thrown by Kite";
162  };
163 };
164 
167 
168  public:
169  generalException(int code, string message)
170  : kiteppException(code, std::move(message)) {};
171 
173  [[nodiscard]] const char* what() const noexcept override {
174  return "GeneralException was thrown by Kite";
175  };
176 };
177 
180 
181  public:
182  permissionException(int code, string message)
183  : kiteppException(code, std::move(message)) {};
184 
186  [[nodiscard]] const char* what() const noexcept override {
187  return "PermissionException was thrown by Kite";
188  };
189 };
190 
194 
195  public:
196  unknownException(int code, string message)
197  : kiteppException(code, std::move(message)) {};
198 
200  [[nodiscard]] const char* what() const noexcept override {
201  return "unknown exception was thrown by Kite";
202  };
203 };
204 
206 class libException : public std::exception {
207 
208  public:
209  explicit libException(string Message): message(std::move(Message)) {};
210 
216  const char* what() { return message.c_str(); };
217 
218  private:
219  string message;
220 };
221 
222 namespace internal {
223 inline void throwException(
224  const string& exceptionString, int code, const string& msg) {
225  // exception strings sent by API
226  static const string tokenExceptionString = "TokenException";
227  static const string userExceptionString = "UserException";
228  static const string orderExcptionString = "OrderException";
229  static const string inputExceptionString = "InputException";
230  static const string networkExceptionString = "NetworkException";
231  static const string dataExceptionString = "DataException";
232  static const string generalExceptionString = "GeneralException";
233  static const string permissionExceptionString = "PermissionException";
234 
235  // when rest api doesn't send an exception but http status code isn't 200
236  // either
237  static const string unknownExceptionString = "NoException";
238 
239  if (exceptionString == tokenExceptionString) {
240  throw tokenException(code, msg);
241  };
242  if (exceptionString == userExceptionString) {
243  throw userException(code, msg);
244  };
245  if (exceptionString == orderExcptionString) {
246  throw orderException(code, msg);
247  };
248  if (exceptionString == inputExceptionString) {
249  throw inputException(code, msg);
250  };
251  if (exceptionString == networkExceptionString) {
252  throw networkException(code, msg);
253  };
254  if (exceptionString == dataExceptionString) {
255  throw dataException(code, msg);
256  };
257  if (exceptionString == generalExceptionString) {
258  throw generalException(code, msg);
259  };
260  if (exceptionString == permissionExceptionString) {
261  throw permissionException(code, msg);
262  };
263  if (exceptionString == unknownExceptionString) {
264  throw unknownException(code, msg);
265  };
266 
267  throw libException("unknown exception was thrown by REST API");
268 };
269 } // namespace internal
270 
271 } // namespace kiteconnect
kiteconnect::unknownException
This exception is thrown when REST API doesn't return a HTTP OK 200 code but doesn't return any excep...
Definition: exceptions.hpp:193
kiteconnect::userException
Represents user account related errors.
Definition: exceptions.hpp:98
kiteconnect::libException
This exception is thrown when an error occures at the library level.
Definition: exceptions.hpp:206
kiteconnect::kiteppException
CPPKiteConnect saves you the hassle of detecting API errors by manually checking HTTP codes or JSON e...
Definition: exceptions.hpp:50
kiteconnect::kiteppException::code
int code() const noexcept
Get HTTP code sent by the REST API.
Definition: exceptions.hpp:68
kiteconnect::tokenException
Represents all token and authentication related errors.
Definition: exceptions.hpp:85
kiteconnect::generalException::what
const char * what() const noexcept override
Provides short descripion of the error.
Definition: exceptions.hpp:173
kiteconnect::networkException
Represents a network issue between Kite and the backend Order Management System (OMS).
Definition: exceptions.hpp:140
kiteconnect::tokenException::what
const char * what() const noexcept override
Provides short descripion of the error.
Definition: exceptions.hpp:92
kiteconnect::unknownException::what
const char * what() const noexcept override
Provides short descripion of the error.
Definition: exceptions.hpp:200
kiteconnect::dataException::what
const char * what() const noexcept override
Provides short descripion of the error.
Definition: exceptions.hpp:160
kiteconnect::generalException
An unclassified, general error.
Definition: exceptions.hpp:166
kiteconnect::permissionException::what
const char * what() const noexcept override
Provides short descripion of the error.
Definition: exceptions.hpp:186
kiteconnect::inputException
Represents user input errors such as missing and invalid parameters.
Definition: exceptions.hpp:124
kiteconnect::kiteppException::message
const char * message() const noexcept
Get error message sent by the REST API.
Definition: exceptions.hpp:75
kiteconnect::orderException
Represents all order placement and manipulation errors.
Definition: exceptions.hpp:111
kiteconnect::kiteppException::what
const char * what() const noexcept override=0
Provides short descripion of the error.
kiteconnect::orderException::what
const char * what() const noexcept override
Provides short descripion of the error.
Definition: exceptions.hpp:118
kiteconnect::permissionException
Represents permission denied exceptions for certain calls.
Definition: exceptions.hpp:179
kiteconnect::libException::what
const char * what()
Provides short description of the error.
Definition: exceptions.hpp:216
kiteconnect::inputException::what
const char * what() const noexcept override
Provides short descripion of the error.
Definition: exceptions.hpp:131
kiteconnect::networkException::what
const char * what() const noexcept override
Provides short descripion of the error.
Definition: exceptions.hpp:147
kiteconnect::userException::what
const char * what() const noexcept override
Provides short descripion of the error.
Definition: exceptions.hpp:105
kiteconnect::dataException
Represents a bad response from the backend Order Management System.
Definition: exceptions.hpp:153