Skip to content

Commit

Permalink
Gracefully handle errors from the datastore.put response (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
zimeg authored Oct 30, 2023
1 parent 258fdad commit 224f178
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
18 changes: 12 additions & 6 deletions functions/sample_function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,18 @@ export default SlackFunction(

// Save the sample object to the datastore
// https://api.slack.com/automation/datastores
await client.apps.datastore.put<typeof SampleObjectDatastore.definition>(
{
datastore: "SampleObjects",
item: sampleObject,
},
);
const putResponse = await client.apps.datastore.put<
typeof SampleObjectDatastore.definition
>({
datastore: "SampleObjects",
item: sampleObject,
});

if (!putResponse.ok) {
return {
error: `Failed to put item into the datastore: ${putResponse.error}`,
};
}

return { outputs: { updatedMsg } };
},
Expand Down
37 changes: 32 additions & 5 deletions functions/sample_function_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { SlackFunctionTester } from "deno-slack-sdk/mod.ts";
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
import {
assertEquals,
assertExists,
assertStringIncludes,
} from "https://deno.land/[email protected]/testing/asserts.ts";
import SampleFunction from "./sample_function.ts";
import * as mf from "https://deno.land/x/[email protected]/mod.ts";

Expand All @@ -8,9 +12,14 @@ const { createContext } = SlackFunctionTester("sample_function");
// Replaces globalThis.fetch with the mocked copy
mf.install();

mf.mock("POST@/api/apps.datastore.put", () => {
// Shared mocks can be defined at the top level of tests
mf.mock("POST@/api/apps.datastore.put", async (args) => {
const body = await args.formData();
const datastore = body.get("datastore");
const item = body.get("item");

return new Response(
`{"ok": true, "item": {"object_id": "d908f8bd-00c6-43f0-9fc3-4da3c2746e14"}}`,
`{"ok": true, "datastore": "${datastore}", "item": ${item}}`,
{
status: 200,
},
Expand All @@ -19,9 +28,27 @@ mf.mock("POST@/api/apps.datastore.put", () => {

Deno.test("Sample function test", async () => {
const inputs = { message: "Hello, World!", user: "U01234567" };
const { outputs } = await SampleFunction(createContext({ inputs }));
await assertEquals(
const { outputs, error } = await SampleFunction(createContext({ inputs }));

assertEquals(error, undefined);
assertEquals(
outputs?.updatedMsg,
":wave: <@U01234567> submitted the following message: \n\n>Hello, World!",
);
});

Deno.test("Sample function datastore error handling", async () => {
// Mocks specific to a test can be overriden within a test
mf.mock("POST@/api/apps.datastore.put", () => {
return new Response(`{"ok": false, "error": "datastore_error"}`, {
status: 200,
});
});

const inputs = { message: "Hello, World!", user: "U01234567" };
const { outputs, error } = await SampleFunction(createContext({ inputs }));

assertExists(error);
assertStringIncludes(error, "datastore_error");
assertEquals(outputs, undefined);
});

0 comments on commit 224f178

Please sign in to comment.