Skip to content

Commit

Permalink
Remove status param from GroutError constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmaLRussell committed Dec 18, 2024
1 parent 507c75d commit 9224ee0
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 12 deletions.
1 change: 0 additions & 1 deletion src/controllers/tileController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const parseIntParam = (param: string): number => {
if (!/^\d+$/.test(param)) {
throw new GroutError(
`"${param}" is not an integer`,
400,
ErrorType.BAD_REQUEST
);
}
Expand Down
6 changes: 6 additions & 0 deletions src/errors/errorType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ export const enum ErrorType {
NOT_FOUND = "NOT_FOUND",
UNEXPECTED_ERROR = "UNEXPECTED_ERROR"
}

export const ErrorTypeStatuses: { [key in ErrorType]: number } = {
[ErrorType.BAD_REQUEST]: 400,
[ErrorType.NOT_FOUND]: 404,
[ErrorType.UNEXPECTED_ERROR]: 500
};
6 changes: 3 additions & 3 deletions src/errors/groutError.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { ErrorType } from "./errorType";
import { ErrorType, ErrorTypeStatuses } from "./errorType";

export class GroutError extends Error {
status: number;
errorType: ErrorType;

constructor(message: string, status: number, errorType: ErrorType) {
constructor(message: string, errorType: ErrorType) {
super(message);

this.name = "GroutError";
this.status = status;
this.status = ErrorTypeStatuses[errorType];
this.errorType = errorType;
}
}
2 changes: 1 addition & 1 deletion src/errors/notFound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import { GroutError } from "./groutError";

export default (req: Request) => {
const { url } = req;
throw new GroutError(`Route not found: ${url}`, 404, ErrorType.NOT_FOUND);
throw new GroutError(`Route not found: ${url}`, ErrorType.NOT_FOUND);
};
7 changes: 1 addition & 6 deletions tests/unit/controllers/tileController.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,7 @@ describe("TileController", () => {
await TileController.getTile(request, mockResponse, mockNext);
// expect the async controller handler to have been used, so the error will be caught and passed to next
expect(mockNext).toHaveBeenCalledWith(
new GroutError(
"Route not found: /mockUrl",
404,
ErrorType.NOT_FOUND
)
new GroutError("Route not found: /mockUrl", ErrorType.NOT_FOUND)
);
};

Expand All @@ -74,7 +70,6 @@ describe("TileController", () => {
expect(mockNext).toHaveBeenCalledWith(
new GroutError(
`"${badParam}" is not an integer`,
400,
ErrorType.BAD_REQUEST
)
);
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/errors/groutError.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { describe, expect, test, vi, beforeEach } from "vitest";
import { GroutError } from "../../../src/errors/groutError";
import { ErrorType } from "../../../src/errors/errorType";

describe("GroutError", () => {
test("is constructed with expected status value", () => {
expect(new GroutError("test", ErrorType.BAD_REQUEST).status).toBe(400);
expect(new GroutError("test", ErrorType.NOT_FOUND).status).toBe(404);
expect(new GroutError("test", ErrorType.UNEXPECTED_ERROR).status).toBe(
500
);
});
});
2 changes: 1 addition & 1 deletion tests/unit/errors/handleError.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe("handleError", () => {
});

test("handles GroutError", () => {
const err = new GroutError("test message", 400, ErrorType.BAD_REQUEST);
const err = new GroutError("test message", ErrorType.BAD_REQUEST);
const mockReq = {} as any;
const mockRes = {} as any;

Expand Down

0 comments on commit 9224ee0

Please sign in to comment.