With async/await becoming standard procedure for a lot of my code, I find myself wrapping blocks of code in try/catch to ensure I’m handling errors properly.

As a result of this, I also try to make my errors a little more useful at the same that I want to show you in this mini post.

[I’ve published 38 videos for new developers, designers, UX, UI, product owners and anyone who needs to conquer the command line today.](https://training.leftlogic.com/buy/terminal/cli2?coupon=BLOG\&utm_source=blog\&utm_medium=banner\&utm_campaign=remysharp-discount)

The example[](#the-example)

I have large validation process that runs through a series of statements and checks for particular problems and rules.

Each validation rule is run and if it fails, it throws with a message, such as:

export function validateFakeExample(token, scope) {
  if (token.text !== 'PRINT') return;

  const next = scope.peekNext();
  if (next.name !== AT) {
    throw new Error('Parser error, PRINT keyword should be followed by AT');
  }
}

My main function runs each of the validation rules all encapsulated inside of a try/catch because I also want to capture additional metadata that will help my user understand what caused the error.

So in my wrapping catch I have something like this:

} catch (error) {
  const message = error.message + `, "${token.text}" at: ${token.pos + 1}`;
  throw new Error(message);
}

This way, when the error is given back to my user, they’ll see:

Parser error, PRINT keyword should be followed by AT, "INK" at: 10

Actually I use this pattern a lot, to catch the source error, interpret it, and throw new Error to help me better understand what was going on.

Except it can be smarter.

Being smart[](#being-smart)

When I call new Error a brand new error object is created. At this point a few things happen, specifically the stack is captured. A stacktrace is incredibly useful for debugging to trace back to the source of the problem.

Except, because I generated a brand new error, my stacktrace will originate from within the catch, which is helpful to a certain degree, but could be a lot more useful.

I could do something with the stack. At times I’ve added a console.log(error.stack) in the catch which I can go searching my logs for.

What I should do is instead of throwing a new Error, I can simply modify the original error.message property (🤦 why did it take me that long).

So now my code looks like this:

} catch (error) {
  error.message += `, "${token.text}" at: ${token.pos + 1}`;
  throw error;
}

My custom error message is passed by to the user (normally me) and my full stacktrace is retained.

Published 24-Sep 2020 under #code. [Edit this post](https://github.com/remy/remysharp.com/blob/main/public/blog/smarter-throwing.md)

👍 14 likes

[![cs](https://webmention.io/avatar/pbs.twimg.com/fe3e0b700309ed70015f0bc89fd9a91b539c0c8988083d1d5c1548b28fb0c03b.jpg "cs")](![Karadjordje(https://webmention.io/avatar/pbs.twimg.com/8122c029906eb5b23954f95278751f9c882cf1b11cee4072f4f93bd8103a6ad3.jpg "Karadjordje")](![btisdall(https://webmention.io/avatar/pbs.twimg.com/7699a2e1917b179210cb4c7ec941557f8f7fd0689579a82ffcf87036cbb49996.jpg "btisdall")](![Ollie Baker(https://webmention.io/avatar/pbs.twimg.com/bb9ef56f61953817438dfe35f15e28474577c842a43be4e05093fe1581fef41c.jpg "Ollie Baker")](![Daniel Szymanek(https://webmention.io/avatar/pbs.twimg.com/5ab392bab465834b7453d50b7f554147bce3d8ceaaeed9cea4006427eaadf36b.jpg "Daniel Szymanek")](![Andy Davies(https://webmention.io/avatar/pbs.twimg.com/a90e45d17c9a935a015f99d326dc9af4063a54eeabcea9348c5775bb27468963.jpg "Andy Davies")](![Adam Taylor(https://webmention.io/avatar/pbs.twimg.com/25e75882b5136fababf7ff2a9f7ed35910c859669565d242faa1f6446d2ec3d1.jpg "Adam Taylor")](![Graeme McCutcheon(https://webmention.io/avatar/pbs.twimg.com/79fc82e93f174ec6558c8d97e24027d6d531019219bce2890baeeae9f8a9f143.jpg "Graeme McCutcheon")](![Bálint Lendvai(https://webmention.io/avatar/pbs.twimg.com/4c917c0734b216c07575b9a4170a382a2e02f7d46e15231ae4e9d99e7d160836.jpg "Bálint Lendvai")](![Tom Turton(https://webmention.io/avatar/pbs.twimg.com/f01b5fa646228bd8f9dd944660761251475c74cdf0c8d15e7d210b1dfba6440c.jpg "Tom Turton")](![Jakub G(https://webmention.io/avatar/pbs.twimg.com/022fe28926eb4af270a34b628e2604e24ef3ceb128db2b50b2f1da09b08b6885.png "Jakub G")](![Prajjwal Singh(https://webmention.io/avatar/pbs.twimg.com/e9b4d4dec7ba154405e6b18b6b2d68cd225795dd1e47c35cb5e277c044c2b89f.jpg "Prajjwal Singh")](![Peter(https://webmention.io/avatar/pbs.twimg.com/89db5e9228272bcc726eaa07816d3ee4be5c503275fd6e12200c4c85d9d2960a.jpg "Peter")](![// const name = 'Rafael'; 🏳️‍🌈(https://webmention.io/avatar/pbs.twimg.com/d8b12c9463f0ab17639b351e6671b4604c35e57504d16577d7003e718ff1507c.jpg "// const name = 'Rafael'; 🏳️‍🌈")](https://twitter.com/racascou)

Comments

Lock Thread

Login

Add Comment[M ↓   Markdown]()

[Commento](https://commento.io)