Skip to content

Commit

Permalink
feat: typed error management
Browse files Browse the repository at this point in the history
  • Loading branch information
StanHannebelle committed Jan 21, 2022
1 parent 522799f commit 67054e7
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/assertions/toExistAsS3Bucket/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AWSClient } from "../../helpers/general";
import { testResult } from "../../utils/testResult";
import { is404Error } from "../utils";

export default {
async toExistAsS3Bucket(bucketName: any) {
Expand All @@ -15,7 +16,7 @@ export default {

return testResult(message, true);
} catch (error) {
if (error.statusCode === 404) {
if (is404Error(error)) {
message = `expected S3 bucket to exist with name ${bucketName} - not found`;

return testResult(message, false);
Expand Down
5 changes: 3 additions & 2 deletions src/assertions/toHaveContentEqualTo/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AWSClient } from "../../helpers/general";
import { testResult } from "../../utils/testResult";
import { isNoSuchBucketError, isNoSuchKeyError } from "../utils";

export default {
async toHaveContentEqualTo({ bucketName, objectName }: any, content: any) {
Expand All @@ -21,12 +22,12 @@ export default {

return testResult(message, false);
} catch (error) {
if (error.code === "NoSuchKey") {
if (isNoSuchKeyError(error)) {
message = `expected ${bucketName} to have object with name ${objectName} - not found`;

return testResult(message, false);
}
if (error.code === "NoSuchBucket") {
if (isNoSuchBucketError(error)) {
message = `expected ${bucketName} to exist - not found`;

return testResult(message, false);
Expand Down
5 changes: 3 additions & 2 deletions src/assertions/toHaveContentTypeEqualTo/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AWSClient } from "../../helpers/general";
import { testResult } from "../../utils/testResult";
import { isNoSuchBucketError, isNoSuchKeyError } from "../utils";

export default {
async toHaveContentTypeEqualTo(
Expand All @@ -24,12 +25,12 @@ export default {

return testResult(message, false);
} catch (error) {
if (error.code === "NoSuchKey") {
if (isNoSuchKeyError(error)) {
message = `expected ${bucketName} to have object with name ${objectName} - not found`;

return testResult(message, false);
}
if (error.code === "NoSuchBucket") {
if (isNoSuchBucketError(error)) {
message = `expected ${bucketName} to exist - not found`;

return testResult(message, false);
Expand Down
3 changes: 2 additions & 1 deletion src/assertions/toHaveObjectWithNameEqualTo/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AWSClient } from "../../helpers/general";
import { testResult } from "../../utils/testResult";
import { isNoSuchKeyError } from "../utils";

export default {
async toHaveS3ObjectWithNameEqualTo(bucketName: any, objectName: any) {
Expand All @@ -16,7 +17,7 @@ export default {

return testResult(message, true);
} catch (error) {
if (error.code === "NoSuchKey") {
if (isNoSuchKeyError(error)) {
message = `expected ${bucketName} to have object with name ${objectName} - not found`;

return testResult(message, false);
Expand Down
11 changes: 11 additions & 0 deletions src/assertions/utils/errorTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type Error404 = { statusCode: 404 };
export const is404Error = (error: any): error is Error404 =>
error.statusCode === 404;

type ErrorNoSuchKey = { code: "NoSuchKey" };
export const isNoSuchKeyError = (error: any): error is ErrorNoSuchKey =>
error.code === "NoSuchKey";

type ErrorNoSuchBucket = { code: "NoSuchBucket" };
export const isNoSuchBucketError = (error: any): error is ErrorNoSuchBucket =>
error.code === "NoSuchBucket";
1 change: 1 addition & 0 deletions src/assertions/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./errorTypes";

0 comments on commit 67054e7

Please sign in to comment.