From 435644448a33addb5d743a895be0a0af3e760815 Mon Sep 17 00:00:00 2001 From: Georgi Krustev Date: Sat, 17 Sep 2016 12:51:04 +0300 Subject: [PATCH] feat: IntlError error method The method returns new Error instance using current message --- src/errors.js | 47 +++++++++++++++++++++++++----------------- test/errors.js | 55 +++++++++++++++++++++++--------------------------- 2 files changed, 53 insertions(+), 49 deletions(-) diff --git a/src/errors.js b/src/errors.js index 6db764e1..5e0ad2b6 100644 --- a/src/errors.js +++ b/src/errors.js @@ -1,40 +1,49 @@ -import errorsList from './errors-list'; +import errorDetails from './error-details'; const formatRegExp = /\{(\d+)}?\}/g; class IntlError { - _error = null; name = ""; + message = ""; - constructor(error) { - if (!error) { - throw new Error("{ code: string, message: string } object is required!"); + constructor({ name, message }) { + if (!name || !message) { + throw new Error("{ name: string, message: string } object is required!"); } - this._error = error; - - this.name = error.name; - } - - appendMessage(message) { - this._error.message += message; + this.name = name; + this.message = message; } formatMessage(...values) { - const formattedMessage = this._error.message.replace(formatRegExp, function(match, index) { - return values[parseInt(index, 10)]; + const flattenValues = flatten(values); + + 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) { - prev[current.name] = new IntlError(current); - return prev; +const flatten = function(arr) { + return arr.reduce((a, b) => a.concat(b), []); }; -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 { errors, diff --git a/test/errors.js b/test/errors.js index 5b717a81..44aa5670 100644 --- a/test/errors.js +++ b/test/errors.js @@ -1,10 +1,9 @@ import { errors, IntlError } from '../src/errors'; -describe('IntlError', () => { +describe('Intl', () => { it('gets message', () => { const error = { - code: "001", - name: "BigError", + name: "Big", message: "Big error!" }; @@ -17,8 +16,7 @@ describe('IntlError', () => { it('should replace single placeholder in the message', () => { const error = { - code: "001", - name: "BigError", + name: "Big", message: "Big error! {0} {1}" }; @@ -29,54 +27,51 @@ describe('IntlError', () => { expect(intlError.formatMessage("p1", "p2"), result); }); - it('should append new message to current one', () => { + it('should return instance with message', () => { const error = { - code: "001", - name: "BigError", - message: "Big error! {0}" + name: "Big", + message: "Big error! {0} {1}" }; - const toAppend = "appended"; 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.formatMessage(), result); + expect(intlError.error("p1", "p2") instanceof Error).toBe(true); + expect(intlError.error("p1", "p2").message).toBe(expectedMessage); }); }); describe('default errors', () => { - it('should have NoLocaleError type', () => { - expect(errors.NoLocaleError.formatMessage()).toMatch(/NoLocaleError/); + it('should have NoLocale type', () => { + expect(errors.NoLocale.formatMessage()).toMatch(/NoLocale/); }); - it('should have NoCurrencyError type', () => { - expect(errors.NoCurrencyError.formatMessage()).toMatch(/NoCurrencyError/); + it('should have NoCurrency type', () => { + expect(errors.NoCurrency.formatMessage()).toMatch(/NoCurrency/); }); - it('should have NoSupplementalCurrencyError type', () => { - expect(errors.NoSupplementalCurrencyError.formatMessage()).toMatch(/NoSupplementalCurrencyError/); + it('should have NoSupplementalCurrency type', () => { + expect(errors.NoSupplementalCurrency.formatMessage()).toMatch(/NoSupplementalCurrency/); }); - it('should have NoCurrencyRegionError type', () => { - expect(errors.NoCurrencyRegionError.formatMessage()).toMatch(/NoCurrencyRegionError/); + it('should have NoCurrencyRegion type', () => { + expect(errors.NoCurrencyRegion.formatMessage()).toMatch(/NoCurrencyRegion/); }); - it('should have NoCurrencyDisplayError type', () => { - expect(errors.NoCurrencyDisplayError.formatMessage()).toMatch(/NoCurrencyDisplayError/); + it('should have NoCurrencyDisplay type', () => { + expect(errors.NoCurrencyDisplay.formatMessage()).toMatch(/NoCurrencyDisplay/); }); - it('should have NoGMTInfoError type', () => { - expect(errors.NoGMTInfoError.formatMessage()).toMatch(/NoGMTInfoError/); + it('should have NoGMTInfo type', () => { + expect(errors.NoGMTInfo.formatMessage()).toMatch(/NoGMTInfo/); }); - it('should have NoWeekDataError type', () => { - expect(errors.NoWeekDataError.formatMessage()).toMatch(/NoWeekDataError/); + it('should have NoWeekData type', () => { + expect(errors.NoWeekData.formatMessage()).toMatch(/NoWeekData/); }); - it('should have NoFirstDayError type', () => { - expect(errors.NoFirstDayError.formatMessage()).toMatch(/NoFirstDayError/); + it('should have NoFirstDay type', () => { + expect(errors.NoFirstDay.formatMessage()).toMatch(/NoFirstDay/); }); });