All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cc.otavia.postgres.PostgresException.scala Maven / Gradle / Ivy

/*
 * Copyright 2022 Yan Kun 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cc.otavia.postgres

import cc.otavia.sql.DatabaseException
import cc.otavia.postgres.PostgresException.formatMessage

import scala.language.unsafeNulls

/** PostgreSQL error including all fields
 *  of the ErrorResponse message of the PostgreSQL frontend/backend protocol.
 *
 *  @param errorMessage
 *    the primary human-readable error message ('M' field)
 *  @param severity
 *    the severity: ERROR, FATAL, or PANIC, or a localized translation of one of these ('S' field)
 *  @param code
 *    use [[sqlState]] instead. the SQLSTATE code for the error ('S' field, value list), it is never localized
 *  @param detail
 *    an optional secondary error message carrying more detail about the problem ('D' field), a newline indicates
 *    paragraph break.
 *  @param hint
 *    an optional suggestion (advice) what to do about the problem ('H' field), a newline indicates
 *    paragraph break.
 *  @param position
 *    a decimal ASCII integer, indicating an error cursor position as an index into the original query string. ('P' field). The first character has
 *    index 1, and positions are measured in characters not bytes.
 *  @param internalPosition
 *    a decimal ASCII integer, indicating an error cursor position ('p' field) as an index into the
 *    internally generated command (see 'q' field).
 *  @param internalQuery
 *    the text of a failed internally-generated command ('q' field).
 *  @param where
 *    an indication of the context in which the error occurred ('W' field). Presently this includes
 *    a call stack traceback of active procedural language functions and internally-generated queries. The trace is one
 *    entry per line, most recent first.
 *  @param file
 *    file name of the source-code location where the error was reported ('F' field).
 *  @param line
 *    line number of the source-code location where the error was reported ('L' field).
 *  @param routine
 *    name of the source-code routine reporting the error ('R' field).
 *  @param schema
 *    if the error was associated with a specific database object, the name of the schema containing that object, if any
 *    ('s' field).
 *  @param table
 *    if the error was associated with a specific table, the name of the table ('t' field).
 *  @param column
 *    if the error was associated with a specific table column, the name of the column ('c' field).
 *  @param dataType
 *    if the error was associated with a specific data type, the name of the data type ('d' field).
 *  @param constraint
 *    if the error was associated with a specific constraint, the name of the constraint ('n' field).
 */
case class PostgresException(
    errorMessage: String | Null,
    severity: String | Null,
    code: String | Null,
    detail: String | Null,
    hint: String | Null,
    position: String | Null,
    internalPosition: String | Null,
    internalQuery: String | Null,
    where: String | Null,
    file: String | Null,
    line: String | Null,
    routine: String | Null,
    schema: String | Null,
    table: String | Null,
    column: String | Null,
    dataType: String | Null,
    constraint: String | Null
) extends DatabaseException(formatMessage(errorMessage, severity, code), 0, code)

object PostgresException {

    private def formatMessage(errorMessage: String | Null, severity: String | Null, code: String | Null): String = {
        val sb = new StringBuilder()
        if (severity != null) sb.append(severity).append(":")
        if (errorMessage != null) {
            if (sb.nonEmpty) sb.append(" ")
            sb.append(errorMessage)
        }
        if (code != null) {
            if (sb.nonEmpty) sb.append(" ")
            sb.append("(").append(code).append(")")
        }
        sb.toString()
    }

    def apply(
        errorMessage: String | Null,
        severity: String | Null,
        code: String | Null,
        detail: String | Null
    ): PostgresException = new PostgresException(
      errorMessage,
      severity,
      code,
      detail,
      null,
      null,
      null,
      null,
      null,
      null,
      null,
      null,
      null,
      null,
      null,
      null,
      null
    )

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy