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.

49 lines
1.3 KiB
JavaScript

'use strict';
const { create } = require("error-chain");
const pgErrorCodes = require("pg-error-codes");
const DatabaseError = require("../database-error.js");
let UndefinedColumnError = create("UndefinedColumnError", { inheritsFrom: DatabaseError });
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;
let errorMessage;
if (table != null) {
errorMessage = `The '${column}' column does not exist in the '${table}' table`;
} else {
/* TODO: Maybe try to extract this from the query... somehow? */
errorMessage = `The '${column}' column does not exist in (unknown table)`;
}
return new UndefinedColumnError(errorMessage, {
originalError: error,
pgCode: error.code,
code: pgErrorCodes[error.code],
query: query,
table: table,
column: column,
});
}
};