-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #131 from Code-Hammers/CHE-169/subtask/Add-Express…
…-Async-Errors-Dep [CHE-169] Add Express Async Errors Dep
- Loading branch information
Showing
12 changed files
with
149 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters