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)