From 5f2ebf47eb462c868f13ee49860cb0dc4b20fb95 Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Sat, 13 Jan 2024 22:27:37 +0100 Subject: [PATCH] Wrap errors using db-errors --- package.json | 1 + src/index.js | 31 +++++++++++++++++++++++++------ yarn.lock | 5 +++++ 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index fe20fd2..8bb2b37 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "license": "WTFPL OR CC0-1.0", "dependencies": { "dataloader": "^2.2.2", + "db-errors": "^0.2.3", "match-value": "^1.1.0", "syncpipe": "^1.0.0" } diff --git a/src/index.js b/src/index.js index ccf3f70..2e77e71 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,7 @@ const assert = require("assert"); const matchValue = require("match-value"); +const dbErrors = require("db-errors"); const knexIntrospect = require("./introspect"); const dataloaderFromKnexResults = require("./dataloader-from-knex-results"); @@ -12,6 +13,14 @@ const dataloaderFromKnexResults = require("./dataloader-from-knex-results"); - Update record: give each type an update method for that individual record, by ID? For "get previous value, then set new value" style usecases */ +async function wrapErrors(callback) { + try { + return callback(); + } catch (error) { + throw dbErrors.wrapError(error); + } +} + function applyFilterPredicate(query, column, predicate, negate = false) { if (predicate.__comparator === "not") { return applyFilterPredicate(query, column, predicate.comparator, true); @@ -111,8 +120,10 @@ module.exports = { remoteTypes[remoteTypeName][remoteField] = async function(_, { $make, $getProperty, knex }) { let tx = (knex ?? globalKnex); - let result = await tx(tableName).columns("id").where({ - [idFieldName]: await $getProperty(this, primaryKey) + let result = await wrapErrors(async () => { + return tx(tableName).columns("id").where({ + [idFieldName]: await $getProperty(this, primaryKey) + }); }); return result.map((item) => { @@ -205,7 +216,9 @@ module.exports = { query = query.limit(limit); } - let result = await query; + let result = await wrapErrors(async () => { + return query; + }); // TODO: Map result(s) to types if (first === true) { @@ -227,7 +240,9 @@ module.exports = { let query = tx(tableName).delete(); query = applyFilterArgument(query, filter); - let deletedRowCount = await query; + let deletedRowCount = await wrapErrors(async () => { + return query; + }); return { count: deletedRowCount }; }, @@ -246,7 +261,9 @@ module.exports = { applyFilterArgument(query, filter); } - let affected = await query; + let affected = await wrapErrors(async () => { + return query; + }); return affected.map((item) => { dlayerKnexTable.clear(item.id); // yeet from dataloader cache, so that we fetch the updated version instead @@ -263,7 +280,9 @@ module.exports = { .insert(values) .returning("id"); - let result = await query; + let result = await wrapErrors(async () => { + return query; + }); if (isArray) { return result.map((item) => { diff --git a/yarn.lock b/yarn.lock index 5f37056..85cae6b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -12,6 +12,11 @@ dataloader@^2.2.2: resolved "https://registry.yarnpkg.com/dataloader/-/dataloader-2.2.2.tgz#216dc509b5abe39d43a9b9d97e6e5e473dfbe3e0" integrity sha512-8YnDaaf7N3k/q5HnTJVuzSyLETjoZjVmHc4AeKAzOvKHEFQKcn64OKBfzHYtE9zGjctNM7V9I0MfnUVLpi7M5g== +db-errors@^0.2.3: + version "0.2.3" + resolved "https://registry.yarnpkg.com/db-errors/-/db-errors-0.2.3.tgz#a6a38952e00b20e790f2695a6446b3c65497ffa2" + integrity sha512-OOgqgDuCavHXjYSJoV2yGhv6SeG8nk42aoCSoyXLZUH7VwFG27rxbavU1z+VrZbZjphw5UkDQwUlD21MwZpUng== + match-value@^1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/match-value/-/match-value-1.1.0.tgz#ad311ef8bbe2d344a53ec3104e28fe221984b98e"