errors icon indicating copy to clipboard operation
errors copied to clipboard

Feature: message property templating

Open tracker1 opened this issue 11 years ago • 3 comments

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 avatar Jun 26 '14 20:06 tracker1

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

tracker1 avatar Jun 26 '14 20:06 tracker1

@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

bodenr avatar Jul 02 '14 10:07 bodenr

@bodenr Does this enhancement added in the release?

mingjinc avatar Jul 13 '17 02:07 mingjinc