aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/libpqxx/include/pqxx/except.hxx
blob: 24d52d13e3d441acd6e8c4bc85780b8a72685825 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/** Definition of libpqxx exception classes.
 *
 * pqxx::sql_error, pqxx::broken_connection, pqxx::in_doubt_error, ...
 *
 * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/except instead.
 *
 * Copyright (c) 2000-2019, Jeroen T. Vermeulen.
 *
 * See COPYING for copyright license.  If you did not receive a file called
 * COPYING with this source code, please notify the distributor of this mistake,
 * or contact the author.
 */
#ifndef PQXX_H_EXCEPT
#define PQXX_H_EXCEPT

#include "pqxx/compiler-public.hxx"
#include "pqxx/compiler-internal-pre.hxx"

#include <stdexcept>

#include "pqxx/util.hxx"


namespace pqxx
{

/**
 * @addtogroup exception Exception classes
 *
 * These exception classes follow, roughly, the two-level hierarchy defined by
 * the PostgreSQL error codes (see Appendix A of the PostgreSQL documentation
 * corresponding to your server version).  The hierarchy given here is, as yet,
 * not a complete mirror of the error codes.  There are some other differences
 * as well, e.g. the error code statement_completion_unknown has a separate
 * status in libpqxx as in_doubt_error, and too_many_connections is classified
 * as a broken_connection rather than a subtype of insufficient_resources.
 *
 * @see http://www.postgresql.org/docs/9.4/interactive/errcodes-appendix.html
 *
 * @{
 */

/// Mixin base class to identify libpqxx-specific exception types
/**
 * If you wish to catch all exception types specific to libpqxx for some reason,
 * catch this type.  All of libpqxx's exception classes are derived from it
 * through multiple-inheritance (they also fit into the standard library's
 * exception hierarchy in more fitting places).
 *
 * This class is not derived from std::exception, since that could easily lead
 * to exception classes with multiple std::exception base-class objects.  As
 * Bart Samwel points out, "catch" is subject to some nasty fineprint in such
 * cases.
 */
class PQXX_LIBEXPORT PQXX_NOVTABLE pqxx_exception
{
public:
  /// Support run-time polymorphism, and keep this class abstract
  virtual ~pqxx_exception() noexcept =0;

  /// Return std::exception base-class object
  /** Use this to get at the exception's what() function, or to downcast to a
   * more specific type using dynamic_cast.
   *
   * Casting directly from pqxx_exception to a specific exception type is not
   * likely to work since pqxx_exception is not (and could not safely be)
   * derived from std::exception.
   *
   * For example, to test dynamically whether an exception is an sql_error:
   *
   * @code
   * try
   * {
   *   // ...
   * }
   * catch (const pqxx::pqxx_exception &e)
   * {
   *   std::cerr << e.base().what() << std::endl;
   *   const pqxx::sql_error *s=dynamic_cast<const pqxx::sql_error*>(&e.base());
   *   if (s) std::cerr << "Query was: " << s->query() << std::endl;
   * }
   * @endcode
   */
  PQXX_CONST virtual const std::exception &base() const noexcept =0;	//[t00]
};


/// Run-time failure encountered by libpqxx, similar to std::runtime_error
class PQXX_LIBEXPORT failure :
  public pqxx_exception, public std::runtime_error
{
  virtual const std::exception &base() const noexcept override
	{ return *this; }
public:
  explicit failure(const std::string &);
};


/// Exception class for lost or failed backend connection.
/**
 * @warning When this happens on Unix-like systems, you may also get a SIGPIPE
 * signal.  That signal aborts the program by default, so if you wish to be able
 * to continue after a connection breaks, be sure to disarm this signal.
 *
 * If you're working on a Unix-like system, see the manual page for
 * @c signal (2) on how to deal with SIGPIPE.  The easiest way to make this
 * signal harmless is to make your program ignore it:
 *
 * @code
 * #include <signal.h>
 *
 * int main()
 * {
 *   signal(SIGPIPE, SIG_IGN);
 *   // ...
 * @endcode
 */
class PQXX_LIBEXPORT broken_connection : public failure
{
public:
  broken_connection();
  explicit broken_connection(const std::string &);
};


/// Exception class for failed queries.
/** Carries, in addition to a regular error message, a copy of the failed query
 * and (if available) the SQLSTATE value accompanying the error.
 */
class PQXX_LIBEXPORT sql_error : public failure
{
  /// Query string.  Empty if unknown.
  const std::string m_query;
  /// SQLSTATE string describing the error type, if known; or empty string.
  const std::string m_sqlstate;

public:
  explicit sql_error(
	const std::string &msg="",
	const std::string &Q="",
	const char sqlstate[]=nullptr);
  virtual ~sql_error() noexcept;

  /// The query whose execution triggered the exception
  PQXX_PURE const std::string &query() const noexcept;			//[t56]

