diff --git a/package-lock.json b/package-lock.json index 1440ba6..8208baa 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "chanfana", - "version": "2.5.0", + "version": "2.5.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "chanfana", - "version": "2.5.0", + "version": "2.5.1", "license": "MIT", "dependencies": { "@asteasolutions/zod-to-openapi": "^7.2.0", diff --git a/package.json b/package.json index 6364d93..385f806 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/endpoints/d1/create.ts b/src/endpoints/d1/create.ts index c29f845..5312fb9 100644 --- a/src/endpoints/d1/create.ts +++ b/src/endpoints/d1/create.ts @@ -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 = Array> extends CreateEndpoint { dbName = "DB"; logger?: Logger; + constraintsMessages: Record = {}; getDBBinding(): D1Database { const env = this.params.router.getBindings(this.args); @@ -36,11 +37,9 @@ export class D1CreateEndpoint = Array> 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]; } } diff --git a/src/endpoints/d1/update.ts b/src/endpoints/d1/update.ts index a93e79d..d5913a7 100644 --- a/src/endpoints/d1/update.ts +++ b/src/endpoints/d1/update.ts @@ -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 = Array> extends UpdateEndpoint { dbName = "DB"; logger?: Logger; + constraintsMessages: Record = {}; getDBBinding(): D1Database { const env = this.params.router.getBindings(this.args); @@ -74,6 +75,13 @@ export class D1UpdateEndpoint = Array> } 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); }