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.
41 lines
1.3 KiB
JavaScript
41 lines
1.3 KiB
JavaScript
'use strict';
|
|
|
|
const createError = require("create-error");
|
|
const pgErrorCodes = require("pg-error-codes");
|
|
|
|
const DatabaseError = require("../database-error.js");
|
|
const getTable = require("../get-table");
|
|
|
|
let UndefinedColumnError = createError(DatabaseError, "UndefinedColumnError");
|
|
|
|
let messageRegex = /^(.+) - column "([^"]+)" of relation "([^"]+)" does not exist$/;
|
|
|
|
module.exports = {
|
|
error: UndefinedColumnError,
|
|
errorName: "UndefinedColumnError",
|
|
check: function checkType(error) {
|
|
return (
|
|
// PostgreSQL (via `pg`):
|
|
(error.length != null && error.file != null && error.line != null && error.routine != null && error.code === "42703")
|
|
)
|
|
},
|
|
convert: function convertError(error) {
|
|
let messageMatch = messageRegex.exec(error.message);
|
|
|
|
if (messageMatch == null) {
|
|
throw new Error("Encountered unknown error format");
|
|
}
|
|
|
|
let [_, query, column, table] = messageMatch;
|
|
|
|
return new UndefinedColumnError(`The '${column}' column does not exist in the '${table}' table`, {
|
|
originalError: error,
|
|
pgCode: error.code,
|
|
code: pgErrorCodes[error.code],
|
|
query: query,
|
|
table: table,
|
|
column: column,
|
|
});
|
|
}
|
|
};
|