Skip to content

Commit c2b679a

Browse files
committed
Ensure that invalid reads immediately following a creation throw a server error
This is a server problem, not a client one.
1 parent c49d150 commit c2b679a

File tree

20 files changed

+154
-22
lines changed

20 files changed

+154
-22
lines changed

src/common/exceptions/creation-failed.exception.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,16 @@ export class CreationFailed extends ServerException {
1313
this.resource = res;
1414
}
1515
}
16+
17+
export class ReadAfterCreationFailed extends CreationFailed {
18+
constructor(
19+
resource: ResourceLike,
20+
options?: { message?: string; cause?: Error },
21+
) {
22+
const res = EnhancedResource.resolve(resource);
23+
super(res, {
24+
message: `Failed to retrieve ${res.name} after creation`,
25+
...options,
26+
});
27+
}
28+
}

src/components/engagement/engagement.repository.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
labelForView,
2121
NotFoundException,
2222
ObjectView,
23+
ReadAfterCreationFailed,
2324
ServerException,
2425
Session,
2526
typenameForView,
@@ -286,7 +287,11 @@ export class EngagementRepository extends CommonRepository {
286287
result.id,
287288
session,
288289
viewOfChangeset(changeset),
289-
)) as UnsecuredDto<LanguageEngagement>;
290+
).catch((e) => {
291+
throw e instanceof NotFoundException
292+
? new ReadAfterCreationFailed(LanguageEngagement)
293+
: e;
294+
})) as UnsecuredDto<LanguageEngagement>;
290295
}
291296

292297
async createInternshipEngagement(
@@ -377,7 +382,11 @@ export class EngagementRepository extends CommonRepository {
377382
result.id,
378383
session,
379384
viewOfChangeset(changeset),
380-
)) as UnsecuredDto<InternshipEngagement>;
385+
).catch((e) => {
386+
throw e instanceof NotFoundException
387+
? new ReadAfterCreationFailed(InternshipEngagement)
388+
: e;
389+
})) as UnsecuredDto<InternshipEngagement>;
381390
}
382391

383392
getActualLanguageChanges = getChanges(LanguageEngagement);

src/components/ethno-art/ethno-art.repository.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import {
44
CreationFailed,
55
DuplicateException,
66
ID,
7+
NotFoundException,
78
PaginatedListType,
9+
ReadAfterCreationFailed,
810
Session,
911
UnsecuredDto,
1012
} from '~/common';
@@ -64,7 +66,11 @@ export class EthnoArtRepository extends DtoRepository(EthnoArt) {
6466
session,
6567
);
6668

67-
return await this.readOne(result.id);
69+
return await this.readOne(result.id).catch((e) => {
70+
throw e instanceof NotFoundException
71+
? new ReadAfterCreationFailed(EthnoArt)
72+
: e;
73+
});
6874
}
6975

