From 6eb40d159cca19480a5f6d576c91388e827b6aa5 Mon Sep 17 00:00:00 2001 From: Jonas Lochmann Date: Mon, 5 Oct 2020 02:00:00 +0200 Subject: [PATCH] Wrap unique constraint and foreign key error objects --- .../apply-actions/dispatch-helper/helper.ts | 16 ++++++++++++++- .../sync/apply-actions/exception/database.ts | 20 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 src/function/sync/apply-actions/exception/database.ts diff --git a/src/function/sync/apply-actions/dispatch-helper/helper.ts b/src/function/sync/apply-actions/dispatch-helper/helper.ts index 7fb000f..4de6d25 100644 --- a/src/function/sync/apply-actions/dispatch-helper/helper.ts +++ b/src/function/sync/apply-actions/dispatch-helper/helper.ts @@ -15,8 +15,10 @@ * along with this program. If not, see . */ +import { ForeignKeyConstraintError, UniqueConstraintError } from 'sequelize' import { ClientPushChangesRequestAction } from '../../../../api/schema' import { EventHandler } from '../../../../monitoring/eventhandler' +import { ApplyActionDatabaseException } from '../exception/database' import { ApplyActionException } from '../exception/index' import { EncodedActionSchemaMismatchException } from '../exception/invalidaction' import { parseEncodedAction } from '../parse-encoded-action' @@ -40,7 +42,19 @@ export async function dispatch ({ type, action, try { const parsedAction = parser(parsedSerializedAction) - await applier(parsedAction) + try { + await applier(parsedAction) + } catch (ex) { + if (ex instanceof UniqueConstraintError) { + throw new ApplyActionDatabaseException({ + staticMessage: 'database unique constraint violation of the fields ' + Object.keys(ex.fields).join(', ') + }) + } else if (ex instanceof ForeignKeyConstraintError) { + throw new ApplyActionDatabaseException({ + staticMessage: 'database foreign key violation at the table ' + ex.table + '/' + ex.index + }) + } else throw ex + } eventHandler.countEvent('dispatched action:' + actionType) } catch (ex) { diff --git a/src/function/sync/apply-actions/exception/database.ts b/src/function/sync/apply-actions/exception/database.ts new file mode 100644 index 0000000..a310dc0 --- /dev/null +++ b/src/function/sync/apply-actions/exception/database.ts @@ -0,0 +1,20 @@ +/* + * server component for the TimeLimit App + * Copyright (C) 2019 - 2020 Jonas Lochmann + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import { ApplyActionException } from './index' + +export class ApplyActionDatabaseException extends ApplyActionException {}