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.
node-database-error/src/errors/not-null-constraint-violati...

37 lines
1.2 KiB
JavaScript

'use strict';
const { create } = require("error-chain");
const pgErrorCodes = require("pg-error-codes");
const DatabaseError = require("../database-error.js");
const getValues = require("../get-values");
let NotNullConstraintViolationError = create(DatabaseError, "NotNullConstraintViolationError", { inheritsFrom: DatabaseError });
let messageRegex = /^(.+) - null value in column "([^"]+)" violates not-null constraint$/;
module.exports = {
error: NotNullConstraintViolationError,
errorName: "NotNullConstraintViolationError",
check: function checkType(error) {
return (
// PostgreSQL (via `pg`):
(error.length != null && error.file != null && error.line != null && error.routine != null && error.code === "23502")
);
},
convert: function convertError(error) {
let [_, query, _column] = messageRegex.exec(error.message);
return new NotNullConstraintViolationError(`Missing required value for column '${error.column}' in table '${error.table}'`, {
originalError: error,
pgCode: error.code,
code: pgErrorCodes[error.code],
schema: error.schema,
table: error.table,
column: error.column,
values: getValues(error.detail),
query: query
});
}
};