Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: gajus/slonik
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v35.1.0
Choose a base ref
...
head repository: gajus/slonik
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v35.2.0
Choose a head ref
  • 1 commit
  • 2 files changed
  • 1 contributor

Commits on Sep 28, 2023

  1. Copy the full SHA
    f9ed121 View commit details
Showing with 33 additions and 9 deletions.
  1. +12 −8 src/errors.ts
  2. +21 −1 test/helpers/createIntegrationTests.ts
20 changes: 12 additions & 8 deletions src/errors.ts
Original file line number Diff line number Diff line change
@@ -113,11 +113,17 @@ export class SchemaValidationError extends SlonikError {
}

type IntegrityConstraintViolationErrorCause = Error & {
constraint: string;
column?: string;
constraint?: string;
table?: string;
};

export class IntegrityConstraintViolationError extends SlonikError {
public constraint: string;
public constraint: string | null;

public column: string | null;

public table: string | null;

public cause?: Error;

@@ -127,13 +133,11 @@ export class IntegrityConstraintViolationError extends SlonikError {
) {
super(message, { cause: error });

if (!error.constraint) {
throw new Error(
'IntegrityConstraintViolationError requires constraint name.',
);
}
this.constraint = error.constraint ?? null;

this.column = error.column ?? null;

this.constraint = error.constraint;
this.table = error.table ?? null;
}
}

22 changes: 21 additions & 1 deletion test/helpers/createIntegrationTests.ts
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ import {
type DatabasePoolConnection,
InputSyntaxError,
InvalidInputError,
NotNullIntegrityConstraintViolationError,
sql,
StatementCancelledError,
StatementTimeoutError,
@@ -85,7 +86,7 @@ export const createTestRunner = (
await connection.query(sql.unsafe`
CREATE TABLE person (
id SERIAL PRIMARY KEY,
name text,
name text NOT NULL,
tags text[],
birth_date date,
payload bytea,
@@ -140,6 +141,23 @@ export const createIntegrationTests = (
test: TestFn<TestContextType>,
PgPool: new () => PgPoolType,
) => {
test('NotNullIntegrityConstraintViolationError identifies the table and column', async (t) => {
const pool = await createPool(t.context.dsn, {
PgPool,
});

const error: NotNullIntegrityConstraintViolationError | undefined =
await t.throwsAsync(
pool.any(sql.unsafe`
INSERT INTO person (name) VALUES (null)
`),
);

t.true(error instanceof NotNullIntegrityConstraintViolationError);
t.is(error?.table, 'person');
t.is(error?.column, 'name');
});

test('properly handles terminated connections', async (t) => {
const pool = await createPool(t.context.dsn, {
PgPool,
@@ -700,10 +718,12 @@ export const createIntegrationTests = (
await pool.query(sql.unsafe`
INSERT INTO person
(
name,
payload
)
VALUES
(
'foo',
${sql.binary(Buffer.from(payload))}
)
`);