-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
197 additions
and
63 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { CustomError } from "./CustomError"; | ||
|
||
export default class BadRequestError extends CustomError { | ||
private static readonly _statusCode = 400; | ||
private readonly _code: number; | ||
private readonly _logging: boolean; | ||
private readonly _context: { [key: string]: any }; | ||
|
||
constructor(params?: {code?: number, message?: string, logging?: boolean, context?: { [key: string]: any }}) { | ||
const { code, message, logging } = params || {}; | ||
|
||
super(message || "Bad request"); | ||
this._code = code || BadRequestError._statusCode; | ||
this._logging = logging || false; | ||
this._context = params?.context || {}; | ||
|
||
// Only because we are extending a built in class | ||
Object.setPrototypeOf(this, BadRequestError.prototype); | ||
} | ||
|
||
get errors() { | ||
return [{ message: this.message, context: this._context }]; | ||
} | ||
|
||
get statusCode() { | ||
return this._code; | ||
} | ||
|
||
get logging() { | ||
return this._logging; | ||
} | ||
} |
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,17 @@ | ||
export type CustomErrorContent = { | ||
message: string, | ||
context?: { [key: string]: any } | ||
}; | ||
|
||
export abstract class CustomError extends Error { | ||
abstract readonly statusCode: number; | ||
abstract readonly errors: CustomErrorContent[]; | ||
abstract readonly logging: boolean; | ||
|
||
constructor(message: string) { | ||
super(message); | ||
|
||
// Only because we are extending a built in class | ||
Object.setPrototypeOf(this, CustomError.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { CustomError } from "./CustomError"; | ||
|
||
export default class DocumentDriveError extends CustomError { | ||
private static readonly _statusCode = 400; | ||
private readonly _code: number; | ||
private readonly _logging: boolean; | ||
private readonly _context: { [key: string]: any }; | ||
|
||
constructor(params?: { code?: number, message?: string, logging?: boolean, context?: { [key: string]: any } }) { | ||
const { code, message, logging } = params || {}; | ||
|
||
super(message || "Bad request"); | ||
this._code = code || DocumentDriveError._statusCode; | ||
this._logging = logging || false; | ||
this._context = params?.context || {}; | ||
|
||
// Only because we are extending a built in class | ||
Object.setPrototypeOf(this, DocumentDriveError.prototype); | ||
} | ||
|
||
get errors() { | ||
return [{ message: this.message, context: this._context }]; | ||
} | ||
|
||
get statusCode() { | ||
return this._code; | ||
} | ||
|
||
get logging() { | ||
return this._logging; | ||
} | ||
} |
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 { NextFunction, Request, Response } from "express"; | ||
import { getChildLogger } from "../logger"; | ||
import { CustomError } from "../errors/CustomError"; | ||
|
||
const logger = getChildLogger({ msgPrefix: 'Generic Error Handler', }); | ||
|
||
export const errorHandler = (err: Error, req: Request, res: Response, next: NextFunction) => { | ||
err.cause = err.cause || 'Unknown'; | ||
logger.error({ | ||
msg: err.message, | ||
}); | ||
|
||
console.log("TEST TEST TEST") | ||
res.status(500).send({ errors: err.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
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