Skip to content

Commit 07440f4

Browse files
committed
refactor 404 error handling for getMi as per letter operations
1 parent d60be84 commit 07440f4

5 files changed

Lines changed: 41 additions & 10 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Error thrown when management information is not found in the database.
3+
*/
4+
export default class MiNotFoundError extends Error {
5+
constructor(
6+
public readonly supplierId: string,
7+
public readonly miId: string,
8+
) {
9+
super(`Management information not found: supplierId=${supplierId}, miId=${miId}`);
10+
this.name = "MiNotFoundError";
11+
}
12+
}

internal/datastore/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ export { default as LetterQueueRepository } from "./letter-queue-repository";
77
export { default as DBHealthcheck } from "./healthcheck";
88
export { default as LetterAlreadyExistsError } from "./errors/letter-already-exists-error";
99
export { default as LetterNotFoundError } from "./errors/letter-not-found-error";
10+
export { default as MiNotFoundError } from "./errors/mi-not-found-error";

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { IncomingMI } from "../../contracts/mi";
22
import { getMI, postMI } from "../mi-operations";
3+
import MiNotFoundError from "@internal/datastore/src/errors/mi-not-found-error";
4+
35

46
describe("postMI function", () => {
57
const incomingMi: IncomingMI = {
@@ -61,4 +63,26 @@ describe("postMI function", () => {
6163
},
6264
});
6365
});
66+
67+
it("should throw notFoundError when letter does not exist", async () => {
68+
const mockRepo = {
69+
getMI: jest
70+
.fn()
71+
.mockRejectedValue(new MiNotFoundError("miId1", "supplier1")),
72+
};
73+
74+
await expect(
75+
getMI("miId1", "supplier1", mockRepo as any),
76+
).rejects.toThrow("No resource found with that ID");
77+
});
78+
79+
it("should throw unexpected error", async () => {
80+
const mockRepo = {
81+
getMI: jest.fn().mockRejectedValue(new Error("unexpected error")),
82+
};
83+
84+
await expect(
85+
getMI("miId1", "supplier1", mockRepo as any),
86+
).rejects.toThrow("unexpected error");
87+
});
6488
});

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export const getLetterById = async (
7474
letter = await letterRepo.getLetterById(supplierId, letterId);
7575
} catch (error) {
7676
if (error instanceof LetterNotFoundError) {
77-
throw new NotFoundError(ApiErrorDetail.NotFoundLetterId);
77+
throw new NotFoundError(ApiErrorDetail.NotFoundId);
7878
}
7979
throw error;
8080
}
@@ -98,7 +98,7 @@ export const getLetterDataUrl = async (
9898
);
9999
} catch (error) {
100100
if (error instanceof LetterNotFoundError) {
101-
throw new NotFoundError(ApiErrorDetail.NotFoundLetterId);
101+
throw new NotFoundError(ApiErrorDetail.NotFoundId);
102102
}
103103
throw error;
104104
}

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,7 @@ import { GetMIResponse, IncomingMI, PostMIResponse } from "../contracts/mi";
33
import { mapToGetMIResponse, mapToPostMIResponse } from "../mappers/mi-mapper";
44
import { ApiErrorDetail } from "../contracts/errors";
55
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-
}
6+
import MiNotFoundError from "@internal/datastore/src/errors/letter-not-found-error";
137

148
export const postMI = async (
159
incomingMi: IncomingMI,
@@ -28,7 +22,7 @@ export const getMI = async (
2822
try {
2923
mi = await miRepo.getMI(miId, supplierId);
3024
} catch (error) {
31-
if (isNotFoundError(error)) {
25+
if (error instanceof MiNotFoundError) {
3226
throw new NotFoundError(ApiErrorDetail.NotFoundId);
3327
}
3428
throw error;

0 commit comments

Comments
 (0)