diff --git a/src/datasources/db/cargobikeAPI.ts b/src/datasources/db/cargobikeAPI.ts index 71ddab7..40f7093 100644 --- a/src/datasources/db/cargobikeAPI.ts +++ b/src/datasources/db/cargobikeAPI.ts @@ -30,6 +30,7 @@ import { EquipmentType } from '../../model/EquipmentType'; import { BikeEventType } from '../../model/BikeEventType'; import { Actions } from '../../model/ActionLog'; import { ResourceLockedError } from '../../errors/ResourceLockedError'; +import { NotFoundError } from '../../errors/NotFoundError'; /** * extended datasource to feed resolvers with data about cargoBikes @@ -82,10 +83,16 @@ export class CargoBikeAPI extends DataSource { } async lockCargoBike (id: number, userId: number) { + if (!await this.connection.getRepository(CargoBike).findOne(id)) { + throw new NotFoundError('CargoBike', 'id', id); + } return await LockUtils.lockEntity(this.connection, CargoBike, 'cb', id, userId); } async unlockCargoBike (id: number, userId: number) { + if (!await this.connection.getRepository(CargoBike).findOne(id)) { + throw new NotFoundError('CargoBike', 'id', id); + } return await LockUtils.unlockEntity(this.connection, CargoBike, 'cb', id, userId); } @@ -115,11 +122,13 @@ export class CargoBikeAPI extends DataSource { .set({ ...cargoBike }) .where('id = :id', { id: cargoBike.id }) .execute(); + equipmentTypeIds && await entityManager.getRepository(CargoBike) .createQueryBuilder('cb') .relation(CargoBike, 'equipmentTypeIds') .of(cargoBike.id) .addAndRemove(equipmentTypeIds, await this.equipmentTypeByCargoBikeId(cargoBike.id)); + equipmentIds && await entityManager.getRepository(CargoBike) .createQueryBuilder('cb') .relation(CargoBike, 'equipmentIds') diff --git a/src/datasources/db/contactinformationAPI.ts b/src/datasources/db/contactinformationAPI.ts index 5df7bc4..10e7959 100644 --- a/src/datasources/db/contactinformationAPI.ts +++ b/src/datasources/db/contactinformationAPI.ts @@ -22,9 +22,9 @@ import { Connection, EntityManager, getConnection } from 'typeorm'; import { ContactInformation } from '../../model/ContactInformation'; import { Person } from '../../model/Person'; import { ActionLogger, DBUtils, LockUtils } from './utils'; -import { GraphQLError } from 'graphql'; import { LendingStation } from '../../model/LendingStation'; import { ResourceLockedError } from '../../errors/ResourceLockedError'; +import { NotFoundError } from '../../errors/NotFoundError'; export class ContactInformationAPI extends DataSource { connection : Connection @@ -77,7 +77,11 @@ export class ContactInformationAPI extends DataSource { .update() .set({ ...person }) .where('id = :id', { id: person.id }) - .execute().then(value => { if (value.affected !== 1) { throw new GraphQLError('Id not found'); } }); + .execute().then(value => { + if (value.affected !== 1) { + throw new NotFoundError('Person', 'id', person.id); + } + }); }); !keepLock && await this.unlockPerson(person.id, userId); return this.personById(person.id); diff --git a/src/datasources/db/providerAPI.ts b/src/datasources/db/providerAPI.ts index 9350034..30fabc3 100644 --- a/src/datasources/db/providerAPI.ts +++ b/src/datasources/db/providerAPI.ts @@ -25,8 +25,8 @@ import { UserInputError } from 'apollo-server-express'; import { CargoBike } from '../../model/CargoBike'; import { LendingStation } from '../../model/LendingStation'; import { ActionLogger, DBUtils, LockUtils } from './utils'; -import { GraphQLError } from 'graphql'; import { ResourceLockedError } from '../../errors/ResourceLockedError'; +import { NotFoundError } from '../../errors/NotFoundError'; export class ProviderAPI extends DataSource { connection : Connection @@ -159,7 +159,11 @@ export class ProviderAPI extends DataSource { .update() .set({ ...provider }) .where('id = :id', { id: provider.id }) - .execute().then(value => { if (value.affected !== 1) { throw new GraphQLError('ID not found'); } }); + .execute().then(value => { + if (value.affected !== 1) { + throw new NotFoundError('Provider', 'id', provider.id); + } + }); await entityManager.getRepository(Provider) .createQueryBuilder('p') .relation(Provider, 'cargoBikeIds') diff --git a/src/datasources/db/utils.ts b/src/datasources/db/utils.ts index 6e8b3f9..d377977 100644 --- a/src/datasources/db/utils.ts +++ b/src/datasources/db/utils.ts @@ -22,6 +22,7 @@ import { Lockable } from '../../model/CargoBike'; import { ActionLog, Actions } from '../../model/ActionLog'; import { UserInputError } from 'apollo-server-express'; import { ResourceLockedError } from '../../errors/ResourceLockedError'; +import { NotFoundError } from '../../errors/NotFoundError'; export function genDateRange (struct: any) { if (!struct.dateRange || !struct.dateRange.from) { @@ -159,7 +160,7 @@ export class LockUtils { .select() .where(alias + '.id = :id', { id: id }) .getOne().catch(() => { - throw new UserInputError('ID not found'); + throw new NotFoundError(target.name, 'id', id); }); } @@ -302,7 +303,7 @@ export class ActionLogger { .where('id = :id', { id: updates.id }) .getRawOne().then(value => { if (value === undefined) { - throw new UserInputError('Id not found'); + throw new NotFoundError(target.name, 'id', updates.id); } return value; }); // use getRawOne to also get ids of related entities diff --git a/src/datasources/db/workshopAPI.ts b/src/datasources/db/workshopAPI.ts index 7c419f3..9c0e510 100644 --- a/src/datasources/db/workshopAPI.ts +++ b/src/datasources/db/workshopAPI.ts @@ -22,10 +22,9 @@ import { Connection, EntityManager, getConnection } from 'typeorm'; import { WorkshopType } from '../../model/WorkshopType'; import { Workshop } from '../../model/Workshop'; import { ActionLogger, DBUtils, LockUtils } from './utils'; -import { UserInputError } from 'apollo-server-express'; -import { GraphQLError } from 'graphql'; import { Participant } from '../../model/Participant'; import { ResourceLockedError } from '../../errors/ResourceLockedError'; +import { NotFoundError } from '../../errors/NotFoundError'; export class WorkshopAPI extends DataSource { connection: Connection @@ -67,7 +66,11 @@ export class WorkshopAPI extends DataSource { .set({ ...workshop }) .where('id = :id', { id: workshop.id }) .execute() - .then(value => { if (value.affected !== 1) { throw new GraphQLError('ID not found'); } }); + .then(value => { + if (value.affected !== 1) { + throw new NotFoundError('Workshop', 'id', workshop.id); + } + }); }); !keepLock && await this.unlockWorkshop(workshop.id, userId); return await this.workshopById(workshop.id); @@ -109,7 +112,11 @@ export class WorkshopAPI extends DataSource { .set({ ...workshopType }) .where('id = :id', { id: workshopType.id }) .execute() - .then(value => { if (value.affected !== 1) { throw new GraphQLError('ID not found'); } }); + .then(value => { + if (value.affected !== 1) { + throw new NotFoundError('WorkshopType', 'id', workshopType.id); + } + }); }); !keepLock && await this.unlockWorkshopType(workshopType.id, userId); return await this.workshopTypeById(workshopType.id); diff --git a/src/errors/NotFoundError.ts b/src/errors/NotFoundError.ts new file mode 100644 index 0000000..d0d1ecf --- /dev/null +++ b/src/errors/NotFoundError.ts @@ -0,0 +1,7 @@ +import { ApolloError } from 'apollo-server-express'; + +export class NotFoundError extends ApolloError { + constructor (type: string, identifierParam: string, identifierValue: any) { + super(`${type} with ${identifierParam} = '${identifierValue}' not found.`, 'ENTITY_NOT_FOUND'); + } +}