Skip to content

Commit 15f3617

Browse files
committed
chore: improve error resolving of prisma errors
1 parent 6b38c00 commit 15f3617

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

apps/api/src/instrument-records/instrument-records.service.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -336,23 +336,21 @@ export class InstrumentRecordsService {
336336

337337
private async createSubjectIfNotFound(subjectId: string) {
338338
try {
339-
await this.subjectsService.findById(subjectId);
339+
return await this.subjectsService.findById(subjectId);
340340
} catch (exception) {
341341
if (exception instanceof NotFoundException) {
342342
const addedSubject: CreateSubjectDto = {
343343
id: subjectId
344344
};
345345
try {
346-
await this.subjectsService.create(addedSubject);
346+
return await this.subjectsService.create(addedSubject);
347347
} catch (prismaError) {
348-
if (prismaError instanceof Prisma.PrismaClientKnownRequestError) {
349-
// The .code property can be accessed in a type-safe manner
350-
// eslint-disable-next-line max-depth
351-
if (prismaError.code === 'P2002') {
352-
console.error(prismaError);
353-
}
348+
if (prismaError instanceof Prisma.PrismaClientKnownRequestError && prismaError.code === 'P2002') {
349+
console.error(prismaError);
350+
return await this.subjectsService.findById(subjectId);
351+
} else {
352+
throw prismaError;
354353
}
355-
throw prismaError;
356354
}
357355
} else {
358356
throw exception;

0 commit comments

Comments
 (0)