  /// SQLSTATE error code if known, or empty string otherwise.
  PQXX_PURE const std::string &sqlstate() const noexcept;
};


/// "Help, I don't know whether transaction was committed successfully!"
/** Exception that might be thrown in rare cases where the connection to the
 * database is lost while finishing a database transaction, and there's no way
 * of telling whether it was actually executed by the backend.  In this case
 * the database is left in an indeterminate (but consistent) state, and only
 * manual inspection will tell which is the case.
 */
class PQXX_LIBEXPORT in_doubt_error : public failure
{
public:
  explicit in_doubt_error(const std::string &);
};


/// The backend saw itself forced to roll back the ongoing transaction.
class PQXX_LIBEXPORT transaction_rollback : public failure
{
public:
  explicit transaction_rollback(const std::string &);
};


/// Transaction failed to serialize.  Please retry it.
/** Can only happen at transaction isolation levels REPEATABLE READ and
 * SERIALIZABLE.
 *
 * The current transaction cannot be committed without violating the guarantees
 * made by its isolation level.  This is the effect of a conflict with another
 * ongoing transaction.  The transaction may still succeed if you try to
 * perform it again.
 */
class PQXX_LIBEXPORT serialization_failure : public transaction_rollback
{
public:
  explicit serialization_failure(const std::string &);
};


/// We can't tell whether our last statement succeeded.
class PQXX_LIBEXPORT statement_completion_unknown : public transaction_rollback
{
public:
  explicit statement_completion_unknown(const std::string &);
};


/// The ongoing transaction has deadlocked.  Retrying it may help.
class PQXX_LIBEXPORT deadlock_detected : public transaction_rollback
{
public:
  explicit deadlock_detected(const std::string &);
};


/// Internal error in libpqxx library
class PQXX_LIBEXPORT internal_error :
  public pqxx_exception, public std::logic_error
{
  virtual const std::exception &base() const noexcept override
	{ return *this; }
public:
  explicit internal_error(const std::string &);
};


/// Error in usage of libpqxx library, similar to std::logic_error
class PQXX_LIBEXPORT usage_error :
  public pqxx_exception, public std::logic_error
{
  virtual const std::exception &base() const noexcept override
	{ return *this; }
public:
  explicit usage_error(const std::string &);
};


/// Invalid argument passed to libpqxx, similar to std::invalid_argument
class PQXX_LIBEXPORT argument_error :
  public pqxx_exception, public std::invalid_argument
{
  virtual const std::exception &base() const noexcept override
	{ return *this; }
public:
  explicit argument_error(const std::string &);
};


/// Value conversion failed, e.g. when converting "Hello" to int.
class PQXX_LIBEXPORT conversion_error :
  public pqxx_exception, public std::domain_error
{
  virtual const std::exception &base() const noexcept override
	{ return *this; }
public:
  explicit conversion_error(const std::string &);
};


/// Something is out of range, similar to std::out_of_range
class PQXX_LIBEXPORT range_error :
  public pqxx_exception, public std::out_of_range
{
  virtual const std::exception &base() const noexcept override
	{ return *this; }
public:
  explicit range_error(const std::string &);
};


/// Query returned an unexpected number of rows.
class PQXX_LIBEXPORT unexpected_rows : public range_error
{
  virtual const std::exception &base() const noexcept override
	{ return *this; }
public:
  explicit unexpected_rows(const std::string &msg) : range_error{msg} {}
};


/// Database feature not supported in current setup
class PQXX_LIBEXPORT feature_not_supported : public sql_error
{
public:
  explicit feature_not_supported(
	const std::string &err,
	const std::string &Q="",
	const char sqlstate[]=nullptr) :
    sql_error{err, Q, sqlstate} {}
};

/// Error in data provided to SQL statement
class PQXX_LIBEXPORT data_exception : public sql_error
{
public:
  explicit data_exception(
	const std::string &err,
	const std::string &Q="",
	const char sqlstate[]=nullptr) :
    sql_error{err, Q, sqlstate} {}
};

class PQXX_LIBEXPORT integrity_constraint_violation : public sql_error
{
public:
  explicit integrity_constraint_violation(
	const std::string &err,
	const std::string &Q="",
	const char sqlstate[]=nullptr) :
    sql_error{err, Q, sqlstate} {}
};

class PQXX_LIBEXPORT restrict_violation :
  public integrity_constraint_violation
{
public:
  explicit restrict_violation(
	const std::string &err,
	const std::string &Q="",
	const char sqlstate[]=nullptr) :
    integrity_constraint_violation{err, Q, sqlstate} {}
};

class PQXX_LIBEXPORT not_null_violation :
  public integrity_constraint_violation
{
public:
  explicit not_null_violation(
	const std::string &err,
	const std::string &Q="",
	const char sqlstate[]=nullptr) :
    integrity_constraint_violation{err, Q, sqlstate} {}
};

class PQXX_LIBEXPORT foreign_key_violation :
  public integrity_constraint_violation
{
public:
  explicit foreign_key_violation(
	const std::string &err,
	const std::string &Q="",
	const char sqlstate[]=nullptr) :
    integrity_constraint_violation{err, Q, sqlstate} {}
};

class PQXX_LIBEXPORT unique_violation :
  public integrity_constraint_violation
{
public:
  explicit unique_violation(
	const std::string &err,
	const std::string &Q="",
	const char sqlstate[]=nullptr) :
    integrity_constraint_violation{err, Q, sqlstate} {}
};

class PQXX_LIBEXPORT check_violation :
  public integrity_constraint_violation
{
public:
  explicit check_violation(
	const std::string &err,
	const std::string &Q="",
	const char sqlstate[]=nullptr) :
    integrity_constraint_violation{err, Q, sqlstate} {}
};

class PQXX_LIBEXPORT invalid_cursor_state : public sql_error
{
public:
  explicit invalid_cursor_state(
	const std::string &err,
	const std::string &Q="",
	const char sqlstate[]=nullptr) :
    sql_error{err, Q, sqlstate} {}
};

class PQXX_LIBEXPORT invalid_sql_statement_name : public sql_error
{
public:
  explicit invalid_sql_statement_name(
	const std::string &err,
	const std::string &Q="",
	const char sqlstate[]=nullptr) :
    sql_error{err, Q, sqlstate} {}
};

class PQXX_LIBEXPORT invalid_cursor_name : public sql_error
{
public:
  explicit invalid_cursor_name(
	const std::string &err,
	const std::string &Q="",
	const char sqlstate[]=nullptr) :
    sql_error{err, Q, sqlstate} {}
};

class PQXX_LIBEXPORT syntax_error : public sql_error
{
public:
  /// Approximate position in string where error occurred, or -1 if unknown.
  const int error_position;

