<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: folacode22</title>
    <description>The latest articles on Forem by folacode22 (@folacode22).</description>
    <link>https://forem.com/folacode22</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F901469%2Fb0b6786a-7798-4078-97ff-4a3b698b3ab9.jpg</url>
      <title>Forem: folacode22</title>
      <link>https://forem.com/folacode22</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/folacode22"/>
    <language>en</language>
    <item>
      <title>Handling exception in NodeJS Express</title>
      <dc:creator>folacode22</dc:creator>
      <pubDate>Tue, 30 Aug 2022 06:05:19 +0000</pubDate>
      <link>https://forem.com/folacode22/handling-exception-in-nodejs-express-4kdo</link>
      <guid>https://forem.com/folacode22/handling-exception-in-nodejs-express-4kdo</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--v2lHp3kA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aw2m6wu2qjlwzry4ysex.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--v2lHp3kA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aw2m6wu2qjlwzry4ysex.jpg" alt="hero" width="303" height="166"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Debugging errors is the hardest part of programming. Errors can appear in your code in a variety of ways, whether as syntax errors, errors in logic, or the most dreaded of all, runtime errors. Runtime errors occur whenever something unexpected occurs in your application, and they often lead to catastrophic issues that can crash your program.&lt;/p&gt;

&lt;p&gt;Like many languages, Node.js provides a mechanism to anticipate errors before they occur. When an error occurs in your code, it turns into an object called an exception. Properly handling these exceptions allows you to recover gracefully from unforeseen issues, resulting in a much better user experience.&lt;/p&gt;

&lt;h4&gt;
  
  
  Table of contents
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Creating exceptions&lt;/li&gt;
&lt;li&gt;Error objects&lt;/li&gt;
&lt;li&gt;Handling exceptions&lt;/li&gt;
&lt;li&gt;Catching uncaught exceptions&lt;/li&gt;
&lt;li&gt;Exceptions with promises&lt;/li&gt;
&lt;li&gt;error handling in synchronous&lt;/li&gt;
&lt;li&gt;error handling in express&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Creating exceptions
&lt;/h3&gt;

&lt;p&gt;An exception is created using the &lt;strong&gt;throw&lt;/strong&gt; keyword:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uqngJOaS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1gtdbm7wy5vlhio5aiwf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uqngJOaS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1gtdbm7wy5vlhio5aiwf.png" alt="throw" width="234" height="217"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As soon as JavaScript executes this line, the normal program flow is halted and the control is held back to the nearest exception handler.&lt;/p&gt;

&lt;p&gt;Usually in client-side code value can be any JavaScript value including a string, a number or an object.&lt;/p&gt;

&lt;p&gt;In Node.js, we don't throw strings, we just throw Error objects.&lt;/p&gt;

&lt;h3&gt;
  
  
  Error objects
&lt;/h3&gt;

&lt;p&gt;An error object is an object that is either an instance of the Error object, or extends the Error class, provided in the Error core module:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1lwKdRDH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lk02ozsuob6eugq3zn2o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1lwKdRDH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lk02ozsuob6eugq3zn2o.png" alt="throw 1" width="840" height="336"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Handling exceptions
&lt;/h3&gt;

&lt;p&gt;An exception handler is a try/catch statement.&lt;/p&gt;

&lt;p&gt;Any exception raised in the lines of code included in the try block is handled in the corresponding catch block:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;try {
  // lines of code
} catch (e) {}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;e in this example is the exception value.&lt;/p&gt;

&lt;p&gt;You can add multiple handlers, that can catch different kinds of errors.&lt;/p&gt;

&lt;h3&gt;
  
  
  Exceptions with promises
&lt;/h3&gt;

