Skip to content

Commit d57721b

Browse files
committed
refactor getMi for better not found handling
1 parent 5568bae commit d57721b

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

lambdas/api-handler/src/services/mi-operations.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import { MIRepository } from "@internal/datastore/src/mi-repository";
22
import { GetMIResponse, IncomingMI, PostMIResponse } from "../contracts/mi";
33
import { mapToGetMIResponse, mapToPostMIResponse } from "../mappers/mi-mapper";
4+
import { ApiErrorDetail } from "../contracts/errors";
5+
import NotFoundError from "../errors/not-found-error";
6+
7+
function isNotFoundError(error: any) {
8+
return (
9+
error instanceof Error &&
10+
/^Management Information with id \w+ not found for supplier \w+$/.test(error.message)
11+
);
12+
}
413

514
export const postMI = async (
615
incomingMi: IncomingMI,
@@ -14,5 +23,15 @@ export const getMI = async (
1423
supplierId: string,
1524
miRepo: MIRepository,
1625
): Promise<GetMIResponse> => {
17-
return mapToGetMIResponse(await miRepo.getMI(miId, supplierId));
26+
let mi;
27+
28+
try {
29+
mi = await miRepo.getMI(miId, supplierId);
30+
} catch (error) {
31+
if (isNotFoundError(error)) {
32+
throw new NotFoundError(ApiErrorDetail.NotFoundId);
33+
}
34+
throw error;
35+
}
36+
return mapToGetMIResponse(mi);
1837
};

0 commit comments

Comments
 (0)