From a56e448b888e4081187f2c3eecef3655bde43f02 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Wed, 13 Sep 2023 10:58:55 +0100 Subject: [PATCH 1/3] Add error code and message --- src/constants/ErrorCode.ts | 6 ++++++ src/constants/ErrorMessages.ts | 5 +++++ src/contracts/IError.ts | 6 ++++++ src/contracts/IReturnResult.ts | 2 ++ src/index.ts | 18 ++++++++++++++++-- 5 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 src/constants/ErrorCode.ts create mode 100644 src/constants/ErrorMessages.ts create mode 100644 src/contracts/IError.ts diff --git a/src/constants/ErrorCode.ts b/src/constants/ErrorCode.ts new file mode 100644 index 0000000..bbf3fef --- /dev/null +++ b/src/constants/ErrorCode.ts @@ -0,0 +1,6 @@ +export enum ErrorCode { + Unknown, + FailedToFetchReddit, + UnableToParseJSON, + NoImageResultsFound, +} \ No newline at end of file diff --git a/src/constants/ErrorMessages.ts b/src/constants/ErrorMessages.ts new file mode 100644 index 0000000..4c5e24a --- /dev/null +++ b/src/constants/ErrorMessages.ts @@ -0,0 +1,5 @@ +export default class ErrorMessages { + public static readonly FailedToFetchReddit = "Failed to fetch result from Reddit"; + public static readonly UnableToParseJSON = "Unable to parse the JSON result"; + public static readonly NoImageResultsFound = "No image results found in response from Reddit"; +} \ No newline at end of file diff --git a/src/contracts/IError.ts b/src/contracts/IError.ts new file mode 100644 index 0000000..5565b93 --- /dev/null +++ b/src/contracts/IError.ts @@ -0,0 +1,6 @@ +import { ErrorCode } from "../constants/ErrorCode"; + +export default interface IError { + Code: ErrorCode; + Message: string; +} \ No newline at end of file diff --git a/src/contracts/IReturnResult.ts b/src/contracts/IReturnResult.ts index 8045c8e..780069d 100644 --- a/src/contracts/IReturnResult.ts +++ b/src/contracts/IReturnResult.ts @@ -1,6 +1,8 @@ +import IError from "./IError.js"; import IRedditResult from "./IRedditResult.js"; export default interface IReturnResult { IsSuccess: boolean; Result?: IRedditResult; + Error?: IError; } \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 400567e..344ab7c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,6 +3,8 @@ import IRedditResult from "./contracts/IRedditResult.js"; import fetch from "got-cjs"; import { List } from 'linqts'; import IFetchResult from "./contracts/IFetchResult.js"; +import { ErrorCode } from "./constants/ErrorCode.js"; +import ErrorMessages from "./constants/ErrorMessages.js"; const sortable = [ 'new', @@ -17,7 +19,11 @@ export default async function randomBunny(subreddit: string, sortBy?: string): P if (!result) { return { - IsSuccess: false + IsSuccess: false, + Error: { + Code: ErrorCode.FailedToFetchReddit, + Message: ErrorMessages.FailedToFetchReddit, + }, } } @@ -25,7 +31,11 @@ export default async function randomBunny(subreddit: string, sortBy?: string): P if (!json) { return { - IsSuccess: false + IsSuccess: false, + Error: { + Code: ErrorCode.UnableToParseJSON, + Message: ErrorMessages.FailedToFetchReddit, + }, } } @@ -40,6 +50,10 @@ export default async function randomBunny(subreddit: string, sortBy?: string): P if (dataWithImages.length == 0) { return { IsSuccess: false, + Error: { + Code: ErrorCode.NoImageResultsFound, + Message: ErrorMessages.NoImageResultsFound, + }, }; } else { random = Math.floor((Math.random() * (dataWithImages.length - 1)) + 0); // Between 0 and (size - 1) -- 2.45.2 From 8b4efab7ceef17042aee4d59f326a7cfb585d700 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Wed, 13 Sep 2023 11:09:38 +0100 Subject: [PATCH 2/3] Update tests --- src/index.ts | 12 ++++++------ tests/index.test.ts | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/index.ts b/src/index.ts index 344ab7c..4ff8e67 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,10 +1,10 @@ -import IReturnResult from "./contracts/IReturnResult.js"; -import IRedditResult from "./contracts/IRedditResult.js"; +import IReturnResult from "./contracts/IReturnResult"; +import IRedditResult from "./contracts/IRedditResult"; import fetch from "got-cjs"; import { List } from 'linqts'; -import IFetchResult from "./contracts/IFetchResult.js"; -import { ErrorCode } from "./constants/ErrorCode.js"; -import ErrorMessages from "./constants/ErrorMessages.js"; +import IFetchResult from "./contracts/IFetchResult"; +import { ErrorCode } from "./constants/ErrorCode"; +import ErrorMessages from "./constants/ErrorMessages"; const sortable = [ 'new', @@ -34,7 +34,7 @@ export default async function randomBunny(subreddit: string, sortBy?: string): P IsSuccess: false, Error: { Code: ErrorCode.UnableToParseJSON, - Message: ErrorMessages.FailedToFetchReddit, + Message: ErrorMessages.UnableToParseJSON, }, } } diff --git a/tests/index.test.ts b/tests/index.test.ts index 6eafc35..107e7ff 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -1,3 +1,5 @@ +import { ErrorCode } from "../src/constants/ErrorCode"; +import ErrorMessages from "../src/constants/ErrorMessages"; import randomBunny from "../src/index"; import fetch from "got-cjs"; @@ -32,6 +34,7 @@ describe('randomBunny', () => { expect(result.IsSuccess).toBeTruthy(); expect(result.Result).toBeDefined(); + expect(result.Error).toBeUndefined(); expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json'); }); @@ -63,6 +66,7 @@ describe('randomBunny', () => { expect(result.IsSuccess).toBeTruthy(); expect(result.Result).toBeDefined(); + expect(result.Error).toBeUndefined(); expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/hot.json'); }); @@ -94,6 +98,7 @@ describe('randomBunny', () => { expect(result.IsSuccess).toBeTruthy(); expect(result.Result).toBeDefined(); + expect(result.Error).toBeUndefined(); expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/hot.json'); }); @@ -105,6 +110,10 @@ describe('randomBunny', () => { expect(result.IsSuccess).toBeFalsy(); expect(result.Result).toBeUndefined(); + expect(result.Error).toBeDefined(); + + expect(result.Error!.Code).toBe(ErrorCode.FailedToFetchReddit); + expect(result.Error!.Message).toBe(ErrorMessages.FailedToFetchReddit); expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json'); }); @@ -118,6 +127,10 @@ describe('randomBunny', () => { expect(result.IsSuccess).toBeFalsy(); expect(result.Result).toBeUndefined(); + expect(result.Error).toBeDefined(); + + expect(result.Error!.Code).toBe(ErrorCode.UnableToParseJSON); + expect(result.Error!.Message).toBe(ErrorMessages.UnableToParseJSON); expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json'); }); @@ -135,6 +148,10 @@ describe('randomBunny', () => { expect(result.IsSuccess).toBeFalsy(); expect(result.Result).toBeUndefined(); + expect(result.Error).toBeDefined(); + + expect(result.Error!.Code).toBe(ErrorCode.NoImageResultsFound); + expect(result.Error!.Message).toBe(ErrorMessages.NoImageResultsFound); expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json'); }); @@ -166,6 +183,10 @@ describe('randomBunny', () => { expect(result.IsSuccess).toBeFalsy(); expect(result.Result).toBeUndefined(); + expect(result.Error).toBeDefined(); + + expect(result.Error!.Code).toBe(ErrorCode.NoImageResultsFound); + expect(result.Error!.Message).toBe(ErrorMessages.NoImageResultsFound); expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json'); }); -- 2.45.2 From 146b2a679c782e90226f9b0dbc5d774824edad7f Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Wed, 13 Sep 2023 11:09:57 +0100 Subject: [PATCH 3/3] Ignore .DS_Store --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 4915027..7025d5c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ node_modules/ dist/ coverage/ yarn-error.log +.DS_Store \ No newline at end of file -- 2.45.2