K
Kysely14mo ago
tzezar

How to retrieve field name raising error in database?

I am using kysely, zod, expressjs. I need to return field error and display it to the end user in frontend form. However I am not sure how to do it with kysely. Code for backend is pretty simple:
const FormData = z.object({
name: z.string({ required_error: "name is required" }),
});

export const createUnitOfMeasurement: RequestHandler = async (
req,
res,
next
) => {
try {
const { name } = FormData.parse(req.body);
const r = await db
.insertInto("unit_of_measurement")
.values({
name: name,
})
.returningAll()
.executeTakeFirstOrThrow();
res.send(r);
} catch (err) {
next(err);
}
};
const FormData = z.object({
name: z.string({ required_error: "name is required" }),
});

export const createUnitOfMeasurement: RequestHandler = async (
req,
res,
next
) => {
try {
const { name } = FormData.parse(req.body);
const r = await db
.insertInto("unit_of_measurement")
.values({
name: name,
})
.returningAll()
.executeTakeFirstOrThrow();
res.send(r);
} catch (err) {
next(err);
}
};
and next(err) is catching error and sending it in response with valid status code. However this is error generated by Kysely: <Error in second message> I would be super gratefull if you give me any suggestion how to extract field name raising error.
3 Replies
tzezar
tzezarOP14mo ago
error: podwójna wartość klucza narusza ograniczenie unikalności "unit_of_measurement_name_5aac1413_uniq"
at H:\projects\snappy\backend_node\node_modules\pg\lib\client.js:526:17
at processTicksAndRejections (node:internal/process/task_queues:95:5)
at async PostgresConnection.executeQuery
at async DefaultQueryExecutor.executeQuery (H:\projects\snappy\backend_node\node_modules\kysely\dist\cjs\query-executor\query-executor-base.js:36:16)
at async InsertQueryBuilder.execute (H:\projects\snappy\backend_node\node_modules\kysely\dist\cjs\query-builder\insert-query-builder.js:517:24)
at async InsertQueryBuilder.executeTakeFirst (H:\projects\snappy\backend_node\node_modules\kysely\dist\cjs\query-builder\insert-query-builder.js:532:26)
at async InsertQueryBuilder.executeTakeFirstOrThrow (H:\projects\snappy\backend_node\node_modules\kysely\dist\cjs\query-builder\insert-query-builder.js:544:24) {
length: 275,
severity: 'BŁĄD',
code: '23505',
detail: 'Klucz (name)=(Kilogram) już istnieje.',
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: 'public',
table: 'unit_of_measurement',
column: undefined,
dataType: undefined,
constraint: 'unit_of_measurement_name_5aac1413_uniq',
file: 'nbtinsert.c',
line: '673',
routine: '_bt_check_unique'
}
POST /api/units-of-measurement 500 51.603 ms - 45
error: podwójna wartość klucza narusza ograniczenie unikalności "unit_of_measurement_name_5aac1413_uniq"
at H:\projects\snappy\backend_node\node_modules\pg\lib\client.js:526:17
at processTicksAndRejections (node:internal/process/task_queues:95:5)
at async PostgresConnection.executeQuery
at async DefaultQueryExecutor.executeQuery (H:\projects\snappy\backend_node\node_modules\kysely\dist\cjs\query-executor\query-executor-base.js:36:16)
at async InsertQueryBuilder.execute (H:\projects\snappy\backend_node\node_modules\kysely\dist\cjs\query-builder\insert-query-builder.js:517:24)
at async InsertQueryBuilder.executeTakeFirst (H:\projects\snappy\backend_node\node_modules\kysely\dist\cjs\query-builder\insert-query-builder.js:532:26)
at async InsertQueryBuilder.executeTakeFirstOrThrow (H:\projects\snappy\backend_node\node_modules\kysely\dist\cjs\query-builder\insert-query-builder.js:544:24) {
length: 275,
severity: 'BŁĄD',
code: '23505',
detail: 'Klucz (name)=(Kilogram) już istnieje.',
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: 'public',
table: 'unit_of_measurement',
column: undefined,
dataType: undefined,
constraint: 'unit_of_measurement_name_5aac1413_uniq',
file: 'nbtinsert.c',
line: '673',
routine: '_bt_check_unique'
}
POST /api/units-of-measurement 500 51.603 ms - 45
In fact, this is my first commercial project in node, having previously worked in go and python. And if it's not a problem with kysely, but for example postgres driver then please let me know.
Igal (mobile)
Igal (mobile)14mo ago
Hey 👋 This is a pg error. refer to their docs.
tzezar
tzezarOP14mo ago
thanks ❤️
Want results from more Discord servers?
Add your server