Add error codes and error messages to failed results (#89)
All checks were successful
continuous-integration/drone/push Build is passing

# Description

- Added an Error Code (enum) and Error Message (string) to all the failed results

#53

## Type of change

Please delete options that are not relevant.

- [x] New feature (non-breaking change which adds functionality)

# How Has This Been Tested?

- This has been tested both manually as well as the unit tests

# Checklist

- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that provde my fix is effective or that my feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged and published in downstream modules

Reviewed-on: https://gitea.vylpes.xyz/RabbitLabs/random-bunny/pulls/89
Co-authored-by: Ethan Lane <ethan@vylpes.com>
Co-committed-by: Ethan Lane <ethan@vylpes.com>
This commit is contained in:
Ethan Lane 2023-09-15 15:18:57 +01:00 committed by Vylpes
parent 07c37e2c06
commit 037608e059
7 changed files with 60 additions and 5 deletions

1
.gitignore vendored
View file

@ -2,3 +2,4 @@ node_modules/
dist/
coverage/
yarn-error.log
.DS_Store

View file

@ -0,0 +1,6 @@
export enum ErrorCode {
Unknown,
FailedToFetchReddit,
UnableToParseJSON,
NoImageResultsFound,
}

View file

@ -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";
}

6
src/contracts/IError.ts Normal file
View file

@ -0,0 +1,6 @@
import { ErrorCode } from "../constants/ErrorCode";
export default interface IError {
Code: ErrorCode;
Message: string;
}

View file

@ -1,6 +1,8 @@
import IError from "./IError.js";
import IRedditResult from "./IRedditResult.js";
export default interface IReturnResult {
IsSuccess: boolean;
Result?: IRedditResult;
Error?: IError;
}

View file

@ -1,8 +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 IFetchResult from "./contracts/IFetchResult";
import { ErrorCode } from "./constants/ErrorCode";
import ErrorMessages from "./constants/ErrorMessages";
const sortable = [
'new',
@ -17,7 +19,11 @@ export default async function randomBunny(subreddit: string, sortBy: string = 'h
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 = 'h
if (!json) {
return {
IsSuccess: false
IsSuccess: false,
Error: {
Code: ErrorCode.UnableToParseJSON,
Message: ErrorMessages.UnableToParseJSON,
},
}
}
@ -40,6 +50,10 @@ export default async function randomBunny(subreddit: string, sortBy: string = 'h
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)

View file

@ -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');
});