Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom constraint messages #214

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading