Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#IP-368] table service unhandled exception #197

Merged
merged 5 commits into from
Aug 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions utils/__tests__/storage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { ServiceResponse, TableService } from "azure-storage";
import { left, right } from "fp-ts/lib/Either";
import { none, some } from "fp-ts/lib/Option";
import { deleteTableEntity, insertTableEntity } from "../storage";

const mockInsertEntity = jest.fn();
const mockDeleteEntity = jest.fn();
const mockTableService = ({
deleteEntity: mockDeleteEntity,
insertEntity: mockInsertEntity
} as unknown) as TableService;

const genericError = new Error("Generic Error");
const aTableName = "table";
const anEntityDescriptor = {
prop: "value"
};
const anErrorResponse: ServiceResponse = {
isSuccessful: false,
md5: "md5",
statusCode: 404
};
const aSuccessResponse: ServiceResponse = {
body: JSON.stringify(anEntityDescriptor),
isSuccessful: true,
md5: "md5",
statusCode: 200
};

describe("insertTableEntity", () => {
it.each`
title | error | result | response | e1 | e2
${"returns an error if insertEntity fail"} | ${genericError} | ${null} | ${null} | ${left(genericError)} | ${null}
${"returns an error if insertEntity fail with a response"} | ${genericError} | ${null} | ${anErrorResponse} | ${left(genericError)} | ${anErrorResponse}
${"returns an error if insertEntity fail with no error"} | ${null} | ${null} | ${anErrorResponse} | ${left(expect.any(Error))} | ${anErrorResponse}
${"returns the response value if insertEntity succeded"} | ${null} | ${anEntityDescriptor} | ${aSuccessResponse} | ${right(anEntityDescriptor)} | ${aSuccessResponse}
`("should $title", async ({ error, result, response, e1, e2 }) => {
mockInsertEntity.mockImplementationOnce((_, __, callback) =>
callback(error, result, response)
);
const insertResponse = await insertTableEntity(
mockTableService,
aTableName
)(anEntityDescriptor);
expect(mockInsertEntity).toBeCalledWith(
aTableName,
anEntityDescriptor,
expect.any(Function)
);
expect(insertResponse.e1).toEqual(e1);
expect(insertResponse.e2).toEqual(e2);
});
});

describe("deleteTableEntity", () => {
it.each`
title | error | response | e1 | e2
${"returns an error if deleteEntity fail"} | ${genericError} | ${null} | ${some(genericError)} | ${null}
${"returns an error if deleteEntity fail with a response"} | ${genericError} | ${anErrorResponse} | ${some(genericError)} | ${anErrorResponse}
${"returns an error if deleteEntity fail with no error"} | ${null} | ${anErrorResponse} | ${some(expect.any(Error))} | ${anErrorResponse}
${"returns the response value if deleteEntity succeded"} | ${null} | ${aSuccessResponse} | ${none} | ${aSuccessResponse}
`("should $title", async ({ error, response, e1, e2 }) => {
mockDeleteEntity.mockImplementationOnce((_, __, callback) =>
callback(error, response)
);
const deleteResponse = await deleteTableEntity(
mockTableService,
aTableName
)(anEntityDescriptor);
expect(mockDeleteEntity).toBeCalledWith(
aTableName,
anEntityDescriptor,
expect.any(Function)
);
expect(deleteResponse.e1).toEqual(e1);
expect(deleteResponse.e2).toEqual(e2);
});
});
35 changes: 24 additions & 11 deletions utils/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,29 @@ export const insertTableEntity = (
) => <T>(
entityDescriptor: T
): Promise<
ITuple2<Either<Error, T | TableService.EntityMetadata>, ServiceResponse>
ITuple2<
Either<Error, T | TableService.EntityMetadata>,
ServiceResponse | null
>
> => {
return new Promise(resolve =>
tableService.insertEntity(
table,
entityDescriptor,
(
error: Error,
error: Error | null,
result: T | TableService.EntityMetadata,
response: ServiceResponse
response: ServiceResponse | null
) =>
resolve(
response.isSuccessful
? Tuple2(right(result), response)
: Tuple2(left(error), response)
// We need to check error first because response could be null
// @ref https://github.com/Azure/azure-storage-node/blob/v2.10.2/lib/common/services/storageserviceclient.js#L250
error || !response.isSuccessful
? Tuple2(
left(error || new Error("Unsuccessful response from storage")),
response
)
: Tuple2(right(result), response)
)
)
);
Expand All @@ -42,16 +50,21 @@ export const deleteTableEntity = (
table: string
) => <T>(
entityDescriptor: T
): Promise<ITuple2<Option<Error>, ServiceResponse>> => {
): Promise<ITuple2<Option<Error>, ServiceResponse | null>> => {
return new Promise(resolve =>
tableService.deleteEntity(
table,
entityDescriptor,
(error: Error, response: ServiceResponse) =>
(error: Error | null, response: ServiceResponse | null) =>
resolve(
response.isSuccessful
? Tuple2(none, response)
: Tuple2(some(error), response)
// We need to check error first because response could be null
// @ref https://github.com/Azure/azure-storage-node/blob/v2.10.2/lib/common/services/storageserviceclient.js#L250
error || !response.isSuccessful
? Tuple2(
some(error || new Error("Unsuccessful response from storage")),
response
)
: Tuple2(none, response)
)
)
);
Expand Down
7 changes: 5 additions & 2 deletions utils/subscription_feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ export const updateSubscriptionStatus = (

if (
deleteResults.some(
_ => _.maybeError.isSome() && _.uResponse.statusCode !== 404
_ =>
// _.uResponse could be null
_.maybeError.isSome() && _.uResponse && _.uResponse.statusCode !== 404
)
) {
// retry
Expand All @@ -115,7 +117,8 @@ export const updateSubscriptionStatus = (
RowKey: eg.String(insertEntity.rowKey),
version: eg.Int32(version)
});
if (resultOrError.isLeft() && sResponse.statusCode !== 409) {
// sResponse could be null
if (resultOrError.isLeft() && sResponse && sResponse.statusCode !== 409) {
// retry
context.log.error(`${logPrefix}|ERROR=${resultOrError.value.message}`);
throw resultOrError.value;
Expand Down