<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by emmanuel Chirchir on Medium]]></title>
        <description><![CDATA[Stories by emmanuel Chirchir on Medium]]></description>
        <link>https://medium.com/@chirchir7370?source=rss-d84cf33f4512------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*O9QsdnJqSKGQxAdbiVczXA.jpeg</url>
            <title>Stories by emmanuel Chirchir on Medium</title>
            <link>https://medium.com/@chirchir7370?source=rss-d84cf33f4512------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 22:26:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@chirchir7370/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Handling Errors in Full-Stack Applications (React.js & Node.js)]]></title>
            <link>https://medium.com/@chirchir7370/handling-errors-in-full-stack-applications-react-js-node-js-389e31c12468?source=rss-d84cf33f4512------2</link>
            <guid isPermaLink="false">https://medium.com/p/389e31c12468</guid>
            <category><![CDATA[react-hook]]></category>
            <category><![CDATA[sequelize]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[react-context-api]]></category>
            <dc:creator><![CDATA[emmanuel Chirchir]]></dc:creator>
            <pubDate>Sat, 06 Jun 2020 18:26:14 GMT</pubDate>
            <atom:updated>2020-06-07T06:49:01.002Z</atom:updated>
            <content:encoded><![CDATA[<p>Error handling is one of the most important requirements in software development. It matters for either small or big projects because, without it, your users will be left wondering and guessing why certain actions did or did not happen. trust me, when errors are handled prematurely, it translates to bad user experience and hence continuous reduction of traffic to your application. While working on a Node.js API and react application, I realized that there are so many ways to prematurely handle errors and there are better ways to maturely deal with these mistakes. Hang in there, let me demonstrate what I am saying with a real application. you can look at the <a href="https://github.com/chirchir12/FULLSTACK-APPS-SERIES/tree/master/crud">source code</a>. The application is a full-stack crud app used to manage employees with an API running on Node and client built with react. For the back-end, I am using express.js to manage routes, Sequelize ORM, and Sequelize-CLI to manage migrations and MYSQL as my database. for the front-end, I am using function-based react components with React hooks. I am also leveraging React context API to store data and react-router-dom to manage routes. I will try and handle errors for fetching a list of employees and display the appropriate message to a user.</p><p><strong>Handling Back-End Errors</strong></p><p>Express.js allows us to create route handlers that return a promise like so</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/879/1*ZYS6KX0Efo4mhryLlc7jAw.png" /><figcaption>express route handler</figcaption></figure><p>the handler has a request and response object and an optional callback <em>next</em> function. Before I knew there is a better way to handle errors in Express.js, this is how I used to handle it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/909/1*N1XvSJY9MLlIW0LToQXaLA.png" /></figure><p>If the <em>findAll</em> method rejects or throws an error, the catch block will handle it. from the screenshot above, you realize that we are generalizing all errors to be 404 and that is not true. what we really want is to return <em>404 status code</em> and <em>not found</em> error message when the client hits this endpoint and finds no resources or <em>server error (500) </em>when something else happened. when searching for better ways, I found that express.js has a default error handler which actually runs when none of our error catching technique works. This error handler receives the error object from our routes through the next callback function. We can customize this error handler to meet our needs. This custom error handler is a middleware that is placed at the end of the middleware function stack (and after your routes please).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/828/1*ewwuMxW3fKsMFOJEdDUFFg.png" /><figcaption>our custom error handler</figcaption></figure><p>From the above screenshot, you can see that the handler takes in four arguments and I am more interested in the <em>error</em> object which contains errors reject or thrown by our route handler. All we now need to do is to create the error object with a <em>message</em> and <em>status code </em>and let catch and next callback function do the magic for us. We are using the <em>http-errors </em>package to help us easily create an error object. Our updated route should now look like below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/903/1*vEeno0SLFx0mh6dFFlZZZw.png" /></figure><p><strong>Handling Front-End Errors</strong></p><p>When the native fetch API in our react app hits the above route handler, it is expecting to either return list of employees or an error message that explains why it couldn’t find anything. I will not go deep into how the Fetch API works but you can find more information <a href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API">here</a>. Here is my version before I found that there is a better way.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/757/1*SZ4rYdEnw65IG1fWdWSanQ.png" /></figure><p>from the screenshot, there is no absolute way our application will know something happened and our users will be kept in the dark waiting for something to happen. The reformed me will handle this in a better way by creating three objects in my state;<em> employee, error, and isLoading</em>. The employee object will contain a list of employees if found. the error object will contain errors from backend and isLoading is a boolean that determines if Fetch is still fetching data from the backend. Here is my state.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/862/1*8mnwe-W0UOqpWWjYH0-RlA.png" /></figure><p>The interesting part about all this is how we fetch data from our API. Fetch API is promised-based and only rejects when there is a network error. When it resolves, the response object will contain some properties and we are interested in <em>ok</em> and <em>statusText</em>. In whichever way we take it, we have to find a way to activate the catch block to capture our errors. Here is how I do it now.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/854/1*WX2qrsbYLMJ6HTAEdudjYQ.png" /></figure><p>Note that when the response object is <em>ok</em>, it means we are expecting some good results but when it is false, it means it failed and we need to alert our users of what happened. <br>That is all for now. If you liked this article, join me and lets clap.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=389e31c12468" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>