Skip to content

Commit

Permalink
Add custom constraint messages (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
G4brym authored Dec 13, 2024
1 parent 42256ee commit 8a50429
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chanfana",
"version": "2.5.0",
"version": "2.5.1",
"description": "OpenAPI 3 and 3.1 schema generator and validator for Hono, itty-router and more!",
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down
11 changes: 5 additions & 6 deletions src/endpoints/d1/create.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { ApiException, InputValidationException } from "../../exceptions";
import { ApiException, type InputValidationException } from "../../exceptions";
import { CreateEndpoint } from "../create";
import type { Logger, O } from "../types";

export class D1CreateEndpoint<HandleArgs extends Array<object> = Array<object>> extends CreateEndpoint<HandleArgs> {
dbName = "DB";
logger?: Logger;
constraintsMessages: Record<string, InputValidationException> = {};

getDBBinding(): D1Database {
const env = this.params.router.getBindings(this.args);
Expand Down Expand Up @@ -36,11 +37,9 @@ export class D1CreateEndpoint<HandleArgs extends Array<object> = Array<object>>
if (this.logger)
this.logger.error(`Caught exception while trying to create ${this.meta.model.tableName}: ${e.message}`);
if (e.message.includes("UNIQUE constraint failed")) {
if (e.message.includes(this.meta.model.tableName) && e.message.includes(this.meta.model.primaryKeys[0])) {
throw new InputValidationException(`An object with this ${this.meta.model.primaryKeys[0]} already exists`, [
"body",
this.meta.model.primaryKeys[0],
]);
const constraintMessage = e.message.split("UNIQUE constraint failed:")[1].split(":")[0].trim();
if (this.constraintsMessages[constraintMessage]) {
throw this.constraintsMessages[constraintMessage];
}
}

Expand Down
10 changes: 9 additions & 1 deletion src/endpoints/d1/update.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { ApiException } from "../../exceptions";
import { ApiException, type InputValidationException } from "../../exceptions";
import type { Logger, O, UpdateFilters } from "../types";
import { UpdateEndpoint } from "../update";

export class D1UpdateEndpoint<HandleArgs extends Array<object> = Array<object>> extends UpdateEndpoint<HandleArgs> {
dbName = "DB";
logger?: Logger;
constraintsMessages: Record<string, InputValidationException> = {};

getDBBinding(): D1Database {
const env = this.params.router.getBindings(this.args);
Expand Down Expand Up @@ -74,6 +75,13 @@ export class D1UpdateEndpoint<HandleArgs extends Array<object> = Array<object>>
} catch (e: any) {
if (this.logger)
this.logger.error(`Caught exception while trying to update ${this.meta.model.tableName}: ${e.message}`);
if (e.message.includes("UNIQUE constraint failed")) {
const constraintMessage = e.message.split("UNIQUE constraint failed:")[1].split(":")[0].trim();
if (this.constraintsMessages[constraintMessage]) {
throw this.constraintsMessages[constraintMessage];
}
}

throw new ApiException(e.message);
}

Expand Down

0 comments on commit 8a50429

Please sign in to comment.