Skip to content

Commit

Permalink
Merge pull request #2 from hayd/error-visibility
Browse files Browse the repository at this point in the history
Throw a readable error when baseFetch fails
  • Loading branch information
chiefbiiko committed Dec 5, 2019
2 parents f6d0a95 + f0a6da1 commit d801894
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
21 changes: 8 additions & 13 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,26 +134,21 @@ function createCache(conf: Doc): Doc {
}

/** Base fetch. */
function baseFetch(conf: Doc, op: string, params: Doc): Promise<Doc> {
async function baseFetch(conf: Doc, op: string, params: Doc): Promise<Doc> {
const payload: Uint8Array = encode(JSON.stringify(params), "utf8");

const headers: Headers = createHeaders(op, payload, conf as HeadersConfig);

return fetch(conf.endpoint, {
const response: Response = await fetch(conf.endpoint, {
method: conf.method,
headers,
body: payload
}).then(
(response: Response): Doc => {
if (!response.ok) {
throw new Error(
`http query request failed: ${response.status} ${response.statusText}`
);
}

return response.json();
}
);
});
const body = await response.json();
if (!response.ok) {
throw new Error(body.message);
}
return body;
}

/** Base op. */
Expand Down
31 changes: 30 additions & 1 deletion test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { test, runIfMain } from "https://deno.land/std/testing/mod.ts";
import { assert, assertEquals } from "https://deno.land/std/testing/asserts.ts";
import {
assert,
assertEquals,
assertThrowsAsync
} from "https://deno.land/std/testing/asserts.ts";
import { encode } from "https://denopkg.com/chiefbiiko/std-encoding/mod.ts";
import { awsSignatureV4, kdf } from "./client/mod.ts";
import { ClientConfig, DynamoDBClient, createClient } from "./mod.ts";
Expand Down Expand Up @@ -403,4 +407,29 @@ test({
}
});

test({
name: "missing table throws a readable error",
async fn(): Promise<void> {
const ddbc: DynamoDBClient = createClient(CONF);

let result: Doc = await ddbc.listTables();

if (result.TableNames.includes("nonexistent_table")) {
await ddbc.deleteTable({
TableName: "nonexistent_table"
});
}

async function fn(): Promise<void> {
await ddbc.scan({ TableName: "nonexistent_table" });
}

assertThrowsAsync(
fn,
Error,
"Cannot do operations on a non-existent table"
);
}
});

runIfMain(import.meta);

0 comments on commit d801894

Please sign in to comment.