The method returns new Error instance using current message
This commit is contained in:
Georgi Krustev 2016-09-17 12:51:04 +03:00
Родитель 879a81d574
Коммит 435644448a
2 изменённых файлов: 53 добавлений и 49 удалений

Просмотреть файл

@ -1,40 +1,49 @@
import errorsList from './errors-list'; import errorDetails from './error-details';
const formatRegExp = /\{(\d+)}?\}/g; const formatRegExp = /\{(\d+)}?\}/g;
class IntlError { class IntlError {
_error = null;
name = ""; name = "";
message = "";
constructor(error) { constructor({ name, message }) {
if (!error) { if (!name || !message) {
throw new Error("{ code: string, message: string } object is required!"); throw new Error("{ name: string, message: string } object is required!");
} }
this._error = error; this.name = name;
this.message = message;
this.name = error.name;
}
appendMessage(message) {
this._error.message += message;
} }
formatMessage(...values) { formatMessage(...values) {
const formattedMessage = this._error.message.replace(formatRegExp, function(match, index) { const flattenValues = flatten(values);
return values[parseInt(index, 10)];
const formattedMessage = this.message.replace(formatRegExp, function(match, index) {
return flattenValues[parseInt(index, 10)];
}); });
return `${this._error.name}: ${formattedMessage}`; return `${this.name}: ${formattedMessage}`;
}
error(...values) {
return new Error(this.formatMessage(values));
} }
} }
const predicate = function(prev, current) { const flatten = function(arr) {
prev[current.name] = new IntlError(current); return arr.reduce((a, b) => a.concat(b), []);
return prev;
}; };
const errors = errorsList.reduce(predicate, {}); const mapErrors = function(errors) {
const predicate = function(prev, name) {
prev[name] = new IntlError({ name, message: errors[name] });
return prev;
};
return Object.keys(errors).reduce(predicate, {});
};
const errors = mapErrors(errorDetails);
export { export {
errors, errors,

Просмотреть файл

@ -1,10 +1,9 @@
import { errors, IntlError } from '../src/errors'; import { errors, IntlError } from '../src/errors';
describe('IntlError', () => { describe('Intl', () => {
it('gets message', () => { it('gets message', () => {
const error = { const error = {
code: "001", name: "Big",
name: "BigError",
message: "Big error!" message: "Big error!"
}; };
@ -17,8 +16,7 @@ describe('IntlError', () => {
it('should replace single placeholder in the message', () => { it('should replace single placeholder in the message', () => {
const error = { const error = {
code: "001", name: "Big",
name: "BigError",
message: "Big error! {0} {1}" message: "Big error! {0} {1}"
}; };
@ -29,54 +27,51 @@ describe('IntlError', () => {
expect(intlError.formatMessage("p1", "p2"), result); expect(intlError.formatMessage("p1", "p2"), result);
}); });
it('should append new message to current one', () => { it('should return instance with message', () => {
const error = { const error = {
code: "001", name: "Big",
name: "BigError", message: "Big error! {0} {1}"
message: "Big error! {0}"
}; };
const toAppend = "appended";
const intlError = new IntlError(error); const intlError = new IntlError(error);
const result = `${error.name}: Big error! {0} ${toAppend}`; const expectedMessage = `${error.name}: Big error! p1 p2`;
intlError.appendMessage(toAppend); expect(intlError.error("p1", "p2") instanceof Error).toBe(true);
expect(intlError.error("p1", "p2").message).toBe(expectedMessage);
expect(intlError.formatMessage(), result);
}); });
}); });
describe('default errors', () => { describe('default errors', () => {
it('should have NoLocaleError type', () => { it('should have NoLocale type', () => {
expect(errors.NoLocaleError.formatMessage()).toMatch(/NoLocaleError/); expect(errors.NoLocale.formatMessage()).toMatch(/NoLocale/);
}); });
it('should have NoCurrencyError type', () => { it('should have NoCurrency type', () => {
expect(errors.NoCurrencyError.formatMessage()).toMatch(/NoCurrencyError/); expect(errors.NoCurrency.formatMessage()).toMatch(/NoCurrency/);
}); });
it('should have NoSupplementalCurrencyError type', () => { it('should have NoSupplementalCurrency type', () => {
expect(errors.NoSupplementalCurrencyError.formatMessage()).toMatch(/NoSupplementalCurrencyError/); expect(errors.NoSupplementalCurrency.formatMessage()).toMatch(/NoSupplementalCurrency/);
}); });
it('should have NoCurrencyRegionError type', () => { it('should have NoCurrencyRegion type', () => {
expect(errors.NoCurrencyRegionError.formatMessage()).toMatch(/NoCurrencyRegionError/); expect(errors.NoCurrencyRegion.formatMessage()).toMatch(/NoCurrencyRegion/);
}); });
it('should have NoCurrencyDisplayError type', () => { it('should have NoCurrencyDisplay type', () => {
expect(errors.NoCurrencyDisplayError.formatMessage()).toMatch(/NoCurrencyDisplayError/); expect(errors.NoCurrencyDisplay.formatMessage()).toMatch(/NoCurrencyDisplay/);
}); });
it('should have NoGMTInfoError type', () => { it('should have NoGMTInfo type', () => {
expect(errors.NoGMTInfoError.formatMessage()).toMatch(/NoGMTInfoError/); expect(errors.NoGMTInfo.formatMessage()).toMatch(/NoGMTInfo/);
}); });
it('should have NoWeekDataError type', () => { it('should have NoWeekData type', () => {
expect(errors.NoWeekDataError.formatMessage()).toMatch(/NoWeekDataError/); expect(errors.NoWeekData.formatMessage()).toMatch(/NoWeekData/);
}); });
it('should have NoFirstDayError type', () => { it('should have NoFirstDay type', () => {
expect(errors.NoFirstDayError.formatMessage()).toMatch(/NoFirstDayError/); expect(errors.NoFirstDay.formatMessage()).toMatch(/NoFirstDay/);
}); });
}); });