- first commit
This commit is contained in:
commit
407717d4b5
57 changed files with 5510 additions and 0 deletions
11
src/exceptions/BadRequest.ts
Normal file
11
src/exceptions/BadRequest.ts
Normal file
|
@ -0,0 +1,11 @@
|
|||
import { StatusCodes } from "http-status-codes";
|
||||
import HTTPException from "./HttpException";
|
||||
|
||||
class BadRequestException extends HTTPException {
|
||||
constructor(message?: string) {
|
||||
super(StatusCodes.BAD_REQUEST, message || "bad Request");
|
||||
this.stack = "";
|
||||
}
|
||||
}
|
||||
|
||||
export default BadRequestException;
|
12
src/exceptions/HttpException.ts
Normal file
12
src/exceptions/HttpException.ts
Normal file
|
@ -0,0 +1,12 @@
|
|||
class HTTPException extends Error {
|
||||
public status: number;
|
||||
public message: string;
|
||||
|
||||
constructor(status: number, message: string) {
|
||||
super(message);
|
||||
this.status = status;
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
|
||||
export default HTTPException;
|
11
src/exceptions/InternalServerError.ts
Normal file
11
src/exceptions/InternalServerError.ts
Normal file
|
@ -0,0 +1,11 @@
|
|||
import { StatusCodes } from "http-status-codes";
|
||||
import HTTPException from "./HttpException";
|
||||
|
||||
class InternalServerErrorException extends HTTPException {
|
||||
constructor(message?: string) {
|
||||
super(StatusCodes.INTERNAL_SERVER_ERROR, message || "Server Error");
|
||||
this.stack = "";
|
||||
}
|
||||
}
|
||||
|
||||
export default InternalServerErrorException;
|
11
src/exceptions/OaiErrorCodes.ts
Normal file
11
src/exceptions/OaiErrorCodes.ts
Normal file
|
@ -0,0 +1,11 @@
|
|||
export enum OaiErrorCodes {
|
||||
BADVERB = 1010,
|
||||
BADARGUMENT = 1011,
|
||||
CANNOTDISSEMINATEFORMAT = 1012,
|
||||
BADRESUMPTIONTOKEN = 1013,
|
||||
NORECORDSMATCH = 1014,
|
||||
IDDOESNOTEXIST = 1015,
|
||||
}
|
||||
|
||||
// 👇️ default export
|
||||
// export { OaiErrorCodes };
|
77
src/exceptions/OaiModelException.ts
Normal file
77
src/exceptions/OaiModelException.ts
Normal file
|
@ -0,0 +1,77 @@
|
|||
import { StatusCodes } from "http-status-codes";
|
||||
// import HTTPException from './HttpException';
|
||||
import { OaiErrorCodes } from "./OaiErrorCodes";
|
||||
|
||||
export class ErrorCode {
|
||||
public static readonly Unauthenticated = "Unauthenticated";
|
||||
public static readonly NotFound = "NotFound";
|
||||
public static readonly MaximumAllowedGrade = "MaximumAllowedGrade";
|
||||
public static readonly AsyncError = "AsyncError";
|
||||
public static readonly UnknownError = "UnknownError";
|
||||
}
|
||||
|
||||
export class ErrorModel {
|
||||
/**
|
||||
* Unique error code which identifies the error.
|
||||
*/
|
||||
public code: string;
|
||||
/**
|
||||
* Status code of the error.
|
||||
*/
|
||||
public status: number;
|
||||
/**
|
||||
* Any additional data that is required for translation.
|
||||
*/
|
||||
// public metaData?: any;
|
||||
}
|
||||
|
||||
export class OaiModelException extends Error {
|
||||
public status: number;
|
||||
public message: string;
|
||||
public oaiCode: number;
|
||||
|
||||
// constructor(status: number, message: string) {
|
||||
// super(message);
|
||||
// this.status = status;
|
||||
// this.message = message;
|
||||
// }
|
||||
constructor(status: number, message: string, oaiCode: number) {
|
||||
super(message);
|
||||
this.status = status;
|
||||
this.message = message;
|
||||
this.oaiCode = oaiCode;
|
||||
}
|
||||
// constructor(code: string = ErrorCode.UnknownError, message: any = null) {
|
||||
// super(code);
|
||||
// Object.setPrototypeOf(this, new.target.prototype);
|
||||
// this.name = code;
|
||||
// this.status = 500;
|
||||
// this.message = message;
|
||||
// // switch (code) {
|
||||
// // case ErrorCode.Unauthenticated:
|
||||
// // this.status = 401;
|
||||
// // break;
|
||||
// // case ErrorCode.MaximumAllowedGrade:
|
||||
// // this.status = 400;
|
||||
// // break;
|
||||
// // case ErrorCode.AsyncError:
|
||||
// // this.status = 400;
|
||||
// // break;
|
||||
// // case ErrorCode.NotFound:
|
||||
// // this.status = 404;
|
||||
// // break;
|
||||
// // default:
|
||||
// // this.status = 500;
|
||||
// // break;
|
||||
// // }
|
||||
// }
|
||||
}
|
||||
|
||||
export class BadOaiModelException extends OaiModelException {
|
||||
constructor(message?: string) {
|
||||
super(StatusCodes.INTERNAL_SERVER_ERROR, message || "bad Request", OaiErrorCodes.BADARGUMENT);
|
||||
this.stack = "";
|
||||
}
|
||||
}
|
||||
|
||||
// export default OaiModelexception;
|
11
src/exceptions/PageNotFoundException.ts
Normal file
11
src/exceptions/PageNotFoundException.ts
Normal file
|
@ -0,0 +1,11 @@
|
|||
import { StatusCodes } from "http-status-codes";
|
||||
import HTTPException from "./HttpException";
|
||||
|
||||
class PageNotFoundException extends HTTPException {
|
||||
constructor(message?: string) {
|
||||
super(StatusCodes.NOT_FOUND, message || "Page not found");
|
||||
this.stack = "";
|
||||
}
|
||||
}
|
||||
|
||||
export default PageNotFoundException;
|
23
src/exceptions/error-handler.ts
Normal file
23
src/exceptions/error-handler.ts
Normal file
|
@ -0,0 +1,23 @@
|
|||
import { Request, Response } from "express";
|
||||
// import { ErrorCode } from './error-code';
|
||||
// import { ErrorException } from './error-exception';
|
||||
// import { ErrorModel } from './error-model';
|
||||
import HTTPException from "./HttpException";
|
||||
import { StatusCodes } from "http-status-codes";
|
||||
import { OaiModelException } from "./OaiModelException";
|
||||
|
||||
export const errorHandler = (err: HTTPException | OaiModelException, req: Request, res: Response) => {
|
||||
console.log("Error handling middleware called.");
|
||||
console.log("Path:", req.path);
|
||||
console.error("Error occured:", err);
|
||||
if (err instanceof HTTPException) {
|
||||
console.log("Http Error is known.");
|
||||
res.status(err.status).send(err);
|
||||
} else if (err instanceof OaiModelException) {
|
||||
console.log("Oai-Error is known.");
|
||||
res.status(err.status).send(err);
|
||||
} else {
|
||||
// For unhandled errors.
|
||||
res.status(500).send({ code: StatusCodes.INTERNAL_SERVER_ERROR, status: 500 });
|
||||
}
|
||||
};
|
Loading…
Add table
editor.link_modal.header
Reference in a new issue