pgerrcode

repository·master·Indexed 42 days ago

https://github.com/jackc/pgerrcode

A library providing constants and helper functions for PostgreSQL error codes. It allows developers to programmatically identify and handle specific database errors using human-readable constants and boolean helper functions to categorize errors into classes.

Tokens
5.3K
Snippets
3
Records
5
Agent score
4%

What's inside pgerrcode

  1. Use pgerrcode for PostgreSQL error constants

    master
    The pgerrcode package provides a collection of constants representing PostgreSQL error codes. This allows developers to handle specific database errors in their code using human-readable constants instead of magic strings or raw error codes.
  2. Identify PostgreSQL error classes using helper functions

    master

    The pgerrcode package provides a suite of boolean helper functions to determine if a given PostgreSQL error code string belongs to a specific error class. Instead of manually checking against multiple constants, you can use these functions to categorize errors (e.g., checking if an error is a connection exception or a data exception).

    Available helper functions include:

    • IsSuccessfulCompletion(code string) bool
    • IsWarning(code string) bool
    • IsNoData(code string) bool
    • IsSQLStatementNotYetComplete(code string) bool
    • IsConnectionException(code string) bool
    • IsTriggeredActionException(code string) bool
    • IsFeatureNotSupported(code string) bool
    • IsInvalidTransactionInitiation(code string) bool
    • IsLocatorException(code string) bool
    • IsInvalidGrantor(code string) bool
    • IsInvalidRoleSpecification(code string) bool
    • IsDiagnosticsException(code string) bool
    • IsCaseNotFound(code string) bool
    • IsCardinalityViolation(code string) bool
    • IsDataException(code string) bool
    • IsIntegrityConstraintViolation(code string) bool
    • IsInvalidCursorState(code string) bool
    • IsInvalidTransactionState(code string) bool
    • IsInvalidSQLStatementName(code string) bool
    • IsTriggeredDataChangeViolation(code string) bool
    • IsInvalidAuthorizationSpecification(code string) bool
    • IsDependentPrivilegeDescriptorsStillExist(code string) bool
    • IsInvalidTransactionTermination(code string) bool
    • IsSQLRoutineException(code string) bool
    • IsInvalidCursorName(code string) bool
    • IsExternalRoutineException(code string) bool
    • IsExternalRoutineInvocationException(code string) bool
    • IsSavepointException(code string) bool
    • IsInvalidCatalogName(code string) bool
    • IsInvalidSchemaName(code string) bool
    • IsTransactionRollback(code string) bool
    • IsSyntaxErrororAccessRuleViolation(code string) bool
    • IsWithCheckOptionViolation(code string) bool
    • IsInsufficientResources(code string) bool
    • IsProgramLimitExceeded(code string) bool
    • IsObjectNotInPrerequisiteState(code string) bool
    • IsOperatorIntervention(code string) bool
    • IsSystemError(code string) bool
    • IsSnapshotFailure(code string) bool
    • IsConfigurationFileError(code string) bool
    • IsForeignDataWrapperError(code string) bool
    • IsPLpgSQLError(code string) bool
    • IsInternalError(code string) bool
  3. Get the name of a PostgreSQL error code using Name()

    master

    The Name function takes a PostgreSQL error code (as a string) and returns its corresponding human-readable string name. If the provided error code is not recognized by the library, it returns an empty string "".

    import "github.com/jackc/pgerrcode"
    
    // Example usage
    name := pgerrcode.Name("23505")
    // name is "UniqueViolation"
    
    unknown := pgerrcode.Name("invalid_code")
    // unknown is ""
  4. Reference PostgreSQL error code constants

    master

    The package exports string constants representing specific PostgreSQL error codes. These can be used for exact matching or passed to the Is... helper functions.

    Note: This segment contains constants for Classes 00 through XX (excluding some specific sub-classes).

    // Example of using constants for exact matching
    if errCode == pgerrcode.UniqueViolation {
        // handle unique constraint violation
    }
    
    // Example of using constants with helper functions
    if pgerrcode.IsConnectionException(errCode) {
        // handle connection issues
    }
  5. Reference: PostgreSQL error code mappings

    master

    The following is a mapping of PostgreSQL error codes to their string names as defined in the pgerrcode package. Use these codes with the Name() function to resolve error descriptions.

    // Mapping of PostgreSQL error codes to names
    // 00000: SuccessfulCompletion
    // 01000: Warning
    // 0100C: DynamicResultSetsReturned
    // 01008: ImplicitZeroBitPadding
    // 01003: NullValueEliminatedInSetFunction
    // 01007: PrivilegeNotGranted
    // 01006: PrivilegeNotRevoked
    // 01004: StringDataRightTruncationWarning
    // 01P01: DeprecatedFeature
    // 02000: NoData
    // 02001: NoAdditionalDynamicResultSetsReturned
    // 03000: SQLStatementNotYetComplete
    // 08000: ConnectionException
    // 08003: ConnectionDoesNotExist
    // 08006: ConnectionFailure
    // 08001: SQLClientUnableToEstablishSQLConnection
    // 08004: SQLServerRejectedEstablishmentOfSQLConnection
    // 08007: TransactionResolutionUnknown
    // 08P01: ProtocolViolation
    // 09000: TriggeredActionException
    // 0A000: FeatureNotSupported
    // 0B000: InvalidTransactionInitiation
    // 0F000: LocatorException
    // 0F001: InvalidLocatorSpecification
    // 0L000: InvalidGrantor
    // 0LP01: InvalidGrantOperation
    // 0P000: InvalidRoleSpecification
    // 0Z000: DiagnosticsException
    // 0Z002: StackedDiagnosticsAccessedWithoutActiveHandler
    // 20000: CaseNotFound
    // 21000: CardinalityViolation
    // 22000: DataException
    // 2202E: ArraySubscriptError
    // 22021: CharacterNotInRepertoire
    // 22008: DatetimeFieldOverflow
    // 22012: DivisionByZero
    // 22005: ErrorInAssignment
    // 2200B: EscapeCharacterConflict
    // 22022: IndicatorOverflow
    // 22015: IntervalFieldOverflow
    // 2201E: InvalidArgumentForLogarithm
    // 22014: InvalidArgumentForNtileFunction
    // 22016: InvalidArgumentForNthValueFunction
    // 2201F: InvalidArgumentForPowerFunction
    // 2201G: InvalidArgumentForWidthBucketFunction
    // 22018: InvalidCharacterValueForCast
    // 22007: InvalidDatetimeFormat
    // 22019: InvalidEscapeCharacter
    // 2200D: InvalidEscapeOctet
    // 22025: InvalidEscapeSequence
    // 22P06: NonstandardUseOfEscapeCharacter
    // 22010: InvalidIndicatorParameterValue
    // 22023: InvalidParameterValue
    // 22013: InvalidPrecedingOrFollowingSize
    // 2201B: InvalidRegularExpression
    // 2201W: InvalidRowCountInLimitClause
    // 2201X: InvalidRowCountInResultOffsetClause
    // 2202H: InvalidTablesampleArgument
    // 2202G: InvalidTablesampleRepeat
    // 22009: InvalidTimeZoneDisplacementValue
    // 2200C: InvalidUseOfEscapeCharacter
    // 2200G: MostSpecificTypeMismatch
    // 22004: NullValueNotAllowedDataException
    // 22002: NullValueNoIndicatorParameter
    // 22003: NumericValueOutOfRange
    // 2200H: SequenceGeneratorLimitExceeded
    // 22026: StringDataLengthMismatch
    // 22001: StringDataRightTruncationDataException
    // 22011: SubstringError
    // 22027: TrimError
    // 22024: UnterminatedCString
    // 2200F: ZeroLengthCharacterString
    // 22P01: FloatingPointException
    // 22P02: InvalidTextRepresentation
    // 22P03: InvalidBinaryRepresentation
    // 22P04: BadCopyFileFormat
    // 22P05: UntranslatableCharacter
    // 2200L: NotAnXMLDocument
    // 2200M: InvalidXMLDocument
    // 2200N: InvalidXMLContent
    // 2200S: InvalidXMLComment
    // 2200T: InvalidXMLProcessingInstruction
    // 22030: DuplicateJSONObjectKeyValue
    // 22031: InvalidArgumentForSQLJSONDatetimeFunction
    // 22032: InvalidJSONText
    // 22033: InvalidSQLJSONSubscript
    // 22034: MoreThanOneSQLJSONItem
    // 22035: NoSQLJSONItem
    // 22036: NonNumericSQLJSONItem
    // 22037: NonUniqueKeysInAJSONObject
    // 22038: SingletonSQLJSONItemRequired
    // 22039: SQLJSONArrayNotFound
    // 2203A: SQLJSONMemberNotFound
    // 2203B: SQLJSONNumberNotFound
    // 2203C: SQLJSONObjectNotFound
    // 2203D: TooManyJSONArrayElements
    // 2203E: TooManyJSONObjectMembers
    // 2203F: SQLJSONScalarRequired
    // 2203G: SQLJSONItemCannotBeCastToTargetType
    // 23000: IntegrityConstraintViolation
    // 23001: RestrictViolation
    // 23502: NotNullViolation
    // 23503: ForeignKeyViolation
    // 23505: UniqueViolation
    // 23514: CheckViolation
    // 23P01: ExclusionViolation
    // 24000: InvalidCursorState
    // 25000: InvalidTransactionState
    // 25001: ActiveSQLTransaction
    // 25002: BranchTransactionAlreadyActive
    // 25008: HeldCursorRequiresSameIsolationLevel
    // 25003: InappropriateAccessModeForBranchTransaction
    // 25004: InappropriateIsolationLevelForBranchTransaction
    // 25005: NoActiveSQLTransactionForBranchTransaction
    // 25006: ReadOnlySQLTransaction
    // 25007: SchemaAndDataStatementMixingNotSupported
    // 25P01: NoActiveSQLTransaction
    // 25P02: InFailedSQLTransaction
    // 25P03: IdleInTransactionSessionTimeout
    // 26000: InvalidSQLStatementName
    // 27000: TriggeredDataChangeViolation
    // 28000: InvalidAuthorizationSpecification
    // 28P01: InvalidPassword
    // 2B000: DependentPrivilegeDescriptorsStillExist
    // 2BP01: DependentObjectsStillExist
    // 2D000: InvalidTransactionTermination
    // 2F000: SQLRoutineException
    // 2F005: FunctionExecutedNoReturnStatement
    // 2F002: ModifyingSQLDataNotPermittedSQLRoutineException
    // 2F003: ProhibitedSQLStatementAttemptedSQLRoutineException
    // 2F004: ReadingSQLDataNotPermittedSQLRoutineException
    // 34000: InvalidCursorName
    // 38000: ExternalRoutineException
    // 38001: ContainingSQLNotPermitted
    // 38002: ModifyingSQLDataNotPermittedExternalRoutineException
    // 38003: ProhibitedSQLStatementAttemptedExternalRoutineException
    // 38004: ReadingSQLDataNotPermittedExternalRoutineException
    // 39000: ExternalRoutineInvocationException
    // 39001: InvalidSQLstateReturned
    // 39004: NullValueNotAllowedExternalRoutineInvocationException
    // 39P01: TriggerProtocolViolated
    // 39P02: SRFProtocolViolated
    // 39P03: EventTriggerProtocolViolated
    // 3B000: SavepointException
    // 3B001: InvalidSavepointSpecification
    // 3D000: InvalidCatalogName
    // 3F000: InvalidSchemaName
    // 40000: TransactionRollback
    // 40002: TransactionIntegrityConstraintViolation
    // 40001: SerializationFailure
    // 40003: StatementCompletionUnknown
    // 40P01: DeadlockDetected
    // 42000: SyntaxErrorOrAccessRuleViolation
    // 42601: SyntaxError
    // 42501: InsufficientPrivilege
    // 42846: CannotCoerce
    // 42803: GroupingError
    // 42P20: WindowingError
    // 42P19: InvalidRecursion
    // 42830: InvalidForeignKey
    // 42602: InvalidName
    // 42622: NameTooLong
    // 42939: ReservedName
    // 42804: DatatypeMismatch
    // 42P18: IndeterminateDatatype
    // 42P21: CollationMismatch
    // 42P22: IndeterminateCollation
    // 42809: WrongObjectType
    // 428C9: GeneratedAlways
    // 42703: UndefinedColumn
    // 42883: UndefinedFunction
    // 42P01: UndefinedTable
    // 42P02: UndefinedParameter
    // 42704: UndefinedObject
    // 42701: DuplicateColumn
    // 42P03: DuplicateCursor
    // 42P04: DuplicateDatabase
    // 42702: AmbiguousColumn
    // 42725: AmbiguousFunction
    // 42P08: AmbiguousParameter
    // 42P09: AmbiguousAlias
    // 42P10: InvalidColumnReference
    // 42611: InvalidColumnDefinition
    // 42P11: InvalidCursorDefinition
    // 42P12: InvalidDatabaseDefinition
    // 42P13: InvalidFunctionDefinition
    // 42P14: InvalidPreparedStatementDefinition
    // 42P15: InvalidSchemaDefinition
    // 42P16: InvalidTableDefinition
    // 42P17: InvalidObjectDefinition
    // 44000: WithCheckOptionViolation
    // 53000: InsufficientResources
    // 53100: DiskFull
    // 53200: OutOfMemory
    // 53300: TooManyConnections
    // 53400: ConfigurationLimitExceeded
    // 54000: ProgramLimitExceeded
    // 54001: StatementTooComplex
    // 54011: TooManyColumns
    // 54023: TooManyArguments
    // 55000: ObjectNotInPrerequisiteState
    // 55006: ObjectInUse
    // 55P02: CantChangeRuntimeParam
    // 55P03: LockNotAvailable
    // 55P04: UnsafeNewEnumValueUsage
    // 57000: OperatorIntervention
    // 57014: QueryCanceled
    // 57P01: AdminShutdown
    // 57P02: CrashShutdown
    // 57P03: CannotConnectNow
    // 57P04: DatabaseDropped
    // 57P05: IdleSessionTimeout
    // 58000: SystemError
    // 58030: IOError
    // 58P01: UndefinedFile
    // 58P02: DuplicateFile
    // 72000: SnapshotTooOld
    // F0000: ConfigFileError
    // F0001: LockFileExists
    // HV000: FDWError
    // HV005: FDWColumnNameNotFound
    // HV002: FDWDynamicParameterValueNeeded
    // HV010: FDWFunctionSequenceError
    // HV021: FDWInconsistentDescriptorInformation
    // HV024: FDWInvalidAttributeValue
    // HV007: FDWInvalidColumnName
    // HV008: FDWInvalidColumnNumber
    // HV004: FDWInvalidDataType
    // HV006: FDWInvalidDataTypeDescriptors
    // HV091: FDWInvalidDescriptorFieldIdentifier
    // HV00B: FDWInvalidHandle
    // HV00C: FDWInvalidOptionIndex
    // HV00D: FDWInvalidOptionName
    // HV090: FDWInvalidStringLengthOrBufferLength
    // HV00A: FDWInvalidStringFormat
    // HV009: FDWInvalidUseOfNullPointer
    // HV014: FDWTooManyHandles
    // HV001: FDWOutOfMemory
    // HV00P: FDWNoSchemas
    // HV00J: FDWOptionNameNotFound
    // HV00K: FDWReplyHandle
    // HV00Q: FDWSchemaNotFound
    // HV00R: FDWTableNotFound
    // HV00L: FDWUnableToCreateExecution
    // HV00M: FDWUnableToCreateReply
    // HV00N: FDWUnableToEstablishConnection
    // P0000: PLpgSQLError
    // P0001: RaiseException
    // P0002: NoDataFound
    // P0003: TooManyRows
    // P0004: AssertFailure
    // XX000: InternalError
    // XX001: DataCorrupted
    // XX002: IndexCorrupted