Skip to content

Commit

Permalink
Merge pull request #131 from Code-Hammers/CHE-169/subtask/Add-Express…
Browse files Browse the repository at this point in the history
…-Async-Errors-Dep

[CHE-169] Add Express Async Errors Dep
  • Loading branch information
brok3turtl3 authored Jun 8, 2024
2 parents 8c3b31b + 3fd0c53 commit 08628cd
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 1 deletion.
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"cookie-parser": "^1.4.6",
"dotenv": "^16.4.5",
"express": "^4.19.2",
"express-async-errors": "^3.1.1",
"express-async-handler": "^1.2.0",
"jsonwebtoken": "^9.0.2",
"mongoose": "^8.3.4",
Expand Down
2 changes: 1 addition & 1 deletion scripts/docker-remove-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fi

# Remove images
echo -e "${CYAN}Removing existing dev images from local environment:${NC}"
IMAGES=$(docker images codehammers/$IMAGE_SEARCH_NAME_DEV* -q)
IMAGES=$(docker images codehammers/*$IMAGE_SEARCH_NAME_DEV* -q)
REMOVEDIMAGES=0
if [ ! -z "$IMAGES" ]; then
docker rmi $IMAGES --force
Expand Down
15 changes: 15 additions & 0 deletions server/errors/badRequestError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { CustomError } from './customError';

export class BadRequestError extends CustomError {
statusCode = 400;

constructor(public message: string) {
super(`❌ Bad Request Error: ${message}`);

Object.setPrototypeOf(this, BadRequestError.prototype);
}

serializeErrors() {
return [{ message: this.message }];
}
}
22 changes: 22 additions & 0 deletions server/errors/customError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
- This is an abstract class (cannot be instantiated) for all of our error types to inherit from
- enforces that all of our error classes include:
1) statusCode & message (mostly used for console.logs) properties
2) serializeErrors method to ensure all errors are formatted the same
*/
export abstract class CustomError extends Error {
abstract statusCode: number;

constructor(message: string) {
super(message);

/*
This step is bc we are inheriting from a built in JS class
Helps correctly set the prototype chain of our custom error classes
Only really necessary if we are targeting es5 when transpiling typescript
*/
Object.setPrototypeOf(this, CustomError.prototype);
}

abstract serializeErrors(): { message: string; field?: string }[];
}
15 changes: 15 additions & 0 deletions server/errors/databaseConnectionError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { CustomError } from './customError';

export class DatabaseConnectionError extends CustomError {
statusCode = 500;

constructor() {
super('❌ Database Connection Error');

Object.setPrototypeOf(this, DatabaseConnectionError.prototype);
}

serializeErrors() {
return [{ message: 'Something went wrong, please try again later' }];
}
}
15 changes: 15 additions & 0 deletions server/errors/internalError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { CustomError } from './customError';

export class InternalError extends CustomError {
statusCode = 500;

constructor() {
super('❌ Internal Error');

Object.setPrototypeOf(this, InternalError.prototype);
}

serializeErrors() {
return [{ message: 'Something went wrong, please try again later' }];
}
}
15 changes: 15 additions & 0 deletions server/errors/notAuthorizedError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { CustomError } from './customError';

export class NotAuthorizedError extends CustomError {
statusCode = 401;

constructor() {
super('❌ Not Authorized');

Object.setPrototypeOf(this, NotAuthorizedError.prototype);
}

serializeErrors() {
return [{ message: 'Not Authorized' }];
}
}
15 changes: 15 additions & 0 deletions server/errors/notFoundError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { CustomError } from './customError';

export class NotFoundError extends CustomError {
statusCode = 404;

constructor() {
super('❌ Not Found Error');

Object.setPrototypeOf(this, NotFoundError.prototype);
}

serializeErrors() {
return [{ message: 'Not Found' }];
}
}
22 changes: 22 additions & 0 deletions server/errors/requestValidationError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { CustomError } from './customError';
import { ValidationError } from './validationError';

export class RequestValidationError extends CustomError {
statusCode = 400;

/*
when instantiating, accepts an array of ValidationError's as an argument
in case 1 or more validations fail
*/
constructor(public errors: ValidationError[]) {
super('❌ Request Validation Failed');

Object.setPrototypeOf(this, RequestValidationError.prototype);
}

serializeErrors() {
return this.errors.map(({ message, field }) => {
return { message, field };
});
}
}
11 changes: 11 additions & 0 deletions server/errors/validationError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export class ValidationError extends Error {
field: string;
// use field argument for info on what failed validation, eg. "email" or "password"
constructor(message: string, field: string) {
super(message);

this.field = field;

Object.setPrototypeOf(this, ValidationError.prototype);
}
}
1 change: 1 addition & 0 deletions server/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'path';
import express, { Request, Response, Application } from 'express';
import 'express-async-errors';
import userRoutes from './routes/userRoutes';
import profileRoutes from './routes/profileRoutes';
import authRoutes from './routes/authRoutes';
Expand Down

0 comments on commit 08628cd

Please sign in to comment.