Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: message property templating #5

Open
tracker1 opened this issue Jun 26, 2014 · 3 comments
Open

Feature: message property templating #5

tracker1 opened this issue Jun 26, 2014 · 3 comments

Comments

@tracker1
Copy link

It would be nice to have the message string as a template against the other properties of the object... (suggest ES6 string template format, lodash has an implementation)

example:

errors.create({
  name: 'DocumentNotFound',
  message: 'The document "${document}" was not found.'
});

console.log(new errors.DocumentNotFound({ document: 'path/to/document.ext' }))

would output:

code: 6xx
name: 'DocumentNotFound'
message: 'The document "path/to/document.ext" was not found'

Idea burrowed from error/typed

//using lodash's _.template
  ...
  if ((/\$\{[^\}]+\}/).test(this.message)) {
    this.message = _.template(this.message, this);
  }
} //end of constructor method

Making lodash a dependency without a version specified to allow for npm dedupe in a hosting package to always work, with a very slim risk of an incompatible version.

@tracker1
Copy link
Author

I'd add it myself, but the make/test setup doesn't work for me in windows (I'm at work).

@bodenr
Copy link
Owner

bodenr commented Jul 2, 2014

@tracker1 - I will try to take a look at this in the next week (as time permits).

Obviously if you get time for PR - that's appreciated, but please include test coverage.

Thanks

@mingjinc
Copy link

@bodenr Does this enhancement added in the release?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants