UNPKG

@directus/api

Version:

Directus is a real-time API and App dashboard for managing SQL database content

84 lines (82 loc) 2.84 kB
import { ContainsNullValuesError, InvalidForeignKeyError, NotNullViolationError, RecordNotUniqueError, ValueOutOfRangeError, ValueTooLongError } from "@directus/errors"; //#region src/database/errors/dialects/postgres.ts var PostgresErrorCodes = /* @__PURE__ */ function(PostgresErrorCodes$1) { PostgresErrorCodes$1["FOREIGN_KEY_VIOLATION"] = "23503"; PostgresErrorCodes$1["NOT_NULL_VIOLATION"] = "23502"; PostgresErrorCodes$1["NUMERIC_VALUE_OUT_OF_RANGE"] = "22003"; PostgresErrorCodes$1["UNIQUE_VIOLATION"] = "23505"; PostgresErrorCodes$1["VALUE_LIMIT_VIOLATION"] = "22001"; return PostgresErrorCodes$1; }(PostgresErrorCodes || {}); function extractError(error, data) { switch (error.code) { case PostgresErrorCodes.UNIQUE_VIOLATION: return uniqueViolation(); case PostgresErrorCodes.NUMERIC_VALUE_OUT_OF_RANGE: return numericValueOutOfRange(); case PostgresErrorCodes.VALUE_LIMIT_VIOLATION: return valueLimitViolation(); case PostgresErrorCodes.NOT_NULL_VIOLATION: return notNullViolation(); case PostgresErrorCodes.FOREIGN_KEY_VIOLATION: return foreignKeyViolation(); default: return error; } function uniqueViolation() { const { table, detail } = error; const matches = detail.match(/\(([^)]+)\)/g); if (!matches) return error; const collection = table; const field = matches[0].slice(1, -1); return new RecordNotUniqueError({ collection, field, value: field ? data[field] : null }); } function numericValueOutOfRange() { const matches = error.message.match(/"(.*?)"/g); if (!matches) return error; const collection = matches[0].slice(1, -1); const fields = Object.keys(data); const field = fields.length === 1 ? fields[0] : null; return new ValueOutOfRangeError({ collection, field, value: field ? data[field] : null }); } function valueLimitViolation() { const matches = error.message.match(/"(.*?)"/g); if (!matches) return error; const collection = matches[0].slice(1, -1); const fields = Object.keys(data); const field = fields.length === 1 ? fields[0] : null; return new ValueTooLongError({ collection, field, value: field ? data[field] : null }); } function notNullViolation() { const { table, column } = error; if (!column) return error; if (error.message.endsWith("contains null values")) return new ContainsNullValuesError({ collection: table, field: column }); return new NotNullViolationError({ collection: table, field: column }); } function foreignKeyViolation() { const { table, detail } = error; const matches = detail.match(/\(([^)]+)\)/g); if (!matches) return error; const collection = table; const field = matches[0].slice(1, -1); return new InvalidForeignKeyError({ collection, field, value: field ? data[field] : null }); } } //#endregion export { extractError };