drogon
C++14/17-based HTTP application framework
Loading...
Searching...
No Matches
Exception.h
Go to the documentation of this file.
1
9
10// taken from libpqxx and modified
11
25
26#pragma once
27
28#include <drogon/exports.h>
29#include <functional>
30#include <stdexcept>
31#include <string>
32
33namespace drogon
34{
35namespace orm
36{
38
50class DROGON_EXPORT DrogonDbException
51{
52 public:
54 virtual ~DrogonDbException() noexcept
55 {
56 }
57
59
83 virtual const std::exception &base() const noexcept
84 {
85 static std::exception except;
86 return except;
87 } //[t00]
88};
89
92class Failure : public DrogonDbException, public std::runtime_error
93{
94 const std::exception &base() const noexcept override
95 {
96 return *this;
97 }
98
99 public:
100 DROGON_EXPORT explicit Failure(const std::string &);
101};
102
104
122class BrokenConnection : public Failure
123{
124 public:
125 DROGON_EXPORT BrokenConnection();
126 DROGON_EXPORT explicit BrokenConnection(const std::string &);
127};
128
130
133class SqlError : public Failure
134{
136 const std::string query_;
138 const std::string sqlState_;
139
140 const int errcode_{0};
141 const int extendedErrcode_{0};
142
143 public:
144 DROGON_EXPORT explicit SqlError(const std::string &msg = "",
145 const std::string &Q = "",
146 const char sqlstate[] = nullptr);
147 DROGON_EXPORT explicit SqlError(const std::string &msg,
148 const std::string &Q,
149 const int errcode,
150 const int extendedErrcode);
151 DROGON_EXPORT virtual ~SqlError() noexcept;
152
154 DROGON_EXPORT const std::string &query() const noexcept;
155 DROGON_EXPORT const std::string &sqlState() const noexcept;
156 DROGON_EXPORT int errcode() const noexcept;
157 DROGON_EXPORT int extendedErrcode() const noexcept;
158};
159
161
167class InDoubtError : public Failure
168{
169 public:
170 DROGON_EXPORT explicit InDoubtError(const std::string &);
171};
172
174class TransactionRollback : public Failure
175{
176 public:
177 DROGON_EXPORT explicit TransactionRollback(const std::string &);
178};
179
181
189class SerializationFailure : public TransactionRollback
190{
191 public:
192 DROGON_EXPORT explicit SerializationFailure(const std::string &);
193};
194
196class StatementCompletionUnknown : public TransactionRollback
197{
198 public:
199 DROGON_EXPORT explicit StatementCompletionUnknown(const std::string &);
200};
201
203class DeadlockDetected : public TransactionRollback
204{
205 public:
206 DROGON_EXPORT explicit DeadlockDetected(const std::string &);
207};
208
210class InternalError : public DrogonDbException, public std::logic_error
211{
212 const std::exception &base() const noexcept override
213 {
214 return *this;
215 }
216
217 public:
218 DROGON_EXPORT explicit InternalError(const std::string &);
219};
220
222class TimeoutError : public DrogonDbException, public std::logic_error
223{
224 const std::exception &base() const noexcept override
225 {
226 return *this;
227 }
228
229 public:
230 DROGON_EXPORT explicit TimeoutError(const std::string &);
231};
232
234class UsageError : public DrogonDbException, public std::logic_error
235{
236 const std::exception &base() const noexcept override
237 {
238 return *this;
239 }
240
241 public:
242 DROGON_EXPORT explicit UsageError(const std::string &);
243};
244
246class ArgumentError : public DrogonDbException, public std::invalid_argument
247{
248 const std::exception &base() const noexcept override
249 {
250 return *this;
251 }
252
253 public:
254 DROGON_EXPORT explicit ArgumentError(const std::string &);
255};
256
258class ConversionError : public DrogonDbException, public std::domain_error
259{
260 const std::exception &base() const noexcept override
261 {
262 return *this;
263 }
264
265 public:
266 DROGON_EXPORT explicit ConversionError(const std::string &);
267};
268
270class RangeError : public DrogonDbException, public std::out_of_range
271{
272 const std::exception &base() const noexcept override
273 {
274 return *this;
275 }
276
277 public:
278 DROGON_EXPORT explicit RangeError(const std::string &);
279};
280
282class UnexpectedRows : public RangeError
283{
284 const std::exception &base() const noexcept override
285 {
286 return *this;
287 }
288
289 public:
290 explicit UnexpectedRows(const std::string &msg) : RangeError(msg)
291 {
292 }
293};
294
296class FeatureNotSupported : public SqlError
297{
298 public:
299 explicit FeatureNotSupported(const std::string &err,
300 const std::string &Q = "",
301 const char sqlstate[] = nullptr)
302 : SqlError(err, Q, sqlstate)
303 {
304 }
305
306 explicit FeatureNotSupported(const std::string &msg,
307 const std::string &Q,
308 const int errcode,
309 const int extendedErrcode)
310 : SqlError(msg, Q, errcode, extendedErrcode)
311 {
312 }
313};
314
316class DataException : public SqlError
317{
318 public:
319 explicit DataException(const std::string &err,
320 const std::string &Q = "",
321 const char sqlstate[] = nullptr)
322 : SqlError(err, Q, sqlstate)
323 {
324 }
325
326 explicit DataException(const std::string &msg,
327 const std::string &Q,
328 const int errcode,
329 const int extendedErrcode)
330 : SqlError(msg, Q, errcode, extendedErrcode)
331 {
332 }
333};
334
335class IntegrityConstraintViolation : public SqlError
336{
337 public:
338 explicit IntegrityConstraintViolation(const std::string &err,
339 const std::string &Q = "",
340 const char sqlstate[] = nullptr)
341 : SqlError(err, Q, sqlstate)
342 {
343 }
344
345 explicit IntegrityConstraintViolation(const std::string &msg,
346 const std::string &Q,
347 const int errcode,
348 const int extendedErrcode)
349 : SqlError(msg, Q, errcode, extendedErrcode)
350 {
351 }
352};
353
354class RestrictViolation : public IntegrityConstraintViolation
355{
356 public:
357 explicit RestrictViolation(const std::string &err,
358 const std::string &Q = "",
359 const char sqlstate[] = nullptr)
360 : IntegrityConstraintViolation(err, Q, sqlstate)
361 {
362 }
363
364 explicit RestrictViolation(const std::string &msg,
365 const std::string &Q,
366 const int errcode,
367 const int extendedErrcode)
368 : IntegrityConstraintViolation(msg, Q, errcode, extendedErrcode)
369 {
370 }
371};
372
373class NotNullViolation : public IntegrityConstraintViolation
374{
375 public:
376 explicit NotNullViolation(const std::string &err,
377 const std::string &Q = "",
378 const char sqlstate[] = nullptr)
379 : IntegrityConstraintViolation(err, Q, sqlstate)
380 {
381 }
382
383 explicit NotNullViolation(const std::string &msg,
384 const std::string &Q,
385 const int errcode,
386 const int extendedErrcode)
387 : IntegrityConstraintViolation(msg, Q, errcode, extendedErrcode)
388 {
389 }
390};
391
392class ForeignKeyViolation : public IntegrityConstraintViolation
393{
394 public:
395 explicit ForeignKeyViolation(const std::string &err,
396 const std::string &Q = "",
397 const char sqlstate[] = nullptr)
398 : IntegrityConstraintViolation(err, Q, sqlstate)
399 {
400 }
401
402 explicit ForeignKeyViolation(const std::string &msg,
403 const std::string &Q,
404 const int errcode,
405 const int extendedErrcode)
406 : IntegrityConstraintViolation(msg, Q, errcode, extendedErrcode)
407 {
408 }
409};
410
411class UniqueViolation : public IntegrityConstraintViolation
412{
413 public:
414 explicit UniqueViolation(const std::string &err,
415 const std::string &Q = "",
416 const char sqlstate[] = nullptr)
417 : IntegrityConstraintViolation(err, Q, sqlstate)
418 {
419 }
420
421 explicit UniqueViolation(const std::string &msg,
422 const std::string &Q,
423 const int errcode,
424 const int extendedErrcode)
425 : IntegrityConstraintViolation(msg, Q, errcode, extendedErrcode)
426 {
427 }
428};
429
430class CheckViolation : public IntegrityConstraintViolation
431{
432 public:
433 explicit CheckViolation(const std::string &err,
434 const std::string &Q = "",
435 const char sqlstate[] = nullptr)
436 : IntegrityConstraintViolation(err, Q, sqlstate)
437 {
438 }
439
440 explicit CheckViolation(const std::string &msg,
441 const std::string &Q,
442 const int errcode,
443 const int extendedErrcode)
444 : IntegrityConstraintViolation(msg, Q, errcode, extendedErrcode)
445 {
446 }
447};
448
449class InvalidCursorState : public SqlError
450{
451 public:
452 explicit InvalidCursorState(const std::string &err,
453 const std::string &Q = "",
454 const char sqlstate[] = nullptr)
455 : SqlError(err, Q, sqlstate)
456 {
457 }
458
459 explicit InvalidCursorState(const std::string &msg,
460 const std::string &Q,
461 const int errcode,
462 const int extendedErrcode)
463 : SqlError(msg, Q, errcode, extendedErrcode)
464 {
465 }
466};
467
468class InvalidSqlStatementName : public SqlError
469{
470 public:
471 explicit InvalidSqlStatementName(const std::string &err,
472 const std::string &Q = "",
473 const char sqlstate[] = nullptr)
474 : SqlError(err, Q, sqlstate)
475 {
476 }
477
478 explicit InvalidSqlStatementName(const std::string &msg,
479 const std::string &Q,
480 const int errcode,
481 const int extendedErrcode)
482 : SqlError(msg, Q, errcode, extendedErrcode)
483 {
484 }
485};
486
487class InvalidCursorName : public SqlError
488{
489 public:
490 explicit InvalidCursorName(const std::string &err,
491 const std::string &Q = "",
492 const char sqlstate[] = nullptr)
493 : SqlError(err, Q, sqlstate)
494 {
495 }
496
497 explicit InvalidCursorName(const std::string &msg,
498 const std::string &Q,
499 const int errcode,
500 const int extendedErrcode)
501 : SqlError(msg, Q, errcode, extendedErrcode)
502 {
503 }
504};
505
506class SyntaxError : public SqlError
507{
508 public:
510 const int errorPosition_;
511
512 explicit SyntaxError(const std::string &err,
513 const std::string &Q = "",
514 const char sqlstate[] = nullptr,
515 int pos = -1)
516 : SqlError(err, Q, sqlstate), errorPosition_(pos)
517 {
518 }
519
520 explicit SyntaxError(const std::string &msg,
521 const std::string &Q,
522 const int errcode,
523 const int extendedErrcode,
524 int pos = -1)
525 : SqlError(msg, Q, errcode, extendedErrcode), errorPosition_(pos)
526 {
527 }
528};
529
530class UndefinedColumn : public SyntaxError
531{
532 public:
533 explicit UndefinedColumn(const std::string &err,
534 const std::string &Q = "",
535 const char sqlstate[] = nullptr)
536 : SyntaxError(err, Q, sqlstate)
537 {
538 }
539
540 explicit UndefinedColumn(const std::string &msg,
541 const std::string &Q,
542 const int errcode,
543 const int extendedErrcode)
544 : SyntaxError(msg, Q, errcode, extendedErrcode)
545 {
546 }
547};
548
549class UndefinedFunction : public SyntaxError
550{
551 public:
552 explicit UndefinedFunction(const std::string &err,
553 const std::string &Q = "",
554 const char sqlstate[] = nullptr)
555 : SyntaxError(err, Q, sqlstate)
556 {
557 }
558
559 explicit UndefinedFunction(const std::string &msg,
560 const std::string &Q,
561 const int errcode,
562 const int extendedErrcode)
563 : SyntaxError(msg, Q, errcode, extendedErrcode)
564 {
565 }
566};
567
568class UndefinedTable : public SyntaxError
569{
570 public:
571 explicit UndefinedTable(const std::string &err,
572 const std::string &Q = "",
573 const char sqlstate[] = nullptr)
574 : SyntaxError(err, Q, sqlstate)
575 {
576 }
577
578 explicit UndefinedTable(const std::string &msg,
579 const std::string &Q,
580 const int errcode,
581 const int extendedErrcode)
582 : SyntaxError(msg, Q, errcode, extendedErrcode)
583 {
584 }
585};
586
587class InsufficientPrivilege : public SqlError
588{
589 public:
590 explicit InsufficientPrivilege(const std::string &err,
591 const std::string &Q = "",
592 const char sqlstate[] = nullptr)
593 : SqlError(err, Q, sqlstate)
594 {
595 }
596
597 explicit InsufficientPrivilege(const std::string &msg,
598 const std::string &Q,
599 const int errcode,
600 const int extendedErrcode)
601 : SqlError(msg, Q, errcode, extendedErrcode)
602 {
603 }
604};
605
607class InsufficientResources : public SqlError
608{
609 public:
610 explicit InsufficientResources(const std::string &err,
611 const std::string &Q = "",
612 const char sqlstate[] = nullptr)
613 : SqlError(err, Q, sqlstate)
614 {
615 }
616
617 explicit InsufficientResources(const std::string &msg,
618 const std::string &Q,
619 const int errcode,
620 const int extendedErrcode)
621 : SqlError(msg, Q, errcode, extendedErrcode)
622 {
623 }
624};
625
626class DiskFull : public InsufficientResources
627{
628 public:
629 explicit DiskFull(const std::string &err,
630 const std::string &Q = "",
631 const char sqlstate[] = nullptr)
632 : InsufficientResources(err, Q, sqlstate)
633 {
634 }
635
636 explicit DiskFull(const std::string &msg,
637 const std::string &Q,
638 const int errcode,
639 const int extendedErrcode)
640 : InsufficientResources(msg, Q, errcode, extendedErrcode)
641 {
642 }
643};
644
645class OutOfMemory : public InsufficientResources
646{
647 public:
648 explicit OutOfMemory(const std::string &err,
649 const std::string &Q = "",
650 const char sqlstate[] = nullptr)
651 : InsufficientResources(err, Q, sqlstate)
652 {
653 }
654
655 explicit OutOfMemory(const std::string &msg,
656 const std::string &Q,
657 const int errcode,
658 const int extendedErrcode)
659 : InsufficientResources(msg, Q, errcode, extendedErrcode)
660 {
661 }
662};
663
664class TooManyConnections : public BrokenConnection
665{
666 public:
667 explicit TooManyConnections(const std::string &err) : BrokenConnection(err)
668 {
669 }
670};
671
672// /// PL/pgSQL error
673// /** Exceptions derived from this class are errors from PL/pgSQL procedures.
674// */
675// class PQXX_LIBEXPORT plpgsql_error : public sql_error
676// {
677// public:
678// explicit plpgsql_error(
679// const std::string &err,
680// const std::string &Q = "",
681// const char sqlstate[] = nullptr) : sql_error(err, Q, sqlstate) {}
682// };
683
684// /// Exception raised in PL/pgSQL procedure
685// class PQXX_LIBEXPORT plpgsql_raise : public plpgsql_error
686// {
687// public:
688// explicit plpgsql_raise(
689// const std::string &err,
690// const std::string &Q = "",
691// const char sqlstate[] = nullptr) : plpgsql_error(err, Q, sqlstate) {}
692// };
693
694// class PQXX_LIBEXPORT plpgsql_no_data_found : public plpgsql_error
695// {
696// public:
697// explicit plpgsql_no_data_found(
698// const std::string &err,
699// const std::string &Q = "",
700// const char sqlstate[] = nullptr) : plpgsql_error(err, Q, sqlstate) {}
701// };
702
703// class PQXX_LIBEXPORT plpgsql_too_many_rows : public plpgsql_error
704// {
705// public:
706// explicit plpgsql_too_many_rows(
707// const std::string &err,
708// const std::string &Q = "",
709// const char sqlstate[] = nullptr) : plpgsql_error(err, Q, sqlstate) {}
710// };
712 std::function<void(const DrogonDbException &)>;
713} // namespace orm
714} // namespace drogon
std::function< void(const DrogonDbException &)> DrogonDbExceptionCallback
Definition Exception.h:711
Mixin base class to identify drogon-db-specific exception types.
Definition Exception.h:51
virtual const std::exception & base() const noexcept
Return std::exception base-class object.
Definition Exception.h:83
virtual ~DrogonDbException() noexcept
Support run-time polymorphism, and keep this class abstract.
Definition Exception.h:54
Exception class for failed queries.
Definition Exception.h:134
DROGON_EXPORT const std::string & query() const noexcept
The query whose execution triggered the exception.
Definition Exception.h:507
const int errorPosition_
Approximate position in string where error occurred, or -1 if unknown.
Definition Exception.h:510
Drogon Test is a minimal effort test framework developed because the major C++ test frameworks doesn'...
Definition Attribute.h:23
STL namespace.