  explicit syntax_error(
	const std::string &err,
	const std::string &Q="",
	const char sqlstate[]=nullptr,
	int pos=-1) :
    sql_error{err, Q, sqlstate}, error_position{pos} {}
};

class PQXX_LIBEXPORT undefined_column : public syntax_error
{
public:
  explicit undefined_column(
	const std::string &err,
	const std::string &Q="",
	const char sqlstate[]=nullptr) :
    syntax_error{err, Q, sqlstate} {}
};

class PQXX_LIBEXPORT undefined_function : public syntax_error
{
public:
  explicit undefined_function(
	const std::string &err,
	const std::string &Q="",
	const char sqlstate[]=nullptr) :
    syntax_error{err, Q, sqlstate} {}
};

class PQXX_LIBEXPORT undefined_table : public syntax_error
{
public:
  explicit undefined_table(
	const std::string &err,
	const std::string &Q="",
	const char sqlstate[]=nullptr) :
    syntax_error{err, Q, sqlstate} {}
};

class PQXX_LIBEXPORT insufficient_privilege : public sql_error
{
public:
  explicit insufficient_privilege(
	const std::string &err,
	const std::string &Q="",
	const char sqlstate[]=nullptr) :
    sql_error{err, Q, sqlstate} {}
};

/// Resource shortage on the server
class PQXX_LIBEXPORT insufficient_resources : public sql_error
{
public:
  explicit insufficient_resources(
	const std::string &err,
	const std::string &Q="",
	const char sqlstate[]=nullptr) :
    sql_error{err,Q, sqlstate} {}
};

class PQXX_LIBEXPORT disk_full : public insufficient_resources
{
public:
  explicit disk_full(
	const std::string &err,
	const std::string &Q="",
	const char sqlstate[]=nullptr) :
    insufficient_resources{err, Q, sqlstate} {}
};

class PQXX_LIBEXPORT out_of_memory : public insufficient_resources
{
public:
  explicit out_of_memory(
	const std::string &err,
	const std::string &Q="",
	const char sqlstate[]=nullptr) :
    insufficient_resources{err, Q, sqlstate} {}
};

class PQXX_LIBEXPORT too_many_connections : public broken_connection
{
public:
  explicit too_many_connections(const std::string &err) :
	broken_connection{err} {}
};

/// PL/pgSQL error
/** Exceptions derived from this class are errors from PL/pgSQL procedures.
 */
class PQXX_LIBEXPORT plpgsql_error : public sql_error
{
public:
  explicit plpgsql_error(
	const std::string &err,
	const std::string &Q="",
	const char sqlstate[]=nullptr) :
    sql_error{err, Q, sqlstate} {}
};

/// Exception raised in PL/pgSQL procedure
class PQXX_LIBEXPORT plpgsql_raise : public plpgsql_error
{
public:
  explicit plpgsql_raise(
	const std::string &err,
	const std::string &Q="",
	const char sqlstate[]=nullptr) :
    plpgsql_error{err, Q, sqlstate} {}
};

class PQXX_LIBEXPORT plpgsql_no_data_found : public plpgsql_error
{
public:
  explicit plpgsql_no_data_found(
	const std::string &err,
	const std::string &Q="",
	const char sqlstate[]=nullptr) :
    plpgsql_error{err, Q, sqlstate} {}
};

class PQXX_LIBEXPORT plpgsql_too_many_rows : public plpgsql_error
{
public:
  explicit plpgsql_too_many_rows(
	const std::string &err,
	const std::string &Q="",
	const char sqlstate[]=nullptr) :
    plpgsql_error{err, Q, sqlstate} {}
};

/**
 * @}
 */

}

#include "pqxx/compiler-internal-post.hxx"

#endif