Added proper number formatting for error messages

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

- We should always format numbers correctly with thousand separators when we're displaying them to users
This commit is contained in:
Hannah Wolfe 2021-03-04 18:08:25 +00:00
parent abac3a5eed
commit 47478eb1f9
3 changed files with 34 additions and 15 deletions

View File

@ -42,19 +42,22 @@ class MaxLimit extends Limit {
generateError(count) { generateError(count) {
let errorObj = super.generateError(); let errorObj = super.generateError();
let max = this.max;
errorObj.message = this.fallbackMessage; errorObj.message = this.fallbackMessage;
if (this.error) { if (this.error) {
try { try {
errorObj.message = _.template(this.error)({max, count}); errorObj.message = _.template(this.error)(
{
max: Intl.NumberFormat().format(this.max),
count: Intl.NumberFormat().format(count)
});
} catch (e) { } catch (e) {
errorObj.message = this.fallbackMessage; errorObj.message = this.fallbackMessage;
} }
} }
errorObj.errorDetails.limit = max; errorObj.errorDetails.limit = this.max;
errorObj.errorDetails.total = count; errorObj.errorDetails.total = count;
return new errors.HostLimitError(errorObj); return new errors.HostLimitError(errorObj);

View File

@ -0,0 +1,28 @@
// Switch these lines once there are useful utils
// const testUtils = require('./utils');
require('./utils');
describe('Limit Service', function () {
describe('Lodash Template', function () {
it('Does not get clobbered by this lib', function () {
require('../lib/limit');
let _ = require('lodash');
_.templateSettings.interpolate.should.eql(/<%=([\s\S]+?)%>/g);
});
});
describe('Error Messages', function () {
it('Formats numbers correctly', function () {
const {MaxLimit} = require('../lib/limit');
let limit = new MaxLimit({name: 'test', config: {max: 35000000, currentCountQuery: () => {}, error: 'Your plan supports up to {{max}} staff users. Please upgrade to add more.'}});
let error = limit.generateError(35000001);
error.message.should.eql('Your plan supports up to 35,000,000 staff users. Please upgrade to add more.');
error.errorDetails.limit.should.eql(35000000);
error.errorDetails.total.should.eql(35000001);
});
});
});

View File

@ -1,12 +0,0 @@
// Switch these lines once there are useful utils
// const testUtils = require('./utils');
require('./utils');
describe('Lodash Template', function () {
it('Does not get clobbered by this lib', function () {
require('../lib/limit');
let _ = require('lodash');
_.templateSettings.interpolate.should.eql(/<%=([\s\S]+?)%>/g);
});
});