'use strict'; const createError = require("create-error"); const pgErrorCodes = require("pg-error-codes"); const DatabaseError = require("../database-error.js"); let UniqueConstraintViolationError = createError(DatabaseError, "UniqueConstraintViolationError"); let detailsRegex = /Key \(([^\)]+)\)=\(([^\)]+)\) already exists\./; module.exports = { error: UniqueConstraintViolationError, errorName: "UniqueConstraintViolationError", check: function checkType(error) { return ( // PostgreSQL (via `pg`): (error.length != null && error.file != null && error.line != null && error.routine != null && error.code === "23505") ) }, convert: function convertError(error) { let [_, column, value] = detailsRegex.exec(error.detail); return new UniqueConstraintViolationError(`Value '${value}' already exists for column '${column}' in table '${error.table}'`, { originalError: error, pgCode: error.code, code: pgErrorCodes[error.code], schema: error.schema, table: error.table, column: column, value: value, constraint: error.constraint }); } };