Skip to content

Feature: message property templating #5

@tracker1

Description

@tracker1

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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions