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 { 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')

@ -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);

@ -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')

@ -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

@ -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);

@ -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