Exposed additional "name" variable in error templates

refs https://github.com/TryGhost/Team/issues/587

- There was a need to be able to use the "name" of the limit inside of error templates like so: `{{name}}` (reference https://github.com/TryGhost/Team/issues/587#issuecomment-814281794)
- This change allows to form custom error messages using following variable: `{{name}}` which is the same as the `name` property provided in the configuration for the limit
This commit is contained in:
Naz 2021-05-12 14:43:34 +04:00
parent 2b46145f88
commit 88f0c332b4
2 changed files with 9 additions and 6 deletions

View File

@ -66,7 +66,8 @@ class MaxLimit extends Limit {
errorObj.message = _.template(this.error)(
{
max: Intl.NumberFormat().format(this.max),
count: Intl.NumberFormat().format(count)
count: Intl.NumberFormat().format(count),
name: this.name
});
} catch (e) {
errorObj.message = this.fallbackMessage;
@ -166,7 +167,8 @@ class MaxPeriodicLimit extends Limit {
errorObj.message = _.template(this.error)(
{
max: Intl.NumberFormat().format(this.maxPeriodic),
count: Intl.NumberFormat().format(count)
count: Intl.NumberFormat().format(count),
name: this.name
});
} catch (e) {
errorObj.message = this.fallbackMessage;

View File

@ -36,20 +36,21 @@ describe('Limit Service', function () {
error.errorDetails.total.should.eql(35000001);
});
it('Supports {{max}} and {{count}} variables', function () {
it('Supports {{max}}, {{count}}, and {{name}} variables', function () {
let limit = new MaxLimit({
name: 'test',
name: 'Test Resources',
config: {
max: 5,
currentCountQuery: () => {},
error: 'Your plan supports up to {{max}} staff users. You are currently at {{count}} staff users.Please upgrade to add more.'
error: '{{name}} limit reached. Your plan supports up to {{max}} staff users. You are currently at {{count}} staff users.Please upgrade to add more.'
},
errors
});
let error = limit.generateError(7);
error.message.should.eql('Your plan supports up to 5 staff users. You are currently at 7 staff users.Please upgrade to add more.');
error.message.should.eql('Test Resources limit reached. Your plan supports up to 5 staff users. You are currently at 7 staff users.Please upgrade to add more.');
error.errorDetails.name.should.eql('Test Resources');
error.errorDetails.limit.should.eql(5);
error.errorDetails.total.should.eql(7);
});