REST API Tutorial (Node, Express & Mongo) #10 - Error Handling

preview_player
Показать описание
Hey gang, in this REST API tutorial I'll show you how we can make some simple middleware to handle our validation errors.

----- COURSE LINKS:

---------------------------------------------------------------------------------------------

========== PSD to WordPress Playlist ==========

============== The Net Ninja =====================

================== Social Links ==================

Рекомендации по теме
Комментарии
Автор

this is probably the most important episode in the series, creates custom middleware, handle error, explained the next parameter. Bravo

brutalbutler
Автор

Hey Ninja thank you for the amazing tutorials. I have graduated and believe me the knowledge that i have gained from my 4 years of university is nothing compared to what you have taught on your channel.

premuifyify
Автор

router.post('/developers', (req, res) => {
Developer.create(req.body)
.then((developer) => {
res.send(developer);
})
.catch((err)=>{
res.send(err.message);
});

Works too. learnt from the same shaun who taught us node js

yeeeeetcode
Автор

Almost 3 years from deploying the tutorial this is still useful... thanks a lot !

tiagooliveira
Автор

My man..! it might be the coffee, the "Limitless", or idk what, but I am downright emotional about this video right now. Thank you so much. I'm a bit new to coding, but I love doing it, scored a bit of a dream job / paid-to-learn-style-internship, and I've been tasked with writing this backend code which is fairly straight forward but kind of middle-mans a lot of requests for the frontend client to actually hit completely different resources/APIs. Works beautifully if the user is an infallible robot of data input! I've been a bit blocked on a conceptual level (so many ways to skin a cat when it comes to code, some considerably better than others), and this just really cleared it up. I've gone from "where do I start? <sad face>" to "HOLD MY BEER, I CAN'T WAIT TO IMPLEMENT THIS!"

I LOVE the way you explained literally everything, but didn't belabor any points, either. Your speed might be a bit aggressive for some, but when you spend as many hours a week as I do on Google and SO, it's a welcome change of pace. There's something to be said for brevity (hi, my name's duplicity).

Truly, an excellent (brilliant?) video, and I rarely subscribe or return to channels, but I'm subscribing to yours. Immense gratitude from across the pond, positive energy, and I hope you have an incredible New Year!

sevendubb
Автор

I love how you split you videos up in several smaller vids rather than one big vid.

asdfasdfuhf
Автор

I came from some econ background.I started learning webdev 3 days ago from scratch. Really glad that that found you early on).

vfxenthusiast
Автор

I was waiting from last 3 days for this, thanks a lot.

brajeshanokha
Автор

Wow, amazing the way you explain and present.. really enjoying it... Thank you!

ozkarj.mobike
Автор

Hey, thank you for all these amazing videos. One thing I'd like to ask is how does the "next" parameter know that it will have to use the error handling middleware instead of the other middlewares present?? I'm having a hard time grasping the intuitive flow of it. A little help would be highly appreciated.

soumavabanerjee
Автор

Instead of middleware i used the following code and it worked ok for me


Ninja.create(req.body).then(function(data, err){
if(err) throw err;
else
res.send(data);// send json back to the user test using postman
}).catch(function(err){
res.status(422).send({error : err.message});
});


Can you explain which according to you is a better way to display error messages from the one u told or the above?

premuifyify
Автор

A simple call back function in the router.get method with params err and data can serve the purpose

aishikbhattacharya
Автор

Thank you so much for this course. Great content!

getulionm
Автор

error message will not display. I get the following:
DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

MakeRandomVids
Автор

Thank you for this great tutorial! You're awesome.

HugoRamirezSoto
Автор

Hi, Ninja, I am not sure whether we should hand errors in the entry file, i.e., "app.js" or "index.js". Obviously, not every error can be assigned a 422 status code. Maybe it's 500 or 403 (Permission Denied). I think may be you can consider useing .catch() and customize the error's status code. I DID handle errors in app.js or index.js, but then I realized that I can use Promise chain, i.e., the .catch API. I can handle errors in it and send a customized code which may be more suitable for the error.

yumingui
Автор

Great video. If I bring down the mongodb for say maintenance or server crash, the promise never returns and hence the client hangs for ever it seems. I even tried to bring mongodb back and hoping the reconnect logic will let it continue etc. But, it never
recovers from that stuck state it seems. If you can cover such real life scenarios, that would be awesome...

psangawar
Автор

finally i understand next xD Thank you!

ankhbayarbatsukh
Автор

You are a god, finally I could get this API shit in my head :D

Romucci
Автор

I think it took much longer before it went to the error handling middleware. I did mongoose.connect(uri, options) with
options having the following properties...

var options = {
useMongoClient: true,
autoIndex: false, // Don't build indexes
// reconnectTries: Number.MAX_VALUE, // Never stop trying to reconnect
// reconnectInterval: 500, // Reconnect every 500ms
poolSize: 10, // Maintain up to 10 socket connections
// If not connected, return errors immediately rather than waiting for reconnect
bufferMaxEntries: 0
};

With this options, it was returning immediately (Because of bufferMaxEntries is set to 0.

psangawar
join shbcf.ru