I'm working on a NodeJS application right now, and I'm somewhat stuck on 
the "best" way to manage exceptions that can be returned (or thrown) by the 

The two things I would like to accomplish are:

Localization - Instead of the exceptions being hardcoded in English, would 
be nice if the messages and details were stored in files, so they can 
easily be switched out to change the language (since thats how the l10n in 
the rest of the application will work as well)
As opposed to having the exception messages hardcoded and spread out 
everywhere, I'm hoping to have it setup so all of the exceptions are stored 
in one place, and when an exception needs to be thrown, I can simply 
reference the specific exception by its "key" or "code", and provide any 
details that can be used to fill in some blanks in the exception messages.
I have a basic version of this setup in the application right now, and I 
copied the relevant parts over to a temporary Github repo 
<https://github.com/jhyland87/sasset-exception-poc>, just as an example of 
what I mean.

Basically, you can see I have the details that are returned by the 
exceptions stored in a series of files (like this one 
and when an exception needs to be thrown, I can reference the key of 
whatever exception needs to be thrown (EG: *account.login.**badPassword* would 
be used when someone tries to authenticate with an incorrect password), and 
provide any data that the exception will need to display.

Heres an example of how to throw one of the exceptions, and how data is 
provided for string substitution in the exception details (this is located 
in the app.js 

const AppError = require( "./exceptions" ).init

try {
    throw new AppError({
        code: 'account.login.badUsername',
        data: 'j.doe'
catch( e ){
    console.log('%s - %s',e.name, e.message)

So the above example throws an exception using the code 
*account.login.badUsername*, and it provides the string *j.doe* in the data 
property. So looking at the exceptions stored in the account.js 
file again, you can see the detail property is The username provided (%s) 
was not found., so when the exception gets thrown, the completed exception 
will look like: *The username provided (j.doe) was not found*.

The logic for retrieving the exception data via the exception code 
provided, (as well as the string substitution in the exception details,) 
can be found in the file.

Down to the question....

Is this the "best" way to accomplish what I'm trying to accomplish? or is 
there another way to go about doing this that may be easier and cleaner? So 
far, it seems to be working ok, I'm just afraid that ill finish the first 
version of this app, and someone will be like "Oh why are you managing 
exceptions like that? why didn't you just __________??".

Any input is appreciated!


P.S. I apologize if this ends up being a duplicate post. I thought I posted 
the question here, but that was a few days ago, and it hasnt shown up.

Job board: http://jobs.nodejs.org/
New group rules: 
Old group rules: 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nodejs+unsubscr...@googlegroups.com.
To post to this group, send email to nodejs@googlegroups.com.
To view this discussion on the web visit 
For more options, visit https://groups.google.com/d/optout.

Reply via email to