Skip to content

Commit 433a578

Browse files
author
sourabh
committed
handle - everything about error handling
1 parent 5c754e8 commit 433a578

File tree

4 files changed

+62
-15
lines changed

4 files changed

+62
-15
lines changed

controllers/home.js

+16-12
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
exports.home = (req,res)=>{
2-
res.status(200).json({
3-
success:true,
4-
gretting:"Hello,Home Page From API"
5-
})
6-
}
7-
exports.Dummy = (req,res)=>{
8-
res.status(200).json({
9-
success:true,
10-
gretting:"Hello,Dummy From API"
11-
})
12-
}
1+
const BigPromise = require("../middlewares/bigPromise");
2+
3+
exports.home = BigPromise(async(req, res) => {
4+
//const db = await something();
5+
res.status(200).json({
6+
success: true,
7+
gretting: "Hello,Home Page From API",
8+
});
9+
});
10+
11+
exports.Dummy = (req, res) => {
12+
res.status(200).json({
13+
success: true,
14+
gretting: "Hello,Dummy From API",
15+
});
16+
};

map_project.js

+23-3
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,34 @@ Ref: ✈️🔗https://expressjs.com/en/5x/api.html#router
1616
@ABOUT_MORGAN
1717
Ref: ✈️🔗 https://www.npmjs.com/package/morgan
1818
SCROLL ON till Bottom ...above link
19-
😗@NOTE: NEEDs to be come befoure route it's convection
19+
😗@NOTE: NEEDs to be come befour route it's convection
2020
2121
@INVITE_&_INSTALL:-> MORGAN,cookieParser,fileUpload,swaggerUi & some REGULAR MIDDLEWARE like json & urlencoded
2222
23+
-----------------NEW--------------------
24+
@SECTION: BASIC CONFIG & CONTROLLER
25+
@TITLE: CUSTOM ERROR HANDLERS
26+
@ABOUT: All about Inject MIDDLEWARE
27+
@LOCATION: 🗃️utils/customError.js
28+
@REF: ✈️🔗https://nodejs.org/api/errors.html#class-error
29+
@OVERVIEW:->
30+
-It an Node Js error Handling it's very POWERFUL
31+
-create error base on class & exports(Production use)
32+
-customize Error A/C to Ref Link
33+
34+
-----------------NEW--------------------
35+
@SECTION: BASIC CONFIG & CONTROLLER
36+
@TITLE: THE Big PROMISE
37+
@ABOUT: All About DB Connetion(Which Problebly take time)
38+
@LOCATION: 🗃️middleware/bigPromise.js
39+
40+
@OVERVIEW:
41+
-Concern About weather the Connetion Connect or Not to DB
42+
-use funtional Programming
43+
-In Order to Clear More Take Refference of LOCATION & visit file
2344
2445
25-
2646
2747
2848
29-
*/
49+
*/

middlewares/bigPromise.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module.exports = func => (req,res,next)=>{
2+
Promise.resolve(func(req,res,next)).catch(next)
3+
}
4+
5+
/*
6+
-🎗️Part Of Funtional Programming
7+
-🎗️Use Promise In Javascript which Handles asynchronous operations;
8+
-🎗️A promise is created using the Promise constructor which takes in a callback function with two parameters, resolve and reject respectively
9+
resolve:-> when the async operation has been successfully completed.
10+
reject :-> when the async operation fails or if some error occurs.
11+
-🎗️In case we use alternative .catch instead reject If anything bad then next back!
12+
@FOLLOW_PROCES_&_CHOOSE_ONE
13+
try catch async await || bigPromise
14+
-🎗️import in home CONTROLLER because promise expect one one function that is present on constroller that's MOTO
15+
*/

utils/customError.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class CustomError extends Error {
2+
constructor(message, code) {
3+
super(message);
4+
this.code = code;
5+
}
6+
}
7+
8+
module.exports = CustomError;

0 commit comments

Comments
 (0)