From 0c18a6321a617365f079d64bb56888e2d98ff145 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Fri, 30 Dec 2016 15:38:21 -0500 Subject: [PATCH] Add StoreMissError Nice and trivial with the new easejs transparent error subtyping! * src/store/StoreMissError.js: Add error class. * src/store/MemoryStore.js (get): Use it. * test/store/MemoryStoreTest.js (#get): Modify test to expect type. --- src/store/MemoryStore.js | 9 ++++++--- src/store/StoreMissError.js | 31 +++++++++++++++++++++++++++++++ test/store/MemoryStoreTest.js | 3 ++- 3 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 src/store/StoreMissError.js diff --git a/src/store/MemoryStore.js b/src/store/MemoryStore.js index 52786f4..d4b92da 100644 --- a/src/store/MemoryStore.js +++ b/src/store/MemoryStore.js @@ -19,8 +19,9 @@ * along with this program. If not, see . */ -var Class = require( 'easejs' ).Class, - Store = require( './Store' ); +var Class = require( 'easejs' ).Class, + Store = require( './Store' ), + StoreMissError = require( './StoreMissError' ); /** @@ -107,7 +108,9 @@ module.exports = Class( 'MemoryStore' ) { return ( this._store[ key ] !== undefined ) ? Promise.resolve( this._store[ key ] ) - : Promise.reject( 'Key ' + key + ' does not exist' ); + : Promise.reject( + StoreMissError( "Key '" + key + "' does not exist" ) + ); }, diff --git a/src/store/StoreMissError.js b/src/store/StoreMissError.js new file mode 100644 index 0000000..8017fdb --- /dev/null +++ b/src/store/StoreMissError.js @@ -0,0 +1,31 @@ +/** + * Error when key is not found in store + * + * Copyright (C) 2016 LoVullo Associates, Inc. + * + * This file is part of the Liza Data Collection Framework + * + * Liza is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +var Class = require( 'easejs' ).Class; + + +/** + * Store key miss + * + * A key was not found in a store. + */ +module.exports = Class( 'StoreMissError' ) + .extend( ReferenceError, {} ); diff --git a/test/store/MemoryStoreTest.js b/test/store/MemoryStoreTest.js index 82392af..7d47bc2 100644 --- a/test/store/MemoryStoreTest.js +++ b/test/store/MemoryStoreTest.js @@ -78,7 +78,8 @@ describe( 'store.MemoryStore', () => it( 'rejects promise if store item does not exist', () => { return expect( Sut().get( 'unknown' ) ) - .to.eventually.be.rejected; + .to.eventually.be.rejected + .and.be.instanceof( store.StoreMissError ); } ); } );