7076
async update(input: UpdateEthnoArt) {

src/components/field-region/field-region.repository.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import {
44
CreationFailed,
55
DuplicateException,
66
ID,
7+
NotFoundException,
8+
ReadAfterCreationFailed,
79
SecuredList,
810
Session,
911
UnsecuredDto,
@@ -58,7 +60,11 @@ export class FieldRegionRepository extends DtoRepository(FieldRegion) {
5860
throw new CreationFailed(FieldRegion);
5961
}
6062

61-
return await this.readOne(result.id);
63+
return await this.readOne(result.id).catch((e) => {
64+
throw e instanceof NotFoundException
65+
? new ReadAfterCreationFailed(FieldRegion)
66+
: e;
67+
});
6268
}
6369

6470
async update(changes: UpdateFieldRegion) {

src/components/field-zone/field-zone.repository.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {
55
CreationFailed,
66
DuplicateException,
77
ID,
8+
NotFoundException,
9+
ReadAfterCreationFailed,
810
SecuredList,
911
Session,
1012
UnsecuredDto,
@@ -58,7 +60,11 @@ export class FieldZoneRepository extends DtoRepository(FieldZone) {
5860
throw new CreationFailed(FieldZone);
5961
}
6062

61-
return await this.readOne(result.id);
63+
return await this.readOne(result.id).catch((e) => {
64+
throw e instanceof NotFoundException
65+
? new ReadAfterCreationFailed(FieldZone)
66+
: e;
67+
});
6268
}
6369

6470
protected hydrate() {

src/components/film/film.repository.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import {
44
CreationFailed,
55
DuplicateException,
66
ID,
7+
NotFoundException,
78
PaginatedListType,
9+
ReadAfterCreationFailed,
810
Session,
911
UnsecuredDto,
1012
} from '~/common';
@@ -59,7 +61,11 @@ export class FilmRepository extends DtoRepository(Film) {
5961
session,
6062
);
6163

62-
return await this.readOne(result.id);
64+
return await this.readOne(result.id).catch((e) => {
65+
throw e instanceof NotFoundException
66+
? new ReadAfterCreationFailed(Film)
67+
: e;
68+
});
6369
}
6470

6571
async update(input: UpdateFilm) {

src/components/funding-account/funding-account.service.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
ID,
66
NotFoundException,
77
ObjectView,
8+
ReadAfterCreationFailed,
89
SecuredList,
910
ServerException,
1011
Session,
@@ -51,7 +52,11 @@ export class FundingAccountService {
5152

5253
this.logger.info(`funding account created`, { id: result.id });
5354

54-
return await this.readOne(result.id, session);
55+
return await this.readOne(result.id, session).catch((e) => {
56+
throw e instanceof NotFoundException
57+
? new ReadAfterCreationFailed(FundingAccount)
58+
: e;
59+
});
5560
} catch (err) {
5661
this.logger.error('Could not create funding account for user', {
5762
exception: err,

src/components/language/ethnologue-language/ethnologue-language.repository.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import {
44
CreationFailed,
55
DuplicateException,
66
ID,
7+
NotFoundException,
8+
ReadAfterCreationFailed,
79
ServerException,
810
} from '~/common';
911
import { DtoRepository, UniquenessError } from '~/core/database';
@@ -56,7 +58,11 @@ export class EthnologueLanguageRepository extends DtoRepository(
5658
throw new CreationFailed(EthnologueLanguage);
5759
}
5860

59-
return await this.readOne(result.id);
61+
return await this.readOne(result.id).catch((e) => {
62+
throw e instanceof NotFoundException
63+
? new ReadAfterCreationFailed(EthnologueLanguage)
64+
: e;
65+
});
6066
}
6167

6268
async update(changes: UpdateEthnologueLanguage & { id: ID }) {

src/components/language/language.repository.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
labelForView,
1616
NotFoundException,
1717
ObjectView,
18+
ReadAfterCreationFailed,
1819
ServerException,
1920
Session,
2021
UnsecuredDto,
@@ -126,7 +127,11 @@ export class LanguageRepository extends DtoRepository<
126127
throw new CreationFailed(Language);
127128
}
128129

129-
return await this.readOne(result.id, session);
130+
return await this.readOne(result.id, session).catch((e) => {
131+
throw e instanceof NotFoundException
132+
? new ReadAfterCreationFailed(Language)
133+
: e;
134+
});
130135
}
131136

132137
async update(

src/components/location/location.repository.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
DuplicateException,
77
generateId,
88
ID,
9+
NotFoundException,
10+
ReadAfterCreationFailed,
911
ServerException,
1012
Session,
1113
UnsecuredDto,
@@ -74,7 +76,11 @@ export class LocationRepository extends DtoRepository(Location) {
7476
throw new CreationFailed(Location);
7577
}
7678

77-
const dto = await this.readOne(result.id);
79+
const dto = await this.readOne(result.id).catch((e) => {
80+
throw e instanceof NotFoundException
81+
? new ReadAfterCreationFailed(Location)
82+
: e;
83+
});
7884

7985
await this.files.createDefinedFile(
8086
mapImageId,

0 commit comments

Comments
 (0)