Skip to content

Commit 60183a9

Browse files
committed
Handle not found exceptions in Elastic repositories
1 parent afde98e commit 60183a9

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

src/Contexts/Shared/infrastructure/persistence/elasticsearch/ElasticRepository.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { Client as ElasticClient } from '@elastic/elasticsearch';
2+
import { ResponseError } from '@elastic/elasticsearch/lib/errors';
3+
import httpStatus from 'http-status';
24
import { AggregateRoot } from '../../../domain/AggregateRoot';
35

46
type Hit = { _source: any };
@@ -15,16 +17,27 @@ export abstract class ElasticRepository<T extends AggregateRoot> {
1517
protected async searchAllInElastic(unserializer: (data: any) => T): Promise<T[]> {
1618
const client = await this.client();
1719

18-
const response = await client.search({
19-
index: this.moduleName(),
20-
body: {
21-
query: {
22-
match_all: {}
20+
try {
21+
const response = await client.search({
22+
index: this.moduleName(),
23+
body: {
24+
query: {
25+
match_all: {}
26+
}
2327
}
28+
});
29+
return response.body.hits.hits.map((hit: Hit) => unserializer({ ...hit._source }));
30+
} catch (e) {
31+
if (this.isNotFoundError(e)) {
32+
return [];
2433
}
25-
});
2634

27-
return response.body.hits.hits.map((hit: Hit) => unserializer({ ...hit._source }));
35+
throw e;
36+
}
37+
}
38+
39+
private isNotFoundError(e: any) {
40+
return e instanceof ResponseError && (e as ResponseError).meta.statusCode === httpStatus.NOT_FOUND;
2841
}
2942

3043
protected async persist(aggregateRoot: T): Promise<void> {

tests/Contexts/Backoffice/Courses/infrastructure/BackofficeCourseRepository.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import { BackofficeCourseMother } from '../domain/BackofficeCourseMother';
77
const repository: ElasticBackofficeCourseRepository = container.get('Backoffice.courses.BackofficeCourseRepository');
88
const environmentArranger: Promise<EnvironmentArranger> = container.get('Backoffice.Backend.EnvironmentArranger');
99

10+
beforeEach(async () => {
11+
await (await environmentArranger).arrange();
12+
});
13+
1014
afterEach(async () => {
1115
await (await environmentArranger).arrange();
1216
});
@@ -20,7 +24,7 @@ describe('BackofficeCourseRepository', () => {
2024

2125
const expectedCourses = await repository.searchAll();
2226

23-
expect(courses.length).toEqual(expectedCourses.length);
27+
expect(courses).toHaveLength(expectedCourses.length);
2428
expect(courses.sort(sort)).toEqual(expectedCourses.sort(sort));
2529
});
2630
});

tests/Contexts/Mooc/Courses/infrastructure/persistence/CourseRepository.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ beforeEach(async () => {
1111
});
1212

1313
afterAll(async () => {
14+
await (await environmentArranger).arrange();
1415
await (await environmentArranger).close();
1516
});
1617

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ beforeEach(async () => {
1111
});
1212

1313
afterAll(async () => {
14+
await (await environmentArranger).arrange();
1415
await (await environmentArranger).close();
1516
});
1617

17-
describe('MongoCoursesCounterRepository', () => {
18+
describe('CoursesCounterRepository', () => {
1819
describe('#save', () => {
1920
it('should save a courses counter', async () => {
2021
const course = CoursesCounterMother.random();

0 commit comments

Comments
 (0)