&lt;p&gt;Using promises you can chain different operations, and handle errors at the end:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
performFunction1()
  .then(performFunction2)
  .then(performFunction3)
  .catch(err =&amp;gt; console.error(err));

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How do you know where the error occurred? You don't really know, but you can handle errors in each of the functions you call (performfunction(x)), and inside the error handler throw a new error, that's going to call the outside catch handler:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const performFunction1 = () =&amp;gt; {
  // ...
  try {
    // ...
  } catch (err) {
    // ... handle it locally
    throw new Error(err.message);
  }
  // ...
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To be able to handle errors locally without handling them in the function we call, we can break the chain. You can create a function in each then() and process the exception:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;performFunction1()
  .then(() =&amp;gt; {
    return performFunction2().catch(err =&amp;gt; {
      // handle error
      throw err; // break the chain!
    });
  })
  .then(() =&amp;gt; {
    return performFunction3().catch(err =&amp;gt; {
      // handle error
      throw err; // break the chain!
    });
  })
  .catch(err =&amp;gt; console.error(err));

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Error Handling in express
&lt;/h3&gt;

&lt;p&gt;Error Handling refers to how Express catches and processes errors that occur both synchronously and asynchronously. Express comes with a default error handler so you don’t need to write your own to get started.&lt;/p&gt;

&lt;p&gt;It’s important to ensure that Express catches all errors that occur while running route handlers and middleware.&lt;/p&gt;

&lt;p&gt;Errors that occur in synchronous code inside route handlers and middleware require no extra work. If synchronous code throws an error, then Express will catch and process it. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.get('/', (req, res) =&amp;gt; {
  throw new Error('BROKEN') // Express will catch this on its own.
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For errors returned from asynchronous functions invoked by route handlers and middleware, you must pass them to the next() function, where Express will catch and process them. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.get('/', (req, res, next) =&amp;gt; {
  fs.readFile('/file-does-not-exist', (err, data) =&amp;gt; {
    if (err) {
      next(err) // Pass errors to Express.
    } else {
      res.send(data)
    }
  })
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;error in asynchronous code, route handlers and middleware that return a Promise will call next(value) automatically when they reject or throw an error. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.get('/user/:id', async (req, res, next) =&amp;gt; {
try{
  const user = await getUserById(req.params.id)
  res.send(user)}
catch(err){next(err)}
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>node</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>How to resolve merge Conflict in git ?</title>
      <dc:creator>folacode22</dc:creator>
      <pubDate>Sun, 07 Aug 2022 21:02:00 +0000</pubDate>
      <link>https://forem.com/folacode22/how-to-resolve-merge-conflict-in-git--mko</link>
      <guid>https://forem.com/folacode22/how-to-resolve-merge-conflict-in-git--mko</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lwdbRV4v--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i4zncxo11oh91o1nj33a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lwdbRV4v--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i4zncxo11oh91o1nj33a.png" alt="git image" width="880" height="415"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt; Table of contents&lt;/h3&gt;


&lt;ul&gt;

&lt;li&gt;what is git?&lt;/li&gt;

&lt;li&gt;why git?&lt;/li&gt;

&lt;li&gt;basic git command&lt;/li&gt;

&lt;li&gt;type of git merge conflict&lt;/li&gt;

&lt;li&gt;how to resolve merge conflict in git?&lt;/li&gt;

&lt;li&gt;git commands to resolve conflicts &lt;/li&gt;

&lt;ul&gt;

&lt;h2 id="1"&gt;What is Git?&lt;/h2&gt;

&lt;p&gt;Git is an open-source, distributed version control system (VCS) designed to handle everything from small to very large projects with speed and efficiency, which has a remote repository on the server-side and a local repository on the client-side. This means that the file or code is not present on a central server but there is a copy of the file stored on the client’s computer.&lt;/p&gt;

&lt;h2 id="2"&gt; why git?&lt;/h2&gt;

&lt;p&gt;Simply put, Git is a version control system that lets you manage and keep track of your source code history. GitHub is a cloud-based hosting service that lets you manage Git repositories. If you have open-source projects that use Git, then GitHub is designed to help you better manage them.&lt;/p&gt;

&lt;h2 id="3"&gt; Basic Git Commands &lt;/h2&gt;



&lt;/ul&gt;
&lt;/ul&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
- git config 
- git init
- git add . 
- git diff
- git commit
- git reset
- git status
- git merge
- git push
- git pull
&lt;/code&gt;&lt;/pre&gt;



&lt;h2 id="4"&gt; What is a Git Merge Conflict?&lt;/h2&gt;

&lt;p&gt;A merge conflict is an event that takes place when Git is unable to automatically resolve differences in code between two commits. Git can merge the changes automatically only if the commits are on different lines or branches.&lt;/p&gt;

&lt;h2 id="5"&gt;Types of Git Merge Conflicts &lt;/h2&gt;

&lt;p&gt;There are two points when a merge can enter a conflict state: &lt;/p&gt;


&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Starting the Merge Process If there are changes in the working directory’s stage area for the current project, merging won’t start. In this case, conflicts happen due to pending changes that need to be stabilized using different Git commands.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;During the Merge Process The failure during the merge process indicates that there is a conflict between the local branch and the branch being merged. In this case, Git resolves as much as possible, but there are things that have to be resolved manually in the conflicted files. We will now go over resolving merge conflicts in Git.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id="6"&gt; How to Resolve Merge Conflicts in Git?&lt;/h2&gt;

&lt;p&gt;There are a few steps that could reduce the steps needed to resolve merge conflicts in Git.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The easiest way to resolve a conflicted file is to open it and make any necessary changes&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After editing the file, we can use the git add command to stage the new merged content.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The final step is to create a new commit with the help of the git commit command. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Git will create a new merge. commit to finalize the merge&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=""&gt; Git Commands to Resolve Conflicts&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;The git log --merge command helps to produce the list of commits that are causing the conflict.
&amp;gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; git log --merge

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;The git diff command helps to identify the differences between the states repositories or files.
&amp;gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git diff 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;The git checkout command is used to undo the changes made to the file, or for changing branches
&amp;gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; git checkout 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;The git reset --mixed command is used to undo changes to the working directory and staging area
&amp;gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; git reset --mixed 

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;The git merge --abort command helps in exiting the merge process and returning back to the state before the merging began.
&amp;gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git merge --abort

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;The git reset command is used at the time of merge conflict to reset the conflicted files to their original state
&amp;gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git reset
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>git</category>
      <category>github</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
