Wrap unique constraint and foreign key error objects

This commit is contained in:
Jonas Lochmann
2020-10-05 02:00:00 +02:00
parent 946381226f
commit 6eb40d159c
2 changed files with 35 additions and 1 deletions
@@ -15,8 +15,10 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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<T1 extends { type: string }, T2> ({ 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) {
@@ -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 <https://www.gnu.org/licenses/>.
*/
import { ApplyActionException } from './index'
export class ApplyActionDatabaseException extends ApplyActionException {}