Skip to content

Commit 30d1add

Browse files
committed
(bug-solved) solved bug (requires mongoose and zod by default. #1)
1 parent 68b096d commit 30d1add

File tree

6 files changed

+52
-40
lines changed

6 files changed

+52
-40
lines changed

core/exhancer.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,26 @@ export class Exhancer {
55

66
#directory
77
#fsRouting
8+
#errorHandler
89

910
/**
1011
*
1112
* @param config {Object}
1213
* @param config.fsRouting {boolean}
1314
* @param config.directory {string}
1415
*/
15-
constructor({fsRouting = false, directory= "src"}) {
16+
constructor(config) {
1617
this.app = express();
17-
this.#fsRouting = fsRouting;
18+
this.#fsRouting = config?.fsRouting || false;
1819
this.#directory = directory;
20+
this.#errorHandler = []
21+
}
22+
23+
/**
24+
* @param handlers {() => {message: string, status: number}}
25+
* */
26+
onError(handlers) {
27+
this.#errorHandler = handlers
1928
}
2029

2130
/**
@@ -25,15 +34,15 @@ export class Exhancer {
2534
*/
2635
async run(port, callback) {
2736
try {
28-
await loader(this.app, {prefix: this.directory})
37+
const isLoaded = await loader(this.app, { watch: this.#directory, handlers: this.#errorHandler || [], prefix: "/api" })
2938
}
30-
39+
3140
catch (e) {
41+
console.log(e)
3242
console.log(`[EXHANCER] ⚠️ Failed to load routes from /${this.#directory} \n ${e} `)
3343
}
34-
finally {
35-
this.app.listen(port, callback)
36-
}
44+
45+
this.app.listen(port, callback)
3746
}
3847
}
3948

core/middlewares.js

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
1-
import mongoose from "mongoose";
2-
import {ZodError} from "zod";
3-
4-
import mongoHanlder from "../plugins/errors/mongoose-errors.js"
5-
import zodHandler from "../plugins/errors/zod-error.js"
6-
7-
export function exhancerErrorHandler() {
1+
export function exhancerErrorHandler(handler) {
82
let errMessage = "";
9-
let errStatus = 500;
3+
let errStatus = 0;
104

11-
return (err, req, res) => {
12-
if (err instanceof mongoose.mongo.MongoServerError) {
13-
const {message, status} = mongoHanlder(err);
14-
errMessage = message
15-
errStatus = status
16-
}
5+
console.log(handler.length)
6+
7+
return (err, req, res, next) => {
8+
for (let i = 0; i < handler?.length; i++) {
9+
const result = handler[i]?.default(err);
1710

18-
if(err instanceof ZodError) {
19-
const {message, status} = zodHandler(err);
20-
errMessage = message
21-
errStatus = status
11+
if (result?.message || result?.status) {
12+
errMessage = result?.message
13+
errStatus = result?.status
14+
break;
15+
}
2216
}
2317

2418
res.status(errStatus || err?.status || 500).send({

index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
import * as exhancer from "./core/exhancer.js"
22

3+
import * as mongoError from "./plugins/errors/mongoose-errors.js";
4+
import * as zodError from "./plugins/errors/zod-error.js";
5+
6+
export {
7+
mongoError,
8+
zodError
9+
}
310
export default exhancer;

plugins/errors/mongoose-errors.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import mongoose from "mongoose";
2+
13
/**
24
*
35
* @param error {Error}
46
* @returns {{message: string, status: number}}
57
*/
6-
export default function handler(error) {
8+
export default function mongoError(error) {
9+
if(error instanceof mongoose.Mon)
710
switch (error.code) {
811
case 11000:
912
const alreadyExists = Object.keys(error?.keyPattern);

plugins/errors/zod-error.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @param error {Error}
44
* @returns {{message: string, status: number}}
55
*/
6-
export default function handler(error) {
6+
export default function zodError(error) {
77
const message = JSON.parse(error.message);
88
const errorMessages = message.map((errorMsg) => {
99

plugins/loader.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
11
import fs from "fs"
22
import path from "path"
3-
import {exhancerErrorHandler} from "../core/middlewares.js";
4-
import * as url from "url";
5-
6-
const __filename = url.fileURLToPath(import.meta.url);
7-
export const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
3+
import { exhancerErrorHandler } from "../core/middlewares.js";
4+
import * as url from "url";
85

96
/**
107
*
118
* @param app {import("express").application}
129
* @param config {Object}
13-
* @param config.prefix {String}
10+
* @param config.prefix {string}
11+
* @param config.handlers {() => {message: string, status: number }[]}
12+
* @param config.watch {string}
1413
* @returns {Promise<boolean>}
1514
*/
1615
async function loader(app, config) {
17-
const routesPath = path.resolve(__dirname, prefix)
18-
const routes = await fs.readdirSync(routesPath);
16+
17+
const routes = await fs.readdirSync(config.watch);
1918

2019
routes.forEach(async (file, i) => {
21-
const filePath = path.resolve(routesPath, file, "index.js");
20+
const filePath = path.resolve(config.watch, file, "index.js");
2221
const loaderFn = await import("file:///" + filePath)
2322

24-
app.use(`${prefix}/${file}`, loaderFn.default())
25-
console.log(`[EXHANCER] ⚡ Loaded "${prefix}/${file}" `)
23+
app.use(`${config.prefix}/${file}`, loaderFn.default())
24+
console.log(`[EXHANCER] ⚡ Loaded "${config.prefix}/${file}" `)
2625

27-
if(routes?.length === i + 1) {
28-
app.use(exhancerErrorHandler());
26+
if (routes?.length === i + 1 && config.handlers?.length) {
27+
app.use(exhancerErrorHandler(config.handlers));
2928
console.log(`[EXHANCER] ⚡ Loaded Error Handler `)
3029
}
3130
})

0 commit comments

Comments
 (0)