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.
36 lines
1.3 KiB
JavaScript
36 lines
1.3 KiB
JavaScript
'use strict';
|
|
|
|
const createError = require("create-error");
|
|
const pgErrorCodes = require("pg-error-codes");
|
|
const DatabaseError = require("../database-error.js");
|
|
|
|
let ForeignKeyConstraintViolationError = createError(DatabaseError, "ForeignKeyConstraintViolationError");
|
|
|
|
let detailsRegex = /^Key \(([^\)]+)\)=\(([^\)]+)\) is not present in table "([^"]+)"\.$/;
|
|
|
|
module.exports = {
|
|
error: ForeignKeyConstraintViolationError,
|
|
errorName: "ForeignKeyConstraintViolationError",
|
|
check: function checkType(error) {
|
|
return (
|
|
// PostgreSQL (via `pg`):
|
|
(error.length != null && error.file != null && error.line != null && error.routine != null && error.code === "23503")
|
|
)
|
|
},
|
|
convert: function convertError(error) {
|
|
let [_, column, value, foreignTable] = detailsRegex.exec(error.detail);
|
|
|
|
return new ForeignKeyConstraintViolationError(`Value for column '${column}' in table '${error.table}' refers to a non-existent key '${value}' in table '${foreignTable}'`, {
|
|
originalError: error,
|
|
pgCode: error.code,
|
|
code: pgErrorCodes[error.code],
|
|
schema: error.schema,
|
|
table: error.table,
|
|
foreignTable: foreignTable,
|
|
column: column,
|
|
value: value,
|
|
constraint: error.constraint
|
|
});
|
|
}
|
|
};
|