|
|
|
'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
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|