Skip to content

Commit

Permalink
Merge pull request #327 from jiho-kr/feature/conflict_exception
Browse files Browse the repository at this point in the history
feat: throw confilctException, when query error occurs
  • Loading branch information
jiho-kr authored Oct 11, 2023
2 parents edbad77 + d342b12 commit 0b6244b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
16 changes: 16 additions & 0 deletions spec/unique-key/unique.controller.create.mysql.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,20 @@ describe('UniqueController', () => {
await request(app.getHttpServer()).post('/unique').send(toCreate).expect(HttpStatus.CONFLICT);
});
});

describe('UPDATE', () => {
it('should throw when cannot update duplicated entities', async () => {
const name = `name-${Date.now()}`;
await request(app.getHttpServer()).post('/unique').send({ name }).expect(HttpStatus.CREATED);
const { body: created } = await request(app.getHttpServer())
.post('/unique')
.send({ name: name + '2' })
.expect(HttpStatus.CREATED);
await request(app.getHttpServer()).patch(`/unique/${created.id}`).send({ name }).expect(HttpStatus.CONFLICT);
await request(app.getHttpServer())
.patch(`/unique/${created.id}`)
.send({ name: name + '3' })
.expect(HttpStatus.OK);
});
});
});
17 changes: 11 additions & 6 deletions src/lib/crud.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ export class CrudService<T extends BaseEntity> {
? result.map((entity) => this.excludeEntity(entity, crudCreateRequest.exclude))
: this.excludeEntity(result[0], crudCreateRequest.exclude);
})
.catch((error) => {
throw new ConflictException(error);
});
.catch(this.throwConflictException);
};

readonly reservedUpsert = async (crudUpsertRequest: CrudUpsertRequest<T>): Promise<T> => {
Expand All @@ -104,7 +102,8 @@ export class CrudService<T extends BaseEntity> {

return this.repository
.save(_.assign(upsertEntity, crudUpsertRequest.body))
.then((entity) => this.excludeEntity(entity, crudUpsertRequest.exclude));
.then((entity) => this.excludeEntity(entity, crudUpsertRequest.exclude))
.catch(this.throwConflictException);
});
};

Expand All @@ -120,7 +119,8 @@ export class CrudService<T extends BaseEntity> {

return this.repository
.save(_.assign(entity, crudUpdateOneRequest.body))
.then((entity) => this.excludeEntity(entity, crudUpdateOneRequest.exclude));
.then((entity) => this.excludeEntity(entity, crudUpdateOneRequest.exclude))
.catch(this.throwConflictException);
});
};

Expand Down Expand Up @@ -148,7 +148,7 @@ export class CrudService<T extends BaseEntity> {
if (!entity) {
throw new NotFoundException();
}
await this.repository.recover(entity);
await this.repository.recover(entity).catch(this.throwConflictException);
return this.excludeEntity(entity, crudRecoverRequest.exclude);
});
};
Expand Down Expand Up @@ -178,4 +178,9 @@ export class CrudService<T extends BaseEntity> {
}
return entity;
}

private throwConflictException(error: Error): never {
Logger.error(error);
throw new ConflictException(error);
}
}

0 comments on commit 0b6244b

Please sign in to comment.