Is Error Handling Hard or easy in Node.Js?

Updated: Mar 7

Error Handling is very much important for ever project.

Errors are not necessary the end of the app. You should handle then correctly!

You can differentiate between different types of errors – technical errors (which are thrown) and ”expected errors” (e.g. invalid user input)

• Error handling can be done with custom if-checks, try-catch, then()-catch() etc.

• We can use the Express error handling middleware to handle all unhandled errors

Types of Errors

  1. Technical/Network Errors

  2. 2. Expected Errors (not Official term but its is what I called them)

  3. Bugs/Logical Errors

1. Technical Errors

The errors because of the data base interaction or the bridge is broken or The server of the database is down(i.e. MongoDb server is down)

To get rid of that

Show error page to the user.

2. Expected Errors:

These errors are when a file cant be read by the database operation fails.

To get rid of that

Inform user about the issue or tell them to possibly retry again

3. Bugs/Logical Errors

These errors occurs when User object is used and it doesn't exist.

To get rid of this error:

Fix during development: We should not show any message this is the developers fault not the user's fault.

Working with Errors:

  1. Error is Thrown

  2. No Error is thrown

Error is thrown:

We have certain tools we can use to test code and catch potential errors so that we can handle easily

Synchronous code - try-catch

Asynchronous code - then()-catch()

In both of the cases we can directly handle errors or User Express error handling function

No error is thrown:

We need to validate the values first then we can either throw error or Directly handle error.

directly handling error which is technical error but where we simply add some code that can continue with the missing input data.

In all the cases we have our different user interface such as Error page,

Intended Page/Response with error information or we can redirect in our No error thrown case.

Error Handling Middleware Correctly

If We throw error outside the Async code so in a place where code executes synchronously so basically outside the promise, then catch block or outside the callback.

2 views0 comments

Recent Posts

See All

SQL UNION Operator UNION operator is used to combine the results of two or more SELECT statements Every SELECT statement within UNION must have the same number of columns The columns must also have si

JOIN clause is used to combine rows from two or more tables. INNER JOIN === selects records that have matching values in both tables SELECT Orders.OrderID, Customers.CustomersID, Orders.OrdersDate FRO