Skip to content

Commit e464f2e

Browse files
committed
Updates to common package
1 parent 78a2b9f commit e464f2e

16 files changed

Lines changed: 171 additions & 3 deletions
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { CustomError } from './custom-error';
2+
export declare class BadRequestError extends CustomError {
3+
statusCode: number;
4+
constructor(message: string);
5+
formatError(): {
6+
message: string;
7+
};
8+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.BadRequestError = void 0;
4+
const custom_error_1 = require("./custom-error");
5+
class BadRequestError extends custom_error_1.CustomError {
6+
constructor(message) {
7+
// This message will be console logged in error handling middleware
8+
super(`Bad request error: ${message}`);
9+
this.statusCode = 400;
10+
Object.setPrototypeOf(this, BadRequestError.prototype);
11+
}
12+
// Formats the error message to send back to the client
13+
formatError() {
14+
return { message: this.message };
15+
}
16+
}
17+
exports.BadRequestError = BadRequestError;
18+
// const err = new BadRequestError('Please provide valid inputs')
19+
// err.formatError() -> to the client
20+
// throw new NotFoundError()
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export declare abstract class CustomError extends Error {
2+
abstract statusCode: number;
3+
constructor(message: string);
4+
abstract formatError(): {
5+
message: string;
6+
};
7+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"use strict";
2+
// Custom Error class to reinforce error class structure
3+
Object.defineProperty(exports, "__esModule", { value: true });
4+
exports.CustomError = void 0;
5+
// abstract - cannot instantiate CustomError, only inherit from it (extend it)
6+
class CustomError extends Error {
7+
constructor(message) {
8+
// Serves as the message for the Error class that CustomError is extending
9+
super(message);
10+
// Make sure properties we are setting end up on CustomError's prototype?
11+
Object.setPrototypeOf(this, CustomError.prototype);
12+
}
13+
}
14+
exports.CustomError = CustomError;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { CustomError } from './custom-error';
2+
export declare class DbConnectionError extends CustomError {
3+
statusCode: number;
4+
constructor();
5+
formatError(): {
6+
message: string;
7+
};
8+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.DbConnectionError = void 0;
4+
const custom_error_1 = require("./custom-error");
5+
class DbConnectionError extends custom_error_1.CustomError {
6+
constructor() {
7+
super('Database connection error');
8+
this.statusCode = 502;
9+
Object.setPrototypeOf(this, DbConnectionError.prototype);
10+
}
11+
formatError() {
12+
return { message: 'Database connection error' };
13+
}
14+
}
15+
exports.DbConnectionError = DbConnectionError;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { CustomError } from './custom-error';
2+
export declare class NotAuthorizedError extends CustomError {
3+
statusCode: number;
4+
constructor();
5+
formatError(): {
6+
message: string;
7+
};
8+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.NotAuthorizedError = void 0;
4+
const custom_error_1 = require("./custom-error");
5+
class NotAuthorizedError extends custom_error_1.CustomError {
6+
constructor() {
7+
// This message will be console logged in error handling middleware
8+
super(`Not Authorized`);
9+
this.statusCode = 401;
10+
Object.setPrototypeOf(this, NotAuthorizedError.prototype);
11+
}
12+
// Formats the error message to send back to the client
13+
formatError() {
14+
return { message: `Not Authorized` };
15+
}
16+
}
17+
exports.NotAuthorizedError = NotAuthorizedError;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { CustomError } from './custom-error';
2+
export declare class NotFoundError extends CustomError {
3+
statusCode: number;
4+
constructor();
5+
formatError(): {
6+
message: string;
7+
};
8+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.NotFoundError = void 0;
4+
const custom_error_1 = require("./custom-error");
5+
// Custom error for NotFound
6+
class NotFoundError extends custom_error_1.CustomError {
7+
constructor() {
8+
// This message will be console logged in error handling middleware
9+
super('Not found error');
10+
this.statusCode = 404;
11+
Object.setPrototypeOf(this, NotFoundError.prototype);
12+
}
13+
// Formats the error message to send back to the client
14+
formatError() {
15+
return { message: 'Not found error' };
16+
}
17+
}
18+
exports.NotFoundError = NotFoundError;

0 commit comments

Comments
 (0)