Skip to content

Commit c49d150

Browse files
committed
CreationFailed error class
This DRYs the strings and allows for identification programmatically & statically
1 parent 6805860 commit c49d150

34 files changed

+107
-73
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { EnhancedResource } from '~/common';
2+
import type { ResourceLike } from '~/core';
3+
import { ServerException } from './exception';
4+
5+
export class CreationFailed extends ServerException {
6+
readonly resource: EnhancedResource<any>;
7+
constructor(
8+
resource: ResourceLike,
9+
options?: { message?: string; cause?: Error },
10+
) {
11+
const res = EnhancedResource.resolve(resource);
12+
super(options?.message ?? `Failed to create ${res.name}`, options?.cause);
13+
this.resource = res;
14+
}
15+
}

src/common/exceptions/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ export * from './unauthenticated.exception';
77
export * from './unauthorized.exception';
88
export * from './service-unavailable.exception';
99
export * from './invalid-id-for-type.exception';
10+
export * from './creation-failed.exception';

src/components/budget/budget-record.repository.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { Injectable } from '@nestjs/common';
22
import { node, Query, relation } from 'cypher-query-builder';
33
import { pickBy } from 'lodash';
44
import {
5+
CreationFailed,
56
ID,
67
labelForView,
78
NotFoundException,
89
ObjectView,
9-
ServerException,
1010
Session,
1111
UnsecuredDto,
1212
} from '~/common';
@@ -69,7 +69,7 @@ export class BudgetRecordRepository extends DtoRepository<
6969
.return<{ id: ID }>('node.id as id')
7070
.first();
7171
if (!result) {
72-
throw new ServerException('Failed to create a budget record');
72+
throw new CreationFailed(BudgetRecord);
7373
}
7474
return result.id;
7575
}

src/components/budget/budget.repository.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { Injectable } from '@nestjs/common';
22
import { inArray, node, Query, relation } from 'cypher-query-builder';
33
import { pickBy } from 'lodash';
44
import {
5+
CreationFailed,
56
ID,
67
labelForView,
78
NotFoundException,
89
ObjectView,
9-
ServerException,
1010
Session,
1111
UnsecuredDto,
1212
viewOfChangeset,
@@ -64,7 +64,7 @@ export class BudgetRepository extends DtoRepository<
6464
.first();
6565

6666
if (!result) {
67-
throw new ServerException('Failed to create budget');
67+
throw new CreationFailed(Budget);
6868
}
6969

7070
return result.id;

src/components/budget/budget.service.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Injectable } from '@nestjs/common';
22
import {
3+
CreationFailed,
34
DuplicateException,
45
generateId,
56
ID,
@@ -78,7 +79,7 @@ export class BudgetService {
7879
userId: session.userId,
7980
exception,
8081
});
81-
throw new ServerException('Could not create budget', exception);
82+
throw new CreationFailed(Budget, { cause: exception });
8283
}
8384
}
8485

@@ -119,7 +120,7 @@ export class BudgetService {
119120
userId: session.userId,
120121
exception,
121122
});
122-
throw new ServerException('Could not create Budget Record', exception);
123+
throw new CreationFailed(BudgetRecord, { cause: exception });
123124
}
124125
}
125126

src/components/ceremony/ceremony.repository.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Injectable } from '@nestjs/common';
22
import { node, Query, relation } from 'cypher-query-builder';
3-
import { ID, ServerException, Session, UnsecuredDto } from '~/common';
3+
import { CreationFailed, ID, Session, UnsecuredDto } from '~/common';
44
import { DtoRepository } from '~/core/database';
55
import {
66
ACTIVE,
@@ -38,7 +38,7 @@ export class CeremonyRepository extends DtoRepository<
3838
.first();
3939

4040
if (!result) {
41-
throw new ServerException('failed to create a ceremony');
41+
throw new CreationFailed(Ceremony);
4242
}
4343
return result;
4444
}

src/components/comments/comment.service.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Injectable } from '@nestjs/common';
22
import { difference } from 'lodash';
33
import {
4+
CreationFailed,
45
ID,
56
InvalidIdForTypeException,
67
isIdLike,
@@ -52,7 +53,7 @@ export class CommentService {
5253
try {
5354
const result = await this.repo.create(input, session);
5455
if (!result) {
55-
throw new ServerException('Failed to create comment');
56+
throw new CreationFailed(Comment);
5657
}
5758
dto = await this.repo.readOne(result.id);
5859
} catch (exception) {
@@ -66,7 +67,7 @@ export class CommentService {
6667
);
6768
}
6869

69-
throw new ServerException('Failed to create comment', exception);
70+
throw new CreationFailed(Comment, { cause: exception });
7071
}
7172

7273
const mentionees = this.mentionNotificationService.extract(dto);

src/components/engagement/engagement.repository.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { difference, pickBy, upperFirst } from 'lodash';
1212
import { DateTime } from 'luxon';
1313
import { MergeExclusive } from 'type-fest';
1414
import {
15+
CreationFailed,
1516
DuplicateException,
1617
generateId,
1718
ID,
@@ -268,7 +269,7 @@ export class EngagementRepository extends CommonRepository {
268269

269270
const result = await query.first();
270271
if (!result) {
271-
throw new ServerException('Could not create Language Engagement');
272+
throw new CreationFailed(LanguageEngagement);
272273
}
273274

274275
await this.files.createDefinedFile(
@@ -359,7 +360,7 @@ export class EngagementRepository extends CommonRepository {
359360
);
360361
}
361362

362-
throw new ServerException('Could not create Internship Engagement');
363+
throw new CreationFailed(InternshipEngagement);
363364
}
364365

365366
await this.files.createDefinedFile(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { Injectable } from '@nestjs/common';
22
import { Query } from 'cypher-query-builder';
33
import {
4+
CreationFailed,
45
DuplicateException,
56
ID,
67
PaginatedListType,
7-
ServerException,
88
Session,
99
UnsecuredDto,
1010
} from '~/common';
@@ -55,7 +55,7 @@ export class EthnoArtRepository extends DtoRepository(EthnoArt) {
5555
.first();
5656

5757
if (!result) {
58-
throw new ServerException('Failed to create ethno art');
58+
throw new CreationFailed(EthnoArt);
5959
}
6060

6161
await this.scriptureRefsService.create(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { Injectable } from '@nestjs/common';
22
import { node, Query, relation } from 'cypher-query-builder';
33
import {
4+
CreationFailed,
45
DuplicateException,
56
ID,
67
SecuredList,
7-
ServerException,
88
Session,
99
UnsecuredDto,
1010
} from '~/common';
@@ -55,7 +55,7 @@ export class FieldRegionRepository extends DtoRepository(FieldRegion) {
5555

5656
const result = await query.first();
5757
if (!result) {
58-
throw new ServerException('failed to create field region');
58+
throw new CreationFailed(FieldRegion);
5959
}
6060

6161
return await this.readOne(result.id);

0 commit comments

Comments
 (0)