PageRenderTime 205ms CodeModel.GetById 121ms app.highlight 62ms RepoModel.GetById 18ms app.codeStats 1ms

/mordor/pq/exception.cpp

http://github.com/mozy/mordor
C++ | 278 lines | 269 code | 8 blank | 1 comment | 276 complexity | 1954552de754b0ddc6a4b94ae4d44c0f MD5 | raw file
  1// Copyright (c) 2010 - Mozy, Inc.
  2
  3#include "exception.h"
  4
  5#include "mordor/log.h"
  6
  7namespace Mordor {
  8namespace PQ {
  9
 10static Logger::ptr g_log = Log::lookup("mordor:pq");
 11
 12void throwException(PGconn *conn)
 13{
 14    const char *error = PQerrorMessage(conn);
 15    MORDOR_LOG_ERROR(g_log) << conn << " connection error: " << error;
 16    MORDOR_THROW_EXCEPTION(ConnectionException(error));
 17}
 18
 19void throwException(PGresult *result)
 20{
 21    std::string message = PQresultErrorMessage(result);
 22    const char *sqlstate = PQresultErrorField(result, PG_DIAG_SQLSTATE);
 23
 24    MORDOR_LOG_ERROR(g_log) << result << " " << sqlstate << ": " << message;
 25    if (!sqlstate || strlen(sqlstate) != 5)
 26        MORDOR_THROW_EXCEPTION(Exception(message));
 27    switch (sqlstate[0]) {
 28        case '2':
 29            switch (sqlstate[1]) {
 30                case '2':
 31                    if (strncmp(sqlstate + 2, "000", 3) == 0)
 32                        MORDOR_THROW_EXCEPTION(DataException(message));
 33                    else if (strncmp(sqlstate + 2, "02E", 3) == 0)
 34                        MORDOR_THROW_EXCEPTION(ArraySubscriptError(message));
 35                    else if (strncmp(sqlstate + 2, "021", 3) == 0)
 36                        MORDOR_THROW_EXCEPTION(CharacterNotInRepertoireException(message));
 37                    else if (strncmp(sqlstate + 2, "008", 3) == 0)
 38                        MORDOR_THROW_EXCEPTION(OverflowException(message));
 39                    else if (strncmp(sqlstate + 2, "012", 3) == 0)
 40                        MORDOR_THROW_EXCEPTION(DivisionByZeroException(message));
 41                    else if (strncmp(sqlstate + 2, "005", 3) == 0)
 42                        MORDOR_THROW_EXCEPTION(AssignmentError(message));
 43                    else if (strncmp(sqlstate + 2, "00B", 3) == 0)
 44                        MORDOR_THROW_EXCEPTION(EscapeCharacterConflictException(message));
 45                    else if (strncmp(sqlstate + 2, "01E", 3) == 0 ||
 46                        strncmp(sqlstate + 2, "01F", 3) == 0 ||
 47                        strncmp(sqlstate + 2, "01G", 3) == 0)
 48                        MORDOR_THROW_EXCEPTION(InvalidArgumentException(message));
 49                    else if (strncmp(sqlstate + 2, "004", 3) == 0)
 50                        MORDOR_THROW_EXCEPTION(NullValueNotAllowedException(message));
 51                    else
 52                        MORDOR_THROW_EXCEPTION(DataException(message));
 53                case '3':
 54                    if (strncmp(sqlstate + 2, "000", 3) == 0)
 55                        MORDOR_THROW_EXCEPTION(IntegrityConstraintViolationException(message));
 56                    else if (strncmp(sqlstate + 2, "001", 3) == 0)
 57                        MORDOR_THROW_EXCEPTION(RestrictViolationException(message));
 58                    else if (strncmp(sqlstate + 2, "502", 3) == 0)
 59                        MORDOR_THROW_EXCEPTION(NotNullViolationException(message));
 60                    else if (strncmp(sqlstate + 2, "503", 3) == 0)
 61                        MORDOR_THROW_EXCEPTION(ForeignKeyViolationException(message));
 62                    else if (strncmp(sqlstate + 2, "505", 3) == 0)
 63                        MORDOR_THROW_EXCEPTION(UniqueViolationException(message));
 64                    else if (strncmp(sqlstate + 2, "514", 3) == 0)
 65                        MORDOR_THROW_EXCEPTION(UniqueViolationException(message));
 66                    else
 67                        MORDOR_THROW_EXCEPTION(IntegrityConstraintViolationException(message));
 68                case '5':
 69                    if (strncmp(sqlstate + 2, "000", 3) == 0)
 70                        MORDOR_THROW_EXCEPTION(InvalidTransactionStateException(message));
 71                    else if (strncmp(sqlstate + 2, "001", 3) == 0)
 72                        MORDOR_THROW_EXCEPTION(ActiveTransactionException(message));
 73                    else if (strncmp(sqlstate + 2, "002", 3) == 0)
 74                        MORDOR_THROW_EXCEPTION(BranchTransactionAlreadyActiveException(message));
 75                    else if (strncmp(sqlstate + 2, "008", 3) == 0)
 76                        MORDOR_THROW_EXCEPTION(HeldCursorRequiresSameIsolationLevelException(message));
 77                    else if (strncmp(sqlstate + 2, "003", 3) == 0)
 78                        MORDOR_THROW_EXCEPTION(InappropriateAccessModeForBranchTransactionException(message));
 79                    else if (strncmp(sqlstate + 2, "004", 3) == 0)
 80                        MORDOR_THROW_EXCEPTION(InappropriateIsolationLevelForBranchTransactionException(message));
 81                    else if (strncmp(sqlstate + 2, "005", 3) == 0)
 82                        MORDOR_THROW_EXCEPTION(NoActiveTransactionForBranchException(message));
 83                    else if (strncmp(sqlstate + 2, "006", 3) == 0)
 84                        MORDOR_THROW_EXCEPTION(ReadOnlyTransactionException(message));
 85                    else if (strncmp(sqlstate + 2, "007", 3) == 0)
 86                        MORDOR_THROW_EXCEPTION(SchemaAndDataStatementMixingNotSupportedException(message));
 87                    else if (strncmp(sqlstate + 2, "P01", 3) == 0)
 88                        MORDOR_THROW_EXCEPTION(NoActiveTransactionException(message));
 89                    else if (strncmp(sqlstate + 2, "P02", 3) == 0)
 90                        MORDOR_THROW_EXCEPTION(InFailedTransactionException(message));
 91                    else
 92                        MORDOR_THROW_EXCEPTION(InvalidTransactionStateException(message));
 93                default:
 94                    break;
 95            }
 96        case '4':
 97            switch (sqlstate[1]) {
 98                case '0':
 99                    if (strncmp(sqlstate + 2, "000", 3) == 0)
100                        MORDOR_THROW_EXCEPTION(TransactionRollbackException(message));
101                    else if (strncmp(sqlstate + 2, "002", 3) == 0)
102                        MORDOR_THROW_EXCEPTION(TransactionIntegrityConstraintViolationException(message));
103                    else if (strncmp(sqlstate + 2, "001", 3) == 0)
104                        MORDOR_THROW_EXCEPTION(SerializationFailureException(message));
105                    else if (strncmp(sqlstate + 2, "003", 3) == 0)
106                        MORDOR_THROW_EXCEPTION(StatementCompletionUnknownException(message));
107                    else if (strncmp(sqlstate + 2, "P01", 3) == 0)
108                        MORDOR_THROW_EXCEPTION(DeadlockDetectedException(message));
109                    else
110                        MORDOR_THROW_EXCEPTION(TransactionRollbackException(message));
111                case '2':
112                    if (strncmp(sqlstate + 2, "000", 3) == 0)
113                        MORDOR_THROW_EXCEPTION(AccessRuleViolationException(message));
114                    else if (strncmp(sqlstate + 2, "601", 3) == 0)
115                        MORDOR_THROW_EXCEPTION(SyntaxError(message));
116                    else if (strncmp(sqlstate + 2, "501", 3) == 0)
117                        MORDOR_THROW_EXCEPTION(InsufficientPrivilegeException(message));
118                    else if (strncmp(sqlstate + 2, "846", 3) == 0)
119                        MORDOR_THROW_EXCEPTION(CannotCoerceException(message));
120                    else if (strncmp(sqlstate + 2, "803", 3) == 0)
121                        MORDOR_THROW_EXCEPTION(GroupingError(message));
122                    else if (strncmp(sqlstate + 2, "P20", 3) == 0)
123                        MORDOR_THROW_EXCEPTION(WindowingError(message));
124                    else if (strncmp(sqlstate + 2, "P19", 3) == 0)
125                        MORDOR_THROW_EXCEPTION(InvalidRecursionException(message));
126                    else if (strncmp(sqlstate + 2, "830", 3) == 0)
127                        MORDOR_THROW_EXCEPTION(InvalidForeignKeyException(message));
128                    else if (strncmp(sqlstate + 2, "602", 3) == 0)
129                        MORDOR_THROW_EXCEPTION(InvalidNameException(message));
130                    else if (strncmp(sqlstate + 2, "622", 3) == 0)
131                        MORDOR_THROW_EXCEPTION(NameTooLongException(message));
132                    else if (strncmp(sqlstate + 2, "939", 3) == 0)
133                        MORDOR_THROW_EXCEPTION(ReservedNameException(message));
134                    else if (strncmp(sqlstate + 2, "804", 3) == 0)
135                        MORDOR_THROW_EXCEPTION(DatatypeMismatchException(message));
136                    else if (strncmp(sqlstate + 2, "P18", 3) == 0)
137                        MORDOR_THROW_EXCEPTION(IndeterminateDatatypeException(message));
138                    else if (strncmp(sqlstate + 2, "809", 3) == 0)
139                        MORDOR_THROW_EXCEPTION(WrongObjectTypeException(message));
140                    else if (strncmp(sqlstate + 2, "703", 3) == 0)
141                        MORDOR_THROW_EXCEPTION(UndefinedColumnException(message));
142                    else if (strncmp(sqlstate + 2, "883", 3) == 0)
143                        MORDOR_THROW_EXCEPTION(UndefinedFunctionException(message));
144                    else if (strncmp(sqlstate + 2, "P01", 3) == 0)
145                        MORDOR_THROW_EXCEPTION(UndefinedTableException(message));
146                    else if (strncmp(sqlstate + 2, "P02", 3) == 0)
147                        MORDOR_THROW_EXCEPTION(UndefinedParameterException(message));
148                    else if (strncmp(sqlstate + 2, "704", 3) == 0)
149                        MORDOR_THROW_EXCEPTION(UndefinedObjectException(message));
150                    else if (strncmp(sqlstate + 2, "701", 3) == 0)
151                        MORDOR_THROW_EXCEPTION(DuplicateColumnException(message));
152                    else if (strncmp(sqlstate + 2, "P03", 3) == 0)
153                        MORDOR_THROW_EXCEPTION(DuplicateCursorException(message));
154                    else if (strncmp(sqlstate + 2, "P04", 3) == 0)
155                        MORDOR_THROW_EXCEPTION(DuplicateDatabaseException(message));
156                    else if (strncmp(sqlstate + 2, "723", 3) == 0)
157                        MORDOR_THROW_EXCEPTION(DuplicateFunctionException(message));
158                    else if (strncmp(sqlstate + 2, "P05", 3) == 0)
159                        MORDOR_THROW_EXCEPTION(DuplicatePreparedStatementException(message));
160                    else if (strncmp(sqlstate + 2, "P06", 3) == 0)
161                        MORDOR_THROW_EXCEPTION(DuplicateSchemaException(message));
162                    else if (strncmp(sqlstate + 2, "P07", 3) == 0)
163                        MORDOR_THROW_EXCEPTION(DuplicateTableException(message));
164                    else if (strncmp(sqlstate + 2, "712", 3) == 0)
165                        MORDOR_THROW_EXCEPTION(DuplicateAliasException(message));
166                    else if (strncmp(sqlstate + 2, "710", 3) == 0)
167                        MORDOR_THROW_EXCEPTION(DuplicateObjectException(message));
168                    else if (strncmp(sqlstate + 2, "702", 3) == 0)
169                        MORDOR_THROW_EXCEPTION(AmbiguousColumnException(message));
170                    else if (strncmp(sqlstate + 2, "725", 3) == 0)
171                        MORDOR_THROW_EXCEPTION(AmbiguousFunctionException(message));
172                    else if (strncmp(sqlstate + 2, "P08", 3) == 0)
173                        MORDOR_THROW_EXCEPTION(AmbiguousParameterException(message));
174                    else if (strncmp(sqlstate + 2, "P09", 3) == 0)
175                        MORDOR_THROW_EXCEPTION(AmbiguousAliasException(message));
176                    else if (strncmp(sqlstate + 2, "P10", 3) == 0)
177                        MORDOR_THROW_EXCEPTION(InvalidColumnReferenceException(message));
178                    else if (strncmp(sqlstate + 2, "P11", 3) == 0)
179                        MORDOR_THROW_EXCEPTION(InvalidColumnDefinitionException(message));
180                    else if (strncmp(sqlstate + 2, "P12", 3) == 0)
181                        MORDOR_THROW_EXCEPTION(InvalidDatabaseDefinitionException(message));
182                    else if (strncmp(sqlstate + 2, "P13", 3) == 0)
183                        MORDOR_THROW_EXCEPTION(InvalidFunctionDefinitionException(message));
184                    else if (strncmp(sqlstate + 2, "P14", 3) == 0)
185                        MORDOR_THROW_EXCEPTION(InvalidPreparedStatementDefinitionException(message));
186                    else if (strncmp(sqlstate + 2, "P15", 3) == 0)
187                        MORDOR_THROW_EXCEPTION(InvalidSchemaDefinitionException(message));
188                    else if (strncmp(sqlstate + 2, "P16", 3) == 0)
189                        MORDOR_THROW_EXCEPTION(InvalidTableDefinitionException(message));
190                    else if (strncmp(sqlstate + 2, "P17", 3) == 0)
191                        MORDOR_THROW_EXCEPTION(InvalidObjectDefinitionException(message));
192                    else
193                        MORDOR_THROW_EXCEPTION(AccessRuleViolationException(message));
194                default:
195                    break;
196            }
197        case '5':
198            switch (sqlstate[1]) {
199                case '3':
200                    if (strncmp(sqlstate + 2, "000", 3) == 0)
201                        MORDOR_THROW_EXCEPTION(InsufficientResourcesException(message));
202                    else if (strncmp(sqlstate + 2, "100", 3) == 0)
203                        MORDOR_THROW_EXCEPTION(DiskFullException(message));
204                    else if (strncmp(sqlstate + 2, "200", 3) == 0)
205                        MORDOR_THROW_EXCEPTION(OutOfMemoryException(message));
206                    else if (strncmp(sqlstate + 2, "300", 3) == 0)
207                        MORDOR_THROW_EXCEPTION(TooManyConnectionsException(message));
208                    else
209                        MORDOR_THROW_EXCEPTION(InsufficientResourcesException(message));
210                case '4':
211                    if (strncmp(sqlstate + 2, "000", 3) == 0)
212                        MORDOR_THROW_EXCEPTION(ProgramLimitExceededException(message));
213                    else if (strncmp(sqlstate + 2, "001", 3) == 0)
214                        MORDOR_THROW_EXCEPTION(StatementTooComplexException(message));
215                    else if (strncmp(sqlstate + 2, "011", 3) == 0)
216                        MORDOR_THROW_EXCEPTION(TooManyColumnsException(message));
217                    else if (strncmp(sqlstate + 2, "023", 3) == 0)
218                        MORDOR_THROW_EXCEPTION(TooManyArgumentsException(message));
219                    else
220                        MORDOR_THROW_EXCEPTION(ProgramLimitExceededException(message));
221                case '7':
222                    if (strncmp(sqlstate + 2, "000", 3) == 0)
223                        MORDOR_THROW_EXCEPTION(OperatorInterventionException(message));
224                    else if (strncmp(sqlstate + 2, "014", 3) == 0)
225                        MORDOR_THROW_EXCEPTION(QueryCanceledException(message));
226                    else if (strncmp(sqlstate + 2, "P01", 3) == 0)
227                        MORDOR_THROW_EXCEPTION(AdminShutdownException(message));
228                    else if (strncmp(sqlstate + 2, "P02", 3) == 0)
229                        MORDOR_THROW_EXCEPTION(CrashShutdownException(message));
230                    else if (strncmp(sqlstate + 2, "P03", 3) == 0)
231                        MORDOR_THROW_EXCEPTION(CannotConnectNowException(message));
232                    else
233                        MORDOR_THROW_EXCEPTION(OperatorInterventionException(message));
234                case '8':
235                    if (strncmp(sqlstate + 2, "030", 3) == 0)
236                        MORDOR_THROW_EXCEPTION(IOError(message));
237                    else if (strncmp(sqlstate + 2, "P01", 3) == 0)
238                        MORDOR_THROW_EXCEPTION(UndefinedFileException(message));
239                    else if (strncmp(sqlstate + 2, "P02", 3) == 0)
240                        MORDOR_THROW_EXCEPTION(DuplicateFileException(message));
241                    else
242                        MORDOR_THROW_EXCEPTION(SystemError(message));
243                default:
244                    break;
245            }
246        case 'F':
247            switch (sqlstate[1]) {
248                case '0':
249                    if (strncmp(sqlstate + 2, "000", 3) == 0)
250                        MORDOR_THROW_EXCEPTION(ConfigFileError(message));
251                    else if (strncmp(sqlstate + 2, "001", 3) == 0)
252                        MORDOR_THROW_EXCEPTION(LockFileExistsException(message));
253                    else
254                        MORDOR_THROW_EXCEPTION(ConfigFileError(message));
255                default:
256                    break;
257            }
258        case 'X':
259            switch (sqlstate[1]) {
260                case 'X':
261                    if (strncmp(sqlstate + 2, "000", 3) == 0)
262                        MORDOR_THROW_EXCEPTION(InternalError(message));
263                    else if (strncmp(sqlstate + 2, "001", 3) == 0)
264                        MORDOR_THROW_EXCEPTION(DataCorruptedException(message));
265                    else if (strncmp(sqlstate + 2, "002", 3) == 0)
266                        MORDOR_THROW_EXCEPTION(IndexCorruptedException(message));
267                    else
268                        MORDOR_THROW_EXCEPTION(InternalError(message));
269                default:
270                    break;
271            }
272        default:
273            break;
274    }
275    MORDOR_THROW_EXCEPTION(Exception(message));
276}
277
278}}