From ee79eba04ef6d0a3445b1d2fe2c5e54e4b1d5e4b Mon Sep 17 00:00:00 2001 From: prostgles Date: Wed, 31 Jan 2024 11:07:16 +0200 Subject: [PATCH] improve error structure --- lib/DboBuilder/dboBuilderUtils.ts | 2 +- package-lock.json | 4 ++-- package.json | 2 +- tests/isomorphicQueries.spec.ts | 23 ++++++++++++++++++++++- tests/server/package-lock.json | 2 +- 5 files changed, 27 insertions(+), 6 deletions(-) diff --git a/lib/DboBuilder/dboBuilderUtils.ts b/lib/DboBuilder/dboBuilderUtils.ts index 8dbfce75..fe2a1fec 100644 --- a/lib/DboBuilder/dboBuilderUtils.ts +++ b/lib/DboBuilder/dboBuilderUtils.ts @@ -71,7 +71,7 @@ export function getSerializedClientErrorFromPGError(rawError: any, args: GetSeri const errObject = { ...((!localParams || !localParams.socket) ? err : {}), - ...pickKeys(err, ["column", "code", "table", "constraint", "hint"]), + ...pickKeys(err, ["column", "code", "table", "constraint", "hint", "severity", "message", "name", "detail"]), }; if (view?.dboBuilder?.constraints && errObject.constraint && !errObject.column) { const constraint = view.dboBuilder.constraints diff --git a/package-lock.json b/package-lock.json index 2adc5590..006c61b8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "prostgles-server", - "version": "4.2.13", + "version": "4.2.14", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "prostgles-server", - "version": "4.2.13", + "version": "4.2.14", "license": "MIT", "dependencies": { "@types/express": "^4.17.13", diff --git a/package.json b/package.json index 4e5cf0af..2fd2bde3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "prostgles-server", - "version": "4.2.13", + "version": "4.2.14", "description": "", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/tests/isomorphicQueries.spec.ts b/tests/isomorphicQueries.spec.ts index 225ea412..20b73339 100644 --- a/tests/isomorphicQueries.spec.ts +++ b/tests/isomorphicQueries.spec.ts @@ -3,11 +3,12 @@ import * as fs from "fs"; import { DBOFullyTyped } from "../dist/DBSchemaBuilder"; import type { DBHandlerClient } from "./client/index"; import { test, describe } from "node:test"; +import { pickKeys } from "prostgles-types"; export const isomorphicQueries = async (db: DBOFullyTyped | DBHandlerClient, log: (msg: string, extra?: any) => void) => { log("Starting isomorphic queries"); - + const isServer = !!(db.items as any).dboBuilder; await describe("Isomorphic queries", async () => { await test("Deleting stale data", async () => { const itemsCount = await db.items.count!() @@ -26,6 +27,26 @@ export const isomorphicQueries = async (db: DBOFullyTyped | DBHandlerClient, log }); + await test("Insert error structure", async () => { + const err = await db.items.insert!({ h: "a" }).catch(err => err); + const clientOnlyError = { + detail: 'Array value must start with "{" or dimension information.', + message: 'malformed array literal: "a"', + code: '22P02', + name: 'error', + severity: 'ERROR', + stack: [ + 'dbo.items.insert()' + ] + } + if(isServer){ + assert.deepStrictEqual(pickKeys(err, Object.keys(clientOnlyError)), clientOnlyError); + assert.equal(typeof err.query, "string"); + } else { + assert.deepStrictEqual(err, clientOnlyError); + } + }); + await test("Prepare data", async () => { if(!db.sql) throw "db.sql missing"; const res = await db.items.insert!([{ name: "a" }, { name: "a" }, { name: "b" }], { returning: "*" }); diff --git a/tests/server/package-lock.json b/tests/server/package-lock.json index c9dea758..b51d7930 100644 --- a/tests/server/package-lock.json +++ b/tests/server/package-lock.json @@ -21,7 +21,7 @@ }, "../..": { "name": "prostgles-server", - "version": "4.2.13", + "version": "4.2.14", "license": "MIT", "dependencies": { "@types/express": "^4.17.13",