Skip to content

Commit

Permalink
[#IOPID-1264] Handle new InternalServerError response from GetProfile (
Browse files Browse the repository at this point in the history
…#1090)

* [#IOPID-1264] Handle new InternalServerError response from GetProfile

* Disable log in tests

* Restore original yarn.lock, upgrade only SDK
  • Loading branch information
BurnedMarshal authored Dec 20, 2023
1 parent d7314b0 commit 0292113
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 24 deletions.
73 changes: 53 additions & 20 deletions src/services/__tests__/profileService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ const notFoundApiResponse = {
};
const APIError = {
status: 500,
value: {
detail: "a detail error",
},
};

const tooManyReqApiMessagesResponse = {
Expand All @@ -121,8 +124,6 @@ const conflictApiMessagesResponse = {
status: 409,
};

const expectedApiError = new Error("Api error.");

const mockGetProfile = jest.fn();
const mockUpdateProfile = jest.fn();
const mockCreateProfile = jest.fn();
Expand Down Expand Up @@ -257,19 +258,35 @@ describe("ProfileService#getProfile", () => {
});
});

it("returns an error if the API returns an error", async () => {
it("returns an error if the API returns an Internal Server Error", async () => {
mockGetProfile.mockImplementation(() => t.success(APIError));

const service = new ProfileService(api);

try {
await service.getProfile(mockedUser);
} catch (e) {
expect(mockGetProfile).toHaveBeenCalledWith({
fiscalCode: mockedUser.fiscal_code,
});
expect(e).toEqual(expectedApiError);
}
const response = await service.getProfile(mockedUser);
expect(mockGetProfile).toHaveBeenCalledWith({
fiscal_code: mockedUser.fiscal_code,
});
expect(response).toMatchObject({
detail: expect.stringContaining("Error retrieving the profile"),
kind: "IResponseErrorInternal",
});
});

it("returns an error if the API client throw an exception", async () => {
const exceptionError = new Error("HTTP Client Error");
mockGetProfile.mockRejectedValueOnce(exceptionError);

const service = new ProfileService(api);

const response = await service.getProfile(mockedUser);
expect(mockGetProfile).toHaveBeenCalledWith({
fiscal_code: mockedUser.fiscal_code,
});
expect(response).toMatchObject({
detail: expect.stringContaining(exceptionError.message),
kind: "IResponseErrorInternal",
});
});
});

Expand Down Expand Up @@ -386,19 +403,35 @@ describe("ProfileService#getApiProfile", () => {
});
});

it("returns an error if the API returns an error", async () => {
it("returns an error if the API returns an Internal Server Error", async () => {
mockGetProfile.mockImplementation(() => t.success(APIError));

const service = new ProfileService(api);

try {
await service.getApiProfile(mockedUser);
} catch (e) {
expect(mockGetProfile).toHaveBeenCalledWith({
fiscalCode: mockedUser.fiscal_code,
});
expect(e).toEqual(expectedApiError);
}
const response = await service.getApiProfile(mockedUser);
expect(mockGetProfile).toHaveBeenCalledWith({
fiscal_code: mockedUser.fiscal_code,
});
expect(response).toMatchObject({
detail: expect.stringContaining("Error retrieving the profile"),
kind: "IResponseErrorInternal",
});
});

it("returns an error if the API client throw an exception", async () => {
const exceptionError = new Error("HTTP Client Error");
mockGetProfile.mockRejectedValueOnce(exceptionError);

const service = new ProfileService(api);

const response = await service.getApiProfile(mockedUser);
expect(mockGetProfile).toHaveBeenCalledWith({
fiscal_code: mockedUser.fiscal_code,
});
expect(response).toMatchObject({
detail: expect.stringContaining(exceptionError.message),
kind: "IResponseErrorInternal",
});
});
});

Expand Down
12 changes: 12 additions & 0 deletions src/services/profileService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ export default class ProfileService {
return ResponseErrorTooManyRequests();
}

if (response.status === 500) {
return ResponseErrorInternal(
`Error retrieving the profile [${response.value.detail}]`
);
}

return unhandledResponseStatus(response.status);
});
});
Expand Down Expand Up @@ -123,6 +129,12 @@ export default class ProfileService {
return ResponseErrorTooManyRequests();
}

if (response.status === 500) {
return ResponseErrorInternal(
`Error retrieving the profile [${response.value.detail}]`
);
}

return unhandledResponseStatus(response.status);
});
});
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -984,9 +984,9 @@
prettier "^2.6.2"

"@pagopa/io-functions-app-sdk@x":
version "5.8.1"
resolved "https://registry.yarnpkg.com/@pagopa/io-functions-app-sdk/-/io-functions-app-sdk-5.8.1.tgz#9ac59eef1e1e83ccd555001aac63114b50f0daf2"
integrity sha512-seoJh0xUymbbYBKFQUlCVKXVVKlRwH+/eF+yhHlTxuyoBHmY5Mw03AohPzx32L6c8OvrIIXnz8m++iW6sIQT/w==
version "5.9.0"
resolved "https://registry.yarnpkg.com/@pagopa/io-functions-app-sdk/-/io-functions-app-sdk-5.9.0.tgz#069dcd46502954eab98cab3bbf013471e27e414a"
integrity sha512-GhgGBpppIrexXs+biYO35mkxKJrO4tu0gjwAMd+0X7/MKKLrOtVy2fb7n0LVyJAHMWfaFr8DGrM3d2arj3v0pw==
dependencies:
"@pagopa/ts-commons" "^10.0.0"
fp-ts "^2.10.5"
Expand Down Expand Up @@ -7959,4 +7959,4 @@ z-schema@^5.0.1:
lodash.isequal "^4.5.0"
validator "^13.7.0"
optionalDependencies:
commander "^9.4.1"
commander "^9.4.1"

0 comments on commit 0292113

Please sign in to comment.