Add NotFoundError class

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/29/head
trivernis 4 years ago
parent cfaf2ef462
commit 1906d8e6e9
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -30,6 +30,7 @@ import { EquipmentType } from '../../model/EquipmentType';
import { BikeEventType } from '../../model/BikeEventType'; import { BikeEventType } from '../../model/BikeEventType';
import { Actions } from '../../model/ActionLog'; import { Actions } from '../../model/ActionLog';
import { ResourceLockedError } from '../../errors/ResourceLockedError'; import { ResourceLockedError } from '../../errors/ResourceLockedError';
import { NotFoundError } from '../../errors/NotFoundError';
/** /**
* extended datasource to feed resolvers with data about cargoBikes * extended datasource to feed resolvers with data about cargoBikes
@ -82,10 +83,16 @@ export class CargoBikeAPI extends DataSource {
} }
async lockCargoBike (id: number, userId: number) { 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); return await LockUtils.lockEntity(this.connection, CargoBike, 'cb', id, userId);
} }
async unlockCargoBike (id: number, userId: number) { 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); return await LockUtils.unlockEntity(this.connection, CargoBike, 'cb', id, userId);
} }
@ -115,11 +122,13 @@ export class CargoBikeAPI extends DataSource {
.set({ ...cargoBike }) .set({ ...cargoBike })
.where('id = :id', { id: cargoBike.id }) .where('id = :id', { id: cargoBike.id })
.execute(); .execute();
equipmentTypeIds && await entityManager.getRepository(CargoBike) equipmentTypeIds && await entityManager.getRepository(CargoBike)
.createQueryBuilder('cb') .createQueryBuilder('cb')
.relation(CargoBike, 'equipmentTypeIds') .relation(CargoBike, 'equipmentTypeIds')
.of(cargoBike.id) .of(cargoBike.id)
.addAndRemove(equipmentTypeIds, await this.equipmentTypeByCargoBikeId(cargoBike.id)); .addAndRemove(equipmentTypeIds, await this.equipmentTypeByCargoBikeId(cargoBike.id));
equipmentIds && await entityManager.getRepository(CargoBike) equipmentIds && await entityManager.getRepository(CargoBike)
.createQueryBuilder('cb') .createQueryBuilder('cb')
.relation(CargoBike, 'equipmentIds') .relation(CargoBike, 'equipmentIds')

@ -22,9 +22,9 @@ import { Connection, EntityManager, getConnection } from 'typeorm';
import { ContactInformation } from '../../model/ContactInformation'; import { ContactInformation } from '../../model/ContactInformation';
import { Person } from '../../model/Person'; import { Person } from '../../model/Person';
import { ActionLogger, DBUtils, LockUtils } from './utils'; import { ActionLogger, DBUtils, LockUtils } from './utils';
import { GraphQLError } from 'graphql';
import { LendingStation } from '../../model/LendingStation'; import { LendingStation } from '../../model/LendingStation';
import { ResourceLockedError } from '../../errors/ResourceLockedError'; import { ResourceLockedError } from '../../errors/ResourceLockedError';
import { NotFoundError } from '../../errors/NotFoundError';
export class ContactInformationAPI extends DataSource { export class ContactInformationAPI extends DataSource {
connection : Connection connection : Connection
@ -77,7 +77,11 @@ export class ContactInformationAPI extends DataSource {
.update() .update()
.set({ ...person }) .set({ ...person })
.where('id = :id', { id: person.id }) .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); !keepLock && await this.unlockPerson(person.id, userId);
return this.personById(person.id); return this.personById(person.id);

@ -25,8 +25,8 @@ import { UserInputError } from 'apollo-server-express';
import { CargoBike } from '../../model/CargoBike'; import { CargoBike } from '../../model/CargoBike';
import { LendingStation } from '../../model/LendingStation'; import { LendingStation } from '../../model/LendingStation';
import { ActionLogger, DBUtils, LockUtils } from './utils'; import { ActionLogger, DBUtils, LockUtils } from './utils';
import { GraphQLError } from 'graphql';
import { ResourceLockedError } from '../../errors/ResourceLockedError'; import { ResourceLockedError } from '../../errors/ResourceLockedError';
import { NotFoundError } from '../../errors/NotFoundError';
export class ProviderAPI extends DataSource { export class ProviderAPI extends DataSource {
connection : Connection connection : Connection
@ -159,7 +159,11 @@ export class ProviderAPI extends DataSource {
.update() .update()
.set({ ...provider }) .set({ ...provider })
.where('id = :id', { id: provider.id }) .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) await entityManager.getRepository(Provider)
.createQueryBuilder('p') .createQueryBuilder('p')
.relation(Provider, 'cargoBikeIds') .relation(Provider, 'cargoBikeIds')

@ -22,6 +22,7 @@ import { Lockable } from '../../model/CargoBike';
import { ActionLog, Actions } from '../../model/ActionLog'; import { ActionLog, Actions } from '../../model/ActionLog';
import { UserInputError } from 'apollo-server-express'; import { UserInputError } from 'apollo-server-express';
import { ResourceLockedError } from '../../errors/ResourceLockedError'; import { ResourceLockedError } from '../../errors/ResourceLockedError';
import { NotFoundError } from '../../errors/NotFoundError';
export function genDateRange (struct: any) { export function genDateRange (struct: any) {
if (!struct.dateRange || !struct.dateRange.from) { if (!struct.dateRange || !struct.dateRange.from) {
@ -159,7 +160,7 @@ export class LockUtils {
.select() .select()
.where(alias + '.id = :id', { id: id }) .where(alias + '.id = :id', { id: id })
.getOne().catch(() => { .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 }) .where('id = :id', { id: updates.id })
.getRawOne().then(value => { .getRawOne().then(value => {
if (value === undefined) { if (value === undefined) {
throw new UserInputError('Id not found'); throw new NotFoundError(target.name, 'id', updates.id);
} }
return value; return value;
}); // use getRawOne to also get ids of related entities }); // use getRawOne to also get ids of related entities

@ -22,10 +22,9 @@ import { Connection, EntityManager, getConnection } from 'typeorm';
import { WorkshopType } from '../../model/WorkshopType'; import { WorkshopType } from '../../model/WorkshopType';
import { Workshop } from '../../model/Workshop'; import { Workshop } from '../../model/Workshop';
import { ActionLogger, DBUtils, LockUtils } from './utils'; import { ActionLogger, DBUtils, LockUtils } from './utils';
import { UserInputError } from 'apollo-server-express';
import { GraphQLError } from 'graphql';
import { Participant } from '../../model/Participant'; import { Participant } from '../../model/Participant';
import { ResourceLockedError } from '../../errors/ResourceLockedError'; import { ResourceLockedError } from '../../errors/ResourceLockedError';
import { NotFoundError } from '../../errors/NotFoundError';
export class WorkshopAPI extends DataSource { export class WorkshopAPI extends DataSource {
connection: Connection connection: Connection
@ -67,7 +66,11 @@ export class WorkshopAPI extends DataSource {
.set({ ...workshop }) .set({ ...workshop })
.where('id = :id', { id: workshop.id }) .where('id = :id', { id: workshop.id })
.execute() .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); !keepLock && await this.unlockWorkshop(workshop.id, userId);
return await this.workshopById(workshop.id); return await this.workshopById(workshop.id);
@ -109,7 +112,11 @@ export class WorkshopAPI extends DataSource {
.set({ ...workshopType }) .set({ ...workshopType })
.where('id = :id', { id: workshopType.id }) .where('id = :id', { id: workshopType.id })
.execute() .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); !keepLock && await this.unlockWorkshopType(workshopType.id, userId);
return await this.workshopTypeById(workshopType.id); return await this.workshopTypeById(workshopType.id);

@ -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');
}
}
Loading…
Cancel
Save