Get rid of try-catch in server-side development, here’s a better way!

PS: This write-up goes well with node.js or express.js development

We’ll go knocking on the try-catch statement door whenever we encounter an asynchronous function in javascript – the traditional way. It is and will always be the backbone to handling…


This content originally appeared on DEV Community and was authored by Muzammil

PS: This write-up goes well with node.js or express.js development

We'll go knocking on the try-catch statement door whenever we encounter an asynchronous function in javascript - the traditional way. It is and will always be the backbone to handling async-await functions in server-side development.
It has been of great help when we had to avoid the callback function and got us out of callback hell several times.

But what if we can enhance this and make the try-catch statements shorter and cleaner. Here goes our typical way of handling things.

async function main(req, res, next) {
  try {
    var result = await doSomething();
    console.log(result);
  } catch (error) {
    console.error(error);
  }
}

Being Javascript developers, we are familiar with the above code, result is printed to the console if everything goes smooth or else error gets printed - 🍰

A Cleaner Alternative

Say if we're using Express framework and handling requests using try-catch, here's something cleaner and simpler that can be replaced for the never-ending lengthy statements.

const catchAsync = (fn) => (req, res, next) =>{
    Promise.resolve(fn(req, res, next)).catch((err) => next(err));
});

catchAsync function can be considered as a middleware that just accepts the parameters (req, res, next). If there's any error, then it returns the error.

This is how we can call our enhanced error catching function to avoid try-catch statements.

const sampleFunction = catchAsync(async (req, res) => {
           const awaitedResponse = await getResponse();
           res.send(awaitedResponse);
});

catchAsync can be called anywhere we need to implement our traditional try-catch logic. Let's keep enhancing our good old lengthy statements and make javascript simpler for budding developers


This content originally appeared on DEV Community and was authored by Muzammil


Print Share Comment Cite Upload Translate Updates
APA

Muzammil | Sciencx (2021-12-01T17:32:55+00:00) Get rid of try-catch in server-side development, here’s a better way!. Retrieved from https://www.scien.cx/2021/12/01/get-rid-of-try-catch-in-server-side-development-heres-a-better-way/

MLA
" » Get rid of try-catch in server-side development, here’s a better way!." Muzammil | Sciencx - Wednesday December 1, 2021, https://www.scien.cx/2021/12/01/get-rid-of-try-catch-in-server-side-development-heres-a-better-way/
HARVARD
Muzammil | Sciencx Wednesday December 1, 2021 » Get rid of try-catch in server-side development, here’s a better way!., viewed ,<https://www.scien.cx/2021/12/01/get-rid-of-try-catch-in-server-side-development-heres-a-better-way/>
VANCOUVER
Muzammil | Sciencx - » Get rid of try-catch in server-side development, here’s a better way!. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/01/get-rid-of-try-catch-in-server-side-development-heres-a-better-way/
CHICAGO
" » Get rid of try-catch in server-side development, here’s a better way!." Muzammil | Sciencx - Accessed . https://www.scien.cx/2021/12/01/get-rid-of-try-catch-in-server-side-development-heres-a-better-way/
IEEE
" » Get rid of try-catch in server-side development, here’s a better way!." Muzammil | Sciencx [Online]. Available: https://www.scien.cx/2021/12/01/get-rid-of-try-catch-in-server-side-development-heres-a-better-way/. [Accessed: ]
rf:citation
» Get rid of try-catch in server-side development, here’s a better way! | Muzammil | Sciencx | https://www.scien.cx/2021/12/01/get-rid-of-try-catch-in-server-side-development-heres-a-better-way/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.