Today I Learned - Rocky Kev

TIL not ignoring errors

POSTED ON:

TAGS:

I'm guilty of this.

Don't ignore caught errors

Doing nothing with a caught error doesn't give you the ability to ever fix or react to said error. Logging the error to the console (console.log) isn't much better as often times it can get lost in a sea of things printed to the console. If you wrap any bit of code in a try/catch it means you think an error may occur there and therefore you should have a plan, or create a code path, for when it occurs.

Bad:

try {
  functionThatMightThrow();
} catch (error) {
  console.log(error);
}

Good:

try {
  functionThatMightThrow();
} catch (error) {
  // One option (more noisy than console.log):
  console.error(error);
  // Another option:
  notifyUserOfError(error);
  // Another option:
  reportErrorToService(error);
  // OR do all three!
}

Via JS Clean Code


Related TILs

Tagged:

TIL 404 errors actually have more specific causes

IIS 7.0 and later versions define the following HTTP status codes that indicate a more specific cause of an error 404, like 404.1 - Site Not Found & 404.2 - ISAPI or CGI restriction.

TIL that 2^24 items will cause a interesting error

Obviously there's not much real use-cases. But that's the fun of finding limitations.

TIL not ignoring errors

Doing nothing with a caught error doesn't give you the ability to ever fix or react to said error.