You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
1.2 KiB
JavaScript
35 lines
1.2 KiB
JavaScript
'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
|
|
});
|
|
}
|
|
};
|