Dino Geek, try to help you

How to handle errors in Node.js?


In Node.js, we can handle errors using several approaches:

1. Error-first callbacks: The asynchronous functions in Node.js have a standard way to expose their errors: the error-first callback. The async function will pass its errors to a callback as the first argument.

```
const fs = require(‘fs’);
fs.readFile(‘a_file.txt’, function(err, data) { if(err) { console.error(‘There was an error reading the file!’, err); return; } // Otherwise handle the data
});
```

1. Try-catch block: Another way to handle errors are try-catch blocks. This is the synchronous counterpart to error-first callbacks. You put the code that may fail inside the “try” block, and handle the error in the “catch” block.

```
let data;
try { data = fs.readFileSync(‘a_file.txt’, ‘utf8’);
} catch (err) { console.error(`There was an error reading the file: ${err}`);
}
```

1. Event Emitters: Some objects in Node.js emit events, including errors events. We can listen to that using the object “.on” method.

```
const server = require(‘http’).createServer();
server.on(‘error’, function(err) { console.error(`There was an error: ${err}`);
});
```

1. Promise rejection: Promises represent asynchronous actions. When they fail, they can be “rejected”. This rejection can be caught and handled.

```
Promise.reject(new Error(‘Promise failed!’)) .then(() => { // This is never executed }) .catch(err => console.log(err)); // This will log: ‘Error: Promise failed!‘
```

1. Error handling middleware in Express.js: If you are dealing with the Express.js framework, there is a specific mechanism to handle errors through middleware.

```
app.use(function(err, req, res, next) { console.error(err.stack); res.status(500).send(‘Something broke!’);
});
```

Remember that unhandled errors often lead to an unstable system, so make sure to appropriately handle all errors in a way that makes sense for your application.


Simply generate articles to optimize your SEO
Simply generate articles to optimize your SEO





DinoGeek offers simple articles on complex technologies

Would you like to be quoted in this article? It's very simple, contact us at dino@eiki.fr

CSS | NodeJS | DNS | DMARC | MAPI | NNTP | htaccess | PHP | HTTPS | Drupal | WEB3 | LLM | Wordpress | TLD | Domain name | IMAP | TCP | NFT | MariaDB | FTP | Zigbee | NMAP | SNMP | SEO | E-Mail | LXC | HTTP | MangoDB | SFTP | RAG | SSH | HTML | ChatGPT API | OSPF | JavaScript | Docker | OpenVZ | ChatGPT | VPS | ZIMBRA | SPF | UDP | Joomla | IPV6 | BGP | Django | Reactjs | DKIM | VMWare | RSYNC | Python | TFTP | Webdav | FAAS | Apache | IPV4 | LDAP | POP3 | SMTP

| Whispers of love (API) | Déclaration d'Amour |






Legal Notice / General Conditions of Use