├── LICENSE ├── README.md ├── errcode.go ├── gen.rb └── go.mod /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2019 Jack Christensen 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | --- 25 | 26 | Underlying Data Under PostgreSQL License: 27 | 28 | PostgreSQL Database Management System 29 | (formerly known as Postgres, then as Postgres95) 30 | 31 | Portions Copyright © 1996-2019, The PostgreSQL Global Development Group 32 | 33 | Portions Copyright © 1994, The Regents of the University of California 34 | 35 | Permission to use, copy, modify, and distribute this software and its documentation for any purpose, without fee, and 36 | without a written agreement is hereby granted, provided that the above copyright notice and this paragraph and the 37 | following two paragraphs appear in all copies. 38 | 39 | IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR 40 | CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 41 | THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 42 | 43 | THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 44 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" 45 | BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR 46 | MODIFICATIONS. 47 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![](https://godoc.org/github.com/jackc/pgerrcode?status.svg)](https://godoc.org/github.com/jackc/pgerrcode) 2 | 3 | # pgerrcode 4 | 5 | Package pgerrcode contains constants for PostgreSQL error codes. 6 | 7 | ## License 8 | 9 | MIT for this package's code and PostgreSQL License for the underlying data. 10 | -------------------------------------------------------------------------------- /errcode.go: -------------------------------------------------------------------------------- 1 | // Package pgerrcode contains constants for PostgreSQL error codes. 2 | package pgerrcode 3 | 4 | // Source: https://www.postgresql.org/docs/16/errcodes-appendix.html 5 | // See gen.rb for script that can convert the error code table to Go code. 6 | 7 | const ( 8 | 9 | // Class 00 — Successful Completion 10 | SuccessfulCompletion = "00000" 11 | 12 | // Class 01 — Warning 13 | Warning = "01000" 14 | DynamicResultSetsReturned = "0100C" 15 | ImplicitZeroBitPadding = "01008" 16 | NullValueEliminatedInSetFunction = "01003" 17 | PrivilegeNotGranted = "01007" 18 | PrivilegeNotRevoked = "01006" 19 | StringDataRightTruncationWarning = "01004" 20 | DeprecatedFeature = "01P01" 21 | 22 | // Class 02 — No Data (this is also a warning class per the SQL standard) 23 | NoData = "02000" 24 | NoAdditionalDynamicResultSetsReturned = "02001" 25 | 26 | // Class 03 — SQL Statement Not Yet Complete 27 | SQLStatementNotYetComplete = "03000" 28 | 29 | // Class 08 — Connection Exception 30 | ConnectionException = "08000" 31 | ConnectionDoesNotExist = "08003" 32 | ConnectionFailure = "08006" 33 | SQLClientUnableToEstablishSQLConnection = "08001" 34 | SQLServerRejectedEstablishmentOfSQLConnection = "08004" 35 | TransactionResolutionUnknown = "08007" 36 | ProtocolViolation = "08P01" 37 | 38 | // Class 09 — Triggered Action Exception 39 | TriggeredActionException = "09000" 40 | 41 | // Class 0A — Feature Not Supported 42 | FeatureNotSupported = "0A000" 43 | 44 | // Class 0B — Invalid Transaction Initiation 45 | InvalidTransactionInitiation = "0B000" 46 | 47 | // Class 0F — Locator Exception 48 | LocatorException = "0F000" 49 | InvalidLocatorSpecification = "0F001" 50 | 51 | // Class 0L — Invalid Grantor 52 | InvalidGrantor = "0L000" 53 | InvalidGrantOperation = "0LP01" 54 | 55 | // Class 0P — Invalid Role Specification 56 | InvalidRoleSpecification = "0P000" 57 | 58 | // Class 0Z — Diagnostics Exception 59 | DiagnosticsException = "0Z000" 60 | StackedDiagnosticsAccessedWithoutActiveHandler = "0Z002" 61 | 62 | // Class 20 — Case Not Found 63 | CaseNotFound = "20000" 64 | 65 | // Class 21 — Cardinality Violation 66 | CardinalityViolation = "21000" 67 | 68 | // Class 22 — Data Exception 69 | DataException = "22000" 70 | ArraySubscriptError = "2202E" 71 | CharacterNotInRepertoire = "22021" 72 | DatetimeFieldOverflow = "22008" 73 | DivisionByZero = "22012" 74 | ErrorInAssignment = "22005" 75 | EscapeCharacterConflict = "2200B" 76 | IndicatorOverflow = "22022" 77 | IntervalFieldOverflow = "22015" 78 | InvalidArgumentForLogarithm = "2201E" 79 | InvalidArgumentForNtileFunction = "22014" 80 | InvalidArgumentForNthValueFunction = "22016" 81 | InvalidArgumentForPowerFunction = "2201F" 82 | InvalidArgumentForWidthBucketFunction = "2201G" 83 | InvalidCharacterValueForCast = "22018" 84 | InvalidDatetimeFormat = "22007" 85 | InvalidEscapeCharacter = "22019" 86 | InvalidEscapeOctet = "2200D" 87 | InvalidEscapeSequence = "22025" 88 | NonstandardUseOfEscapeCharacter = "22P06" 89 | InvalidIndicatorParameterValue = "22010" 90 | InvalidParameterValue = "22023" 91 | InvalidPrecedingOrFollowingSize = "22013" 92 | InvalidRegularExpression = "2201B" 93 | InvalidRowCountInLimitClause = "2201W" 94 | InvalidRowCountInResultOffsetClause = "2201X" 95 | InvalidTablesampleArgument = "2202H" 96 | InvalidTablesampleRepeat = "2202G" 97 | InvalidTimeZoneDisplacementValue = "22009" 98 | InvalidUseOfEscapeCharacter = "2200C" 99 | MostSpecificTypeMismatch = "2200G" 100 | NullValueNotAllowedDataException = "22004" 101 | NullValueNoIndicatorParameter = "22002" 102 | NumericValueOutOfRange = "22003" 103 | SequenceGeneratorLimitExceeded = "2200H" 104 | StringDataLengthMismatch = "22026" 105 | StringDataRightTruncationDataException = "22001" 106 | SubstringError = "22011" 107 | TrimError = "22027" 108 | UnterminatedCString = "22024" 109 | ZeroLengthCharacterString = "2200F" 110 | FloatingPointException = "22P01" 111 | InvalidTextRepresentation = "22P02" 112 | InvalidBinaryRepresentation = "22P03" 113 | BadCopyFileFormat = "22P04" 114 | UntranslatableCharacter = "22P05" 115 | NotAnXMLDocument = "2200L" 116 | InvalidXMLDocument = "2200M" 117 | InvalidXMLContent = "2200N" 118 | InvalidXMLComment = "2200S" 119 | InvalidXMLProcessingInstruction = "2200T" 120 | DuplicateJSONObjectKeyValue = "22030" 121 | InvalidArgumentForSQLJSONDatetimeFunction = "22031" 122 | InvalidJSONText = "22032" 123 | InvalidSQLJSONSubscript = "22033" 124 | MoreThanOneSQLJSONItem = "22034" 125 | NoSQLJSONItem = "22035" 126 | NonNumericSQLJSONItem = "22036" 127 | NonUniqueKeysInAJSONObject = "22037" 128 | SingletonSQLJSONItemRequired = "22038" 129 | SQLJSONArrayNotFound = "22039" 130 | SQLJSONMemberNotFound = "2203A" 131 | SQLJSONNumberNotFound = "2203B" 132 | SQLJSONObjectNotFound = "2203C" 133 | TooManyJSONArrayElements = "2203D" 134 | TooManyJSONObjectMembers = "2203E" 135 | SQLJSONScalarRequired = "2203F" 136 | SQLJSONItemCannotBeCastToTargetType = "2203G" 137 | 138 | // Class 23 — Integrity Constraint Violation 139 | IntegrityConstraintViolation = "23000" 140 | RestrictViolation = "23001" 141 | NotNullViolation = "23502" 142 | ForeignKeyViolation = "23503" 143 | UniqueViolation = "23505" 144 | CheckViolation = "23514" 145 | ExclusionViolation = "23P01" 146 | 147 | // Class 24 — Invalid Cursor State 148 | InvalidCursorState = "24000" 149 | 150 | // Class 25 — Invalid Transaction State 151 | InvalidTransactionState = "25000" 152 | ActiveSQLTransaction = "25001" 153 | BranchTransactionAlreadyActive = "25002" 154 | HeldCursorRequiresSameIsolationLevel = "25008" 155 | InappropriateAccessModeForBranchTransaction = "25003" 156 | InappropriateIsolationLevelForBranchTransaction = "25004" 157 | NoActiveSQLTransactionForBranchTransaction = "25005" 158 | ReadOnlySQLTransaction = "25006" 159 | SchemaAndDataStatementMixingNotSupported = "25007" 160 | NoActiveSQLTransaction = "25P01" 161 | InFailedSQLTransaction = "25P02" 162 | IdleInTransactionSessionTimeout = "25P03" 163 | 164 | // Class 26 — Invalid SQL Statement Name 165 | InvalidSQLStatementName = "26000" 166 | 167 | // Class 27 — Triggered Data Change Violation 168 | TriggeredDataChangeViolation = "27000" 169 | 170 | // Class 28 — Invalid Authorization Specification 171 | InvalidAuthorizationSpecification = "28000" 172 | InvalidPassword = "28P01" 173 | 174 | // Class 2B — Dependent Privilege Descriptors Still Exist 175 | DependentPrivilegeDescriptorsStillExist = "2B000" 176 | DependentObjectsStillExist = "2BP01" 177 | 178 | // Class 2D — Invalid Transaction Termination 179 | InvalidTransactionTermination = "2D000" 180 | 181 | // Class 2F — SQL Routine Exception 182 | SQLRoutineException = "2F000" 183 | FunctionExecutedNoReturnStatement = "2F005" 184 | ModifyingSQLDataNotPermittedSQLRoutineException = "2F002" 185 | ProhibitedSQLStatementAttemptedSQLRoutineException = "2F003" 186 | ReadingSQLDataNotPermittedSQLRoutineException = "2F004" 187 | 188 | // Class 34 — Invalid Cursor Name 189 | InvalidCursorName = "34000" 190 | 191 | // Class 38 — External Routine Exception 192 | ExternalRoutineException = "38000" 193 | ContainingSQLNotPermitted = "38001" 194 | ModifyingSQLDataNotPermittedExternalRoutineException = "38002" 195 | ProhibitedSQLStatementAttemptedExternalRoutineException = "38003" 196 | ReadingSQLDataNotPermittedExternalRoutineException = "38004" 197 | 198 | // Class 39 — External Routine Invocation Exception 199 | ExternalRoutineInvocationException = "39000" 200 | InvalidSQLstateReturned = "39001" 201 | NullValueNotAllowedExternalRoutineInvocationException = "39004" 202 | TriggerProtocolViolated = "39P01" 203 | SRFProtocolViolated = "39P02" 204 | EventTriggerProtocolViolated = "39P03" 205 | 206 | // Class 3B — Savepoint Exception 207 | SavepointException = "3B000" 208 | InvalidSavepointSpecification = "3B001" 209 | 210 | // Class 3D — Invalid Catalog Name 211 | InvalidCatalogName = "3D000" 212 | 213 | // Class 3F — Invalid Schema Name 214 | InvalidSchemaName = "3F000" 215 | 216 | // Class 40 — Transaction Rollback 217 | TransactionRollback = "40000" 218 | TransactionIntegrityConstraintViolation = "40002" 219 | SerializationFailure = "40001" 220 | StatementCompletionUnknown = "40003" 221 | DeadlockDetected = "40P01" 222 | 223 | // Class 42 — Syntax Error or Access Rule Violation 224 | SyntaxErrorOrAccessRuleViolation = "42000" 225 | SyntaxError = "42601" 226 | InsufficientPrivilege = "42501" 227 | CannotCoerce = "42846" 228 | GroupingError = "42803" 229 | WindowingError = "42P20" 230 | InvalidRecursion = "42P19" 231 | InvalidForeignKey = "42830" 232 | InvalidName = "42602" 233 | NameTooLong = "42622" 234 | ReservedName = "42939" 235 | DatatypeMismatch = "42804" 236 | IndeterminateDatatype = "42P18" 237 | CollationMismatch = "42P21" 238 | IndeterminateCollation = "42P22" 239 | WrongObjectType = "42809" 240 | GeneratedAlways = "428C9" 241 | UndefinedColumn = "42703" 242 | UndefinedFunction = "42883" 243 | UndefinedTable = "42P01" 244 | UndefinedParameter = "42P02" 245 | UndefinedObject = "42704" 246 | DuplicateColumn = "42701" 247 | DuplicateCursor = "42P03" 248 | DuplicateDatabase = "42P04" 249 | DuplicateFunction = "42723" 250 | DuplicatePreparedStatement = "42P05" 251 | DuplicateSchema = "42P06" 252 | DuplicateTable = "42P07" 253 | DuplicateAlias = "42712" 254 | DuplicateObject = "42710" 255 | AmbiguousColumn = "42702" 256 | AmbiguousFunction = "42725" 257 | AmbiguousParameter = "42P08" 258 | AmbiguousAlias = "42P09" 259 | InvalidColumnReference = "42P10" 260 | InvalidColumnDefinition = "42611" 261 | InvalidCursorDefinition = "42P11" 262 | InvalidDatabaseDefinition = "42P12" 263 | InvalidFunctionDefinition = "42P13" 264 | InvalidPreparedStatementDefinition = "42P14" 265 | InvalidSchemaDefinition = "42P15" 266 | InvalidTableDefinition = "42P16" 267 | InvalidObjectDefinition = "42P17" 268 | 269 | // Class 44 — WITH CHECK OPTION Violation 270 | WithCheckOptionViolation = "44000" 271 | 272 | // Class 53 — Insufficient Resources 273 | InsufficientResources = "53000" 274 | DiskFull = "53100" 275 | OutOfMemory = "53200" 276 | TooManyConnections = "53300" 277 | ConfigurationLimitExceeded = "53400" 278 | 279 | // Class 54 — Program Limit Exceeded 280 | ProgramLimitExceeded = "54000" 281 | StatementTooComplex = "54001" 282 | TooManyColumns = "54011" 283 | TooManyArguments = "54023" 284 | 285 | // Class 55 — Object Not In Prerequisite State 286 | ObjectNotInPrerequisiteState = "55000" 287 | ObjectInUse = "55006" 288 | CantChangeRuntimeParam = "55P02" 289 | LockNotAvailable = "55P03" 290 | UnsafeNewEnumValueUsage = "55P04" 291 | 292 | // Class 57 — Operator Intervention 293 | OperatorIntervention = "57000" 294 | QueryCanceled = "57014" 295 | AdminShutdown = "57P01" 296 | CrashShutdown = "57P02" 297 | CannotConnectNow = "57P03" 298 | DatabaseDropped = "57P04" 299 | IdleSessionTimeout = "57P05" 300 | 301 | // Class 58 — System Error (errors external to PostgreSQL itself) 302 | SystemError = "58000" 303 | IOError = "58030" 304 | UndefinedFile = "58P01" 305 | DuplicateFile = "58P02" 306 | 307 | // Class 72 — Snapshot Failure 308 | SnapshotTooOld = "72000" 309 | 310 | // Class F0 — Configuration File Error 311 | ConfigFileError = "F0000" 312 | LockFileExists = "F0001" 313 | 314 | // Class HV — Foreign Data Wrapper Error (SQL/MED) 315 | FDWError = "HV000" 316 | FDWColumnNameNotFound = "HV005" 317 | FDWDynamicParameterValueNeeded = "HV002" 318 | FDWFunctionSequenceError = "HV010" 319 | FDWInconsistentDescriptorInformation = "HV021" 320 | FDWInvalidAttributeValue = "HV024" 321 | FDWInvalidColumnName = "HV007" 322 | FDWInvalidColumnNumber = "HV008" 323 | FDWInvalidDataType = "HV004" 324 | FDWInvalidDataTypeDescriptors = "HV006" 325 | FDWInvalidDescriptorFieldIdentifier = "HV091" 326 | FDWInvalidHandle = "HV00B" 327 | FDWInvalidOptionIndex = "HV00C" 328 | FDWInvalidOptionName = "HV00D" 329 | FDWInvalidStringLengthOrBufferLength = "HV090" 330 | FDWInvalidStringFormat = "HV00A" 331 | FDWInvalidUseOfNullPointer = "HV009" 332 | FDWTooManyHandles = "HV014" 333 | FDWOutOfMemory = "HV001" 334 | FDWNoSchemas = "HV00P" 335 | FDWOptionNameNotFound = "HV00J" 336 | FDWReplyHandle = "HV00K" 337 | FDWSchemaNotFound = "HV00Q" 338 | FDWTableNotFound = "HV00R" 339 | FDWUnableToCreateExecution = "HV00L" 340 | FDWUnableToCreateReply = "HV00M" 341 | FDWUnableToEstablishConnection = "HV00N" 342 | 343 | // Class P0 — PL/pgSQL Error 344 | PLpgSQLError = "P0000" 345 | RaiseException = "P0001" 346 | NoDataFound = "P0002" 347 | TooManyRows = "P0003" 348 | AssertFailure = "P0004" 349 | 350 | // Class XX — Internal Error 351 | InternalError = "XX000" 352 | DataCorrupted = "XX001" 353 | IndexCorrupted = "XX002" 354 | ) 355 | 356 | // IsSuccessfulCompletion asserts the error code class is Class 00 — Successful Completion 357 | func IsSuccessfulCompletion(code string) bool { 358 | switch code { 359 | case SuccessfulCompletion: 360 | return true 361 | } 362 | return false 363 | } 364 | 365 | // IsWarning asserts the error code class is Class 01 — Warning 366 | func IsWarning(code string) bool { 367 | switch code { 368 | case Warning, DynamicResultSetsReturned, ImplicitZeroBitPadding, NullValueEliminatedInSetFunction, PrivilegeNotGranted, PrivilegeNotRevoked, StringDataRightTruncationWarning, DeprecatedFeature: 369 | return true 370 | } 371 | return false 372 | } 373 | 374 | // IsNoData asserts the error code class is Class 02 — No Data (this is also a warning class per the SQL standard) 375 | func IsNoData(code string) bool { 376 | switch code { 377 | case NoData, NoAdditionalDynamicResultSetsReturned: 378 | return true 379 | } 380 | return false 381 | } 382 | 383 | // IsSQLStatementNotYetComplete asserts the error code class is Class 03 — SQL Statement Not Yet Complete 384 | func IsSQLStatementNotYetComplete(code string) bool { 385 | switch code { 386 | case SQLStatementNotYetComplete: 387 | return true 388 | } 389 | return false 390 | } 391 | 392 | // IsConnectionException asserts the error code class is Class 08 — Connection Exception 393 | func IsConnectionException(code string) bool { 394 | switch code { 395 | case ConnectionException, ConnectionDoesNotExist, ConnectionFailure, SQLClientUnableToEstablishSQLConnection, SQLServerRejectedEstablishmentOfSQLConnection, TransactionResolutionUnknown, ProtocolViolation: 396 | return true 397 | } 398 | return false 399 | } 400 | 401 | // IsTriggeredActionException asserts the error code class is Class 09 — Triggered Action Exception 402 | func IsTriggeredActionException(code string) bool { 403 | switch code { 404 | case TriggeredActionException: 405 | return true 406 | } 407 | return false 408 | } 409 | 410 | // IsFeatureNotSupported asserts the error code class is Class 0A — Feature Not Supported 411 | func IsFeatureNotSupported(code string) bool { 412 | switch code { 413 | case FeatureNotSupported: 414 | return true 415 | } 416 | return false 417 | } 418 | 419 | // IsInvalidTransactionInitiation asserts the error code class is Class 0B — Invalid Transaction Initiation 420 | func IsInvalidTransactionInitiation(code string) bool { 421 | switch code { 422 | case InvalidTransactionInitiation: 423 | return true 424 | } 425 | return false 426 | } 427 | 428 | // IsLocatorException asserts the error code class is Class 0F — Locator Exception 429 | func IsLocatorException(code string) bool { 430 | switch code { 431 | case LocatorException, InvalidLocatorSpecification: 432 | return true 433 | } 434 | return false 435 | } 436 | 437 | // IsInvalidGrantor asserts the error code class is Class 0L — Invalid Grantor 438 | func IsInvalidGrantor(code string) bool { 439 | switch code { 440 | case InvalidGrantor, InvalidGrantOperation: 441 | return true 442 | } 443 | return false 444 | } 445 | 446 | // IsInvalidRoleSpecification asserts the error code class is Class 0P — Invalid Role Specification 447 | func IsInvalidRoleSpecification(code string) bool { 448 | switch code { 449 | case InvalidRoleSpecification: 450 | return true 451 | } 452 | return false 453 | } 454 | 455 | // IsDiagnosticsException asserts the error code class is Class 0Z — Diagnostics Exception 456 | func IsDiagnosticsException(code string) bool { 457 | switch code { 458 | case DiagnosticsException, StackedDiagnosticsAccessedWithoutActiveHandler: 459 | return true 460 | } 461 | return false 462 | } 463 | 464 | // IsCaseNotFound asserts the error code class is Class 20 — Case Not Found 465 | func IsCaseNotFound(code string) bool { 466 | switch code { 467 | case CaseNotFound: 468 | return true 469 | } 470 | return false 471 | } 472 | 473 | // IsCardinalityViolation asserts the error code class is Class 21 — Cardinality Violation 474 | func IsCardinalityViolation(code string) bool { 475 | switch code { 476 | case CardinalityViolation: 477 | return true 478 | } 479 | return false 480 | } 481 | 482 | // IsDataException asserts the error code class is Class 22 — Data Exception 483 | func IsDataException(code string) bool { 484 | switch code { 485 | case DataException, ArraySubscriptError, CharacterNotInRepertoire, DatetimeFieldOverflow, DivisionByZero, ErrorInAssignment, EscapeCharacterConflict, IndicatorOverflow, IntervalFieldOverflow, InvalidArgumentForLogarithm, InvalidArgumentForNtileFunction, InvalidArgumentForNthValueFunction, InvalidArgumentForPowerFunction, InvalidArgumentForWidthBucketFunction, InvalidCharacterValueForCast, InvalidDatetimeFormat, InvalidEscapeCharacter, InvalidEscapeOctet, InvalidEscapeSequence, NonstandardUseOfEscapeCharacter, InvalidIndicatorParameterValue, InvalidParameterValue, InvalidPrecedingOrFollowingSize, InvalidRegularExpression, InvalidRowCountInLimitClause, InvalidRowCountInResultOffsetClause, InvalidTablesampleArgument, InvalidTablesampleRepeat, InvalidTimeZoneDisplacementValue, InvalidUseOfEscapeCharacter, MostSpecificTypeMismatch, NullValueNotAllowedDataException, NullValueNoIndicatorParameter, NumericValueOutOfRange, SequenceGeneratorLimitExceeded, StringDataLengthMismatch, StringDataRightTruncationDataException, SubstringError, TrimError, UnterminatedCString, ZeroLengthCharacterString, FloatingPointException, InvalidTextRepresentation, InvalidBinaryRepresentation, BadCopyFileFormat, UntranslatableCharacter, NotAnXMLDocument, InvalidXMLDocument, InvalidXMLContent, InvalidXMLComment, InvalidXMLProcessingInstruction, DuplicateJSONObjectKeyValue, InvalidArgumentForSQLJSONDatetimeFunction, InvalidJSONText, InvalidSQLJSONSubscript, MoreThanOneSQLJSONItem, NoSQLJSONItem, NonNumericSQLJSONItem, NonUniqueKeysInAJSONObject, SingletonSQLJSONItemRequired, SQLJSONArrayNotFound, SQLJSONMemberNotFound, SQLJSONNumberNotFound, SQLJSONObjectNotFound, TooManyJSONArrayElements, TooManyJSONObjectMembers, SQLJSONScalarRequired, SQLJSONItemCannotBeCastToTargetType: 486 | return true 487 | } 488 | return false 489 | } 490 | 491 | // IsIntegrityConstraintViolation asserts the error code class is Class 23 — Integrity Constraint Violation 492 | func IsIntegrityConstraintViolation(code string) bool { 493 | switch code { 494 | case IntegrityConstraintViolation, RestrictViolation, NotNullViolation, ForeignKeyViolation, UniqueViolation, CheckViolation, ExclusionViolation: 495 | return true 496 | } 497 | return false 498 | } 499 | 500 | // IsInvalidCursorState asserts the error code class is Class 24 — Invalid Cursor State 501 | func IsInvalidCursorState(code string) bool { 502 | switch code { 503 | case InvalidCursorState: 504 | return true 505 | } 506 | return false 507 | } 508 | 509 | // IsInvalidTransactionState asserts the error code class is Class 25 — Invalid Transaction State 510 | func IsInvalidTransactionState(code string) bool { 511 | switch code { 512 | case InvalidTransactionState, ActiveSQLTransaction, BranchTransactionAlreadyActive, HeldCursorRequiresSameIsolationLevel, InappropriateAccessModeForBranchTransaction, InappropriateIsolationLevelForBranchTransaction, NoActiveSQLTransactionForBranchTransaction, ReadOnlySQLTransaction, SchemaAndDataStatementMixingNotSupported, NoActiveSQLTransaction, InFailedSQLTransaction, IdleInTransactionSessionTimeout: 513 | return true 514 | } 515 | return false 516 | } 517 | 518 | // IsInvalidSQLStatementName asserts the error code class is Class 26 — Invalid SQL Statement Name 519 | func IsInvalidSQLStatementName(code string) bool { 520 | switch code { 521 | case InvalidSQLStatementName: 522 | return true 523 | } 524 | return false 525 | } 526 | 527 | // IsTriggeredDataChangeViolation asserts the error code class is Class 27 — Triggered Data Change Violation 528 | func IsTriggeredDataChangeViolation(code string) bool { 529 | switch code { 530 | case TriggeredDataChangeViolation: 531 | return true 532 | } 533 | return false 534 | } 535 | 536 | // IsInvalidAuthorizationSpecification asserts the error code class is Class 28 — Invalid Authorization Specification 537 | func IsInvalidAuthorizationSpecification(code string) bool { 538 | switch code { 539 | case InvalidAuthorizationSpecification, InvalidPassword: 540 | return true 541 | } 542 | return false 543 | } 544 | 545 | // IsDependentPrivilegeDescriptorsStillExist asserts the error code class is Class 2B — Dependent Privilege Descriptors Still Exist 546 | func IsDependentPrivilegeDescriptorsStillExist(code string) bool { 547 | switch code { 548 | case DependentPrivilegeDescriptorsStillExist, DependentObjectsStillExist: 549 | return true 550 | } 551 | return false 552 | } 553 | 554 | // IsInvalidTransactionTermination asserts the error code class is Class 2D — Invalid Transaction Termination 555 | func IsInvalidTransactionTermination(code string) bool { 556 | switch code { 557 | case InvalidTransactionTermination: 558 | return true 559 | } 560 | return false 561 | } 562 | 563 | // IsSQLRoutineException asserts the error code class is Class 2F — SQL Routine Exception 564 | func IsSQLRoutineException(code string) bool { 565 | switch code { 566 | case SQLRoutineException, FunctionExecutedNoReturnStatement, ModifyingSQLDataNotPermittedSQLRoutineException, ProhibitedSQLStatementAttemptedSQLRoutineException, ReadingSQLDataNotPermittedSQLRoutineException: 567 | return true 568 | } 569 | return false 570 | } 571 | 572 | // IsInvalidCursorName asserts the error code class is Class 34 — Invalid Cursor Name 573 | func IsInvalidCursorName(code string) bool { 574 | switch code { 575 | case InvalidCursorName: 576 | return true 577 | } 578 | return false 579 | } 580 | 581 | // IsExternalRoutineException asserts the error code class is Class 38 — External Routine Exception 582 | func IsExternalRoutineException(code string) bool { 583 | switch code { 584 | case ExternalRoutineException, ContainingSQLNotPermitted, ModifyingSQLDataNotPermittedExternalRoutineException, ProhibitedSQLStatementAttemptedExternalRoutineException, ReadingSQLDataNotPermittedExternalRoutineException: 585 | return true 586 | } 587 | return false 588 | } 589 | 590 | // IsExternalRoutineInvocationException asserts the error code class is Class 39 — External Routine Invocation Exception 591 | func IsExternalRoutineInvocationException(code string) bool { 592 | switch code { 593 | case ExternalRoutineInvocationException, InvalidSQLstateReturned, NullValueNotAllowedExternalRoutineInvocationException, TriggerProtocolViolated, SRFProtocolViolated, EventTriggerProtocolViolated: 594 | return true 595 | } 596 | return false 597 | } 598 | 599 | // IsSavepointException asserts the error code class is Class 3B — Savepoint Exception 600 | func IsSavepointException(code string) bool { 601 | switch code { 602 | case SavepointException, InvalidSavepointSpecification: 603 | return true 604 | } 605 | return false 606 | } 607 | 608 | // IsInvalidCatalogName asserts the error code class is Class 3D — Invalid Catalog Name 609 | func IsInvalidCatalogName(code string) bool { 610 | switch code { 611 | case InvalidCatalogName: 612 | return true 613 | } 614 | return false 615 | } 616 | 617 | // IsInvalidSchemaName asserts the error code class is Class 3F — Invalid Schema Name 618 | func IsInvalidSchemaName(code string) bool { 619 | switch code { 620 | case InvalidSchemaName: 621 | return true 622 | } 623 | return false 624 | } 625 | 626 | // IsTransactionRollback asserts the error code class is Class 40 — Transaction Rollback 627 | func IsTransactionRollback(code string) bool { 628 | switch code { 629 | case TransactionRollback, TransactionIntegrityConstraintViolation, SerializationFailure, StatementCompletionUnknown, DeadlockDetected: 630 | return true 631 | } 632 | return false 633 | } 634 | 635 | // IsSyntaxErrororAccessRuleViolation asserts the error code class is Class 42 — Syntax Error or Access Rule Violation 636 | func IsSyntaxErrororAccessRuleViolation(code string) bool { 637 | switch code { 638 | case SyntaxErrorOrAccessRuleViolation, SyntaxError, InsufficientPrivilege, CannotCoerce, GroupingError, WindowingError, InvalidRecursion, InvalidForeignKey, InvalidName, NameTooLong, ReservedName, DatatypeMismatch, IndeterminateDatatype, CollationMismatch, IndeterminateCollation, WrongObjectType, GeneratedAlways, UndefinedColumn, UndefinedFunction, UndefinedTable, UndefinedParameter, UndefinedObject, DuplicateColumn, DuplicateCursor, DuplicateDatabase, DuplicateFunction, DuplicatePreparedStatement, DuplicateSchema, DuplicateTable, DuplicateAlias, DuplicateObject, AmbiguousColumn, AmbiguousFunction, AmbiguousParameter, AmbiguousAlias, InvalidColumnReference, InvalidColumnDefinition, InvalidCursorDefinition, InvalidDatabaseDefinition, InvalidFunctionDefinition, InvalidPreparedStatementDefinition, InvalidSchemaDefinition, InvalidTableDefinition, InvalidObjectDefinition: 639 | return true 640 | } 641 | return false 642 | } 643 | 644 | // IsWithCheckOptionViolation asserts the error code class is Class 44 — WITH CHECK OPTION Violation 645 | func IsWithCheckOptionViolation(code string) bool { 646 | switch code { 647 | case WithCheckOptionViolation: 648 | return true 649 | } 650 | return false 651 | } 652 | 653 | // IsInsufficientResources asserts the error code class is Class 53 — Insufficient Resources 654 | func IsInsufficientResources(code string) bool { 655 | switch code { 656 | case InsufficientResources, DiskFull, OutOfMemory, TooManyConnections, ConfigurationLimitExceeded: 657 | return true 658 | } 659 | return false 660 | } 661 | 662 | // IsProgramLimitExceeded asserts the error code class is Class 54 — Program Limit Exceeded 663 | func IsProgramLimitExceeded(code string) bool { 664 | switch code { 665 | case ProgramLimitExceeded, StatementTooComplex, TooManyColumns, TooManyArguments: 666 | return true 667 | } 668 | return false 669 | } 670 | 671 | // IsObjectNotInPrerequisiteState asserts the error code class is Class 55 — Object Not In Prerequisite State 672 | func IsObjectNotInPrerequisiteState(code string) bool { 673 | switch code { 674 | case ObjectNotInPrerequisiteState, ObjectInUse, CantChangeRuntimeParam, LockNotAvailable, UnsafeNewEnumValueUsage: 675 | return true 676 | } 677 | return false 678 | } 679 | 680 | // IsOperatorIntervention asserts the error code class is Class 57 — Operator Intervention 681 | func IsOperatorIntervention(code string) bool { 682 | switch code { 683 | case OperatorIntervention, QueryCanceled, AdminShutdown, CrashShutdown, CannotConnectNow, DatabaseDropped, IdleSessionTimeout: 684 | return true 685 | } 686 | return false 687 | } 688 | 689 | // IsSystemError asserts the error code class is Class 58 — System Error (errors external to PostgreSQL itself) 690 | func IsSystemError(code string) bool { 691 | switch code { 692 | case SystemError, IOError, UndefinedFile, DuplicateFile: 693 | return true 694 | } 695 | return false 696 | } 697 | 698 | // IsSnapshotFailure asserts the error code class is Class 72 — Snapshot Failure 699 | func IsSnapshotFailure(code string) bool { 700 | switch code { 701 | case SnapshotTooOld: 702 | return true 703 | } 704 | return false 705 | } 706 | 707 | // IsConfigurationFileError asserts the error code class is Class F0 — Configuration File Error 708 | func IsConfigurationFileError(code string) bool { 709 | switch code { 710 | case ConfigFileError, LockFileExists: 711 | return true 712 | } 713 | return false 714 | } 715 | 716 | // IsForeignDataWrapperError asserts the error code class is Class HV — Foreign Data Wrapper Error (SQL/MED) 717 | func IsForeignDataWrapperError(code string) bool { 718 | switch code { 719 | case FDWError, FDWColumnNameNotFound, FDWDynamicParameterValueNeeded, FDWFunctionSequenceError, FDWInconsistentDescriptorInformation, FDWInvalidAttributeValue, FDWInvalidColumnName, FDWInvalidColumnNumber, FDWInvalidDataType, FDWInvalidDataTypeDescriptors, FDWInvalidDescriptorFieldIdentifier, FDWInvalidHandle, FDWInvalidOptionIndex, FDWInvalidOptionName, FDWInvalidStringLengthOrBufferLength, FDWInvalidStringFormat, FDWInvalidUseOfNullPointer, FDWTooManyHandles, FDWOutOfMemory, FDWNoSchemas, FDWOptionNameNotFound, FDWReplyHandle, FDWSchemaNotFound, FDWTableNotFound, FDWUnableToCreateExecution, FDWUnableToCreateReply, FDWUnableToEstablishConnection: 720 | return true 721 | } 722 | return false 723 | } 724 | 725 | // IsPLpgSQLError asserts the error code class is Class P0 — PL/pgSQL Error 726 | func IsPLpgSQLError(code string) bool { 727 | switch code { 728 | case PLpgSQLError, RaiseException, NoDataFound, TooManyRows, AssertFailure: 729 | return true 730 | } 731 | return false 732 | } 733 | 734 | // IsInternalError asserts the error code class is Class XX — Internal Error 735 | func IsInternalError(code string) bool { 736 | switch code { 737 | case InternalError, DataCorrupted, IndexCorrupted: 738 | return true 739 | } 740 | return false 741 | } 742 | -------------------------------------------------------------------------------- /gen.rb: -------------------------------------------------------------------------------- 1 | # Run this script against the data in table A.1. on https://www.postgresql.org/docs/16/errcodes-appendix.html. 2 | # 3 | # Source data should be formatted like the following: 4 | # 5 | # Class 00 — Successful Completion 6 | # 00000 successful_completion 7 | # Class 01 — Warning 8 | # 01000 warning 9 | # 0100C dynamic_result_sets_returned 10 | # 11 | # for best results pass through gofmt 12 | # ruby gen.rb < tablecontents.txt | gofmt > errcode.go 13 | 14 | code_name_overrides = { 15 | # Some error code names are repeated. In those cases add the error class as a suffix. 16 | "01004" => "StringDataRightTruncationWarning", 17 | "22001" => "StringDataRightTruncationDataException", 18 | "22004" => "NullValueNotAllowedDataException", 19 | "2F002" => "ModifyingSQLDataNotPermittedSQLRoutineException", 20 | "2F003" => "ProhibitedSQLStatementAttemptedSQLRoutineException", 21 | "2F004" => "ReadingSQLDataNotPermittedSQLRoutineException", 22 | "38002" => "ModifyingSQLDataNotPermittedExternalRoutineException", 23 | "38003" => "ProhibitedSQLStatementAttemptedExternalRoutineException", 24 | "38004" => "ReadingSQLDataNotPermittedExternalRoutineException", 25 | "39004" => "NullValueNotAllowedExternalRoutineInvocationException", 26 | 27 | # Go casing corrections 28 | "08001" => "SQLClientUnableToEstablishSQLConnection", 29 | "08004" => "SQLServerRejectedEstablishmentOfSQLConnection", 30 | "P0000" => "PLpgSQLError" 31 | } 32 | 33 | class_name_overrides = { 34 | # Go casing corrections 35 | "WITHCHECKOPTIONViolation" => "WithCheckOptionViolation" 36 | } 37 | 38 | cls_errs = Array.new 39 | cls_assertions = Array.new 40 | last_cls = "" 41 | last_cls_full = "" 42 | 43 | def build_assert_func(last_cls, last_cls_full, cls_errs) 44 | <<~GO 45 | // Is#{last_cls} asserts the error code class is #{last_cls_full} 46 | func Is#{last_cls} (code string) bool { 47 | switch code{ 48 | case #{cls_errs.join(", ")}: 49 | return true 50 | } 51 | return false 52 | } 53 | GO 54 | end 55 | 56 | puts <<~STR 57 | // Package pgerrcode contains constants for PostgreSQL error codes. 58 | package pgerrcode 59 | 60 | // Source: https://www.postgresql.org/docs/16/errcodes-appendix.html 61 | // See gen.rb for script that can convert the error code table to Go code. 62 | 63 | const ( 64 | STR 65 | 66 | ARGF.each do |line| 67 | case line 68 | when /^Class/ 69 | if cls_errs.length > 0 && last_cls != "" 70 | assert_func = build_assert_func(class_name_overrides.fetch(last_cls) { last_cls }, last_cls_full, cls_errs) 71 | cls_assertions.push(assert_func) 72 | end 73 | last_cls = line.split("—")[1] 74 | .gsub(" ", "") 75 | .gsub("/", "") 76 | .gsub("\n", "") 77 | .sub(/\(\w*\)/, "") 78 | last_cls_full = line.gsub("\n", "") 79 | cls_errs.clear 80 | puts 81 | puts "// #{line}" 82 | when /^(\w{5})\s+(\w+)/ 83 | code = $1 84 | name = code_name_overrides.fetch(code) do 85 | $2.split("_").map(&:capitalize).join 86 | .gsub("Sql", "SQL") 87 | .gsub("Xml", "XML") 88 | .gsub("Fdw", "FDW") 89 | .gsub("Srf", "SRF") 90 | .gsub("Io", "IO") 91 | .gsub("Json", "JSON") 92 | end 93 | cls_errs.push(name) 94 | puts %Q[#{name} = "#{code}"] 95 | else 96 | puts line 97 | end 98 | end 99 | puts ")" 100 | 101 | if cls_errs.length > 0 102 | assert_func = build_assert_func(class_name_overrides.fetch(last_cls) { last_cls }, last_cls_full, cls_errs) 103 | cls_assertions.push(assert_func) 104 | end 105 | 106 | cls_assertions.each do |cls_assertion| 107 | puts cls_assertion 108 | end 109 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/jackc/pgerrcode 2 | 3 | go 1.12 4 | --------------------------------------------------------------------------------