Skip to content

Commit

Permalink
Add support for prettifying index constraint errors with their descri…
Browse files Browse the repository at this point in the history
…ption

Depends-on: balena-io-modules/abstract-sql-compiler#237
Change-type: minor
  • Loading branch information
thgreasi committed Jan 5, 2024
1 parent c79a7de commit 9ebe36b
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/sbvr-api/sbvr-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,26 @@ const prettifyConstraintError = (
case 'postgres': {
const resourceName = resolveSynonym(request);
const abstractSqlModel = getFinalAbstractSqlModel(request);
matches = new RegExp(
'"' + abstractSqlModel.tables[resourceName].name + '_(.*?)_key"',
).exec(err.message);
const table = abstractSqlModel.tables[resourceName];
matches = new RegExp('"' + table.name + '_(.*?)_key"').exec(
err.message,
);
if (matches == null && table.indexes.length > 0) {
const violatedConstraintName =
/duplicate key value violates unique constraint "(.*)"/.exec(
err.message,
)?.[1];
if (violatedConstraintName != null) {
const index = table.indexes.find(
(idx) =>
idx.name === violatedConstraintName &&

Check failure on line 269 in src/sbvr-api/sbvr-utils.ts

View workflow job for this annotation

GitHub Actions / Flowzone / Test npm (16.x)

Property 'name' does not exist on type '{ type: string; fields: string[]; }'.

Check failure on line 269 in src/sbvr-api/sbvr-utils.ts

View workflow job for this annotation

GitHub Actions / Flowzone / Test npm (18.x)

Property 'name' does not exist on type '{ type: string; fields: string[]; }'.

Check failure on line 269 in src/sbvr-api/sbvr-utils.ts

View workflow job for this annotation

GitHub Actions / Flowzone / Test npm (20.x)

Property 'name' does not exist on type '{ type: string; fields: string[]; }'.
idx.description != null,

Check failure on line 270 in src/sbvr-api/sbvr-utils.ts

View workflow job for this annotation

GitHub Actions / Flowzone / Test npm (16.x)

Property 'description' does not exist on type '{ type: string; fields: string[]; }'.

Check failure on line 270 in src/sbvr-api/sbvr-utils.ts

View workflow job for this annotation

GitHub Actions / Flowzone / Test npm (18.x)

Property 'description' does not exist on type '{ type: string; fields: string[]; }'.

Check failure on line 270 in src/sbvr-api/sbvr-utils.ts

View workflow job for this annotation

GitHub Actions / Flowzone / Test npm (20.x)

Property 'description' does not exist on type '{ type: string; fields: string[]; }'.
);
if (index != null) {
throw new BadRequestError(index.description);

Check failure on line 273 in src/sbvr-api/sbvr-utils.ts

View workflow job for this annotation

GitHub Actions / Flowzone / Test npm (16.x)

Property 'description' does not exist on type '{ type: string; fields: string[]; }'.

Check failure on line 273 in src/sbvr-api/sbvr-utils.ts

View workflow job for this annotation

GitHub Actions / Flowzone / Test npm (18.x)

Property 'description' does not exist on type '{ type: string; fields: string[]; }'.

Check failure on line 273 in src/sbvr-api/sbvr-utils.ts

View workflow job for this annotation

GitHub Actions / Flowzone / Test npm (20.x)

Property 'description' does not exist on type '{ type: string; fields: string[]; }'.
}
}
}
break;
}
}
Expand Down

0 comments on commit 9ebe36b

Please sign in to comment.