Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike Gerwitz <mike.gerwitz@rtspecialty.com>2019-10-24 15:10:16 -0400
committerMike Gerwitz <mike.gerwitz@rtspecialty.com>2019-10-29 13:36:55 -0400
commit1ed8ad1cd716531a0ab573b61599a7baf8a96a9a (patch)
treead7618372a769eca1b75e41363a8ed92d3ee21cd
parent1aa69c2a56fe2ad46dfdf10061b9726c18f0e8d8 (diff)
downloadliza-1ed8ad1cd716531a0ab573b61599a7baf8a96a9a.tar.gz
liza-1ed8ad1cd716531a0ab573b61599a7baf8a96a9a.tar.bz2
liza-1ed8ad1cd716531a0ab573b61599a7baf8a96a9a.zip
RatingService: Return promise
This only replaces the callbacks at the highest level and starts to move toward proper error handling. The private methods do not yet properly propagate errors.
-rw-r--r--src/server/daemon/controller.js8
-rw-r--r--src/server/service/RatingService.ts32
-rw-r--r--test/server/service/RatingServiceTest.ts44
3 files changed, 53 insertions, 31 deletions
diff --git a/src/server/daemon/controller.js b/src/server/daemon/controller.js
index c027e4e..0f9cfa3 100644
--- a/src/server/daemon/controller.js
+++ b/src/server/daemon/controller.js
@@ -536,11 +536,9 @@ function doRoute( program, request, data, resolve, reject )
{
var response = UserResponse( request );
- rating_service.request( request, response, quote, alias, function()
- {
- // we're done; free the lock
- free();
- } );
+ rating_service.request( request, response, quote, alias )
+ .catch( () => {} )
+ .then( () => free() );
} );
}, true );
}
diff --git a/src/server/service/RatingService.ts b/src/server/service/RatingService.ts
index e811075..9338758 100644
--- a/src/server/service/RatingService.ts
+++ b/src/server/service/RatingService.ts
@@ -78,24 +78,22 @@ export class RatingService
/**
* Sends rates to the client
*
- * Note that the continuation will be called after all data saving is
+ * Note that the promise will be resolved after all data saving is
* complete; the request will be sent back to the client before then.
*
* @param request - user request to satisfy
* @param _response - pending response
* @param quote - quote to export
* @param cmd - applicable of command request
- * @param callback - continuation after saving is complete
*
- * @return Server self to allow for method chaining
+ * @return result promise
*/
request(
request: UserRequest,
_response: UserResponse,
quote: ServerSideQuote,
cmd: string,
- callback: RequestCallback
- )
+ ): Promise<void>
{
// cmd represents a request for a single rater
if ( !cmd && this._isQuoteValid( quote ) )
@@ -105,23 +103,23 @@ export class RatingService
data: {},
}, [] );
- callback();
- return this;
+ return Promise.resolve();
}
var program = quote.getProgram();
- try
- {
- this._performRating( request, program, quote, cmd, callback );
- }
- catch ( err )
+ return new Promise( ( resolve, reject ) =>
{
- this._sendRatingError( request, quote, program, err );
- callback();
- }
-
- return this;
+ try
+ {
+ this._performRating( request, program, quote, cmd, resolve );
+ }
+ catch ( err )
+ {
+ this._sendRatingError( request, quote, program, err );
+ reject( err );
+ }
+ } );
}
diff --git a/test/server/service/RatingServiceTest.ts b/test/server/service/RatingServiceTest.ts
index c40ea06..c9bb108 100644
--- a/test/server/service/RatingServiceTest.ts
+++ b/test/server/service/RatingServiceTest.ts
@@ -19,7 +19,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-import { expect } from 'chai';
import { RatingService as Sut } from "../../../src/server/service/RatingService";
import { ClientActions } from "../../../src/client/action/ClientAction";
@@ -39,10 +38,13 @@ import {
Callback as ServerDaoCallback
} from "../../../src/server/db/ServerDao";
+import { expect, use as chai_use } from 'chai';
+chai_use( require( 'chai-as-promised' ) );
+
describe( 'RatingService', () =>
{
- it( "saves rate data to own field", done =>
+ it( "saves rate data to own field", () =>
{
const {
logger,
@@ -76,11 +78,35 @@ describe( 'RatingService', () =>
const sut = new Sut( logger, dao, server, raters );
- sut.request( request, response, quote, "", () =>
- {
- expect( saved_rates ).to.be.true;
- done();
- } );
+ return sut.request( request, response, quote, "" )
+ .then( () =>
+ {
+ expect( saved_rates ).to.be.true;
+ } );
+ } );
+
+
+ it( "rejects with error", () =>
+ {
+ const {
+ logger,
+ server,
+ raters,
+ dao,
+ request,
+ response,
+ quote,
+ rater,
+ } = getStubs();
+
+ const expected_error = new Error( "expected error" );
+
+ rater.rate = () => { throw expected_error; };
+
+ const sut = new Sut( logger, dao, server, raters );
+
+ return expect( sut.request( request, response, quote, "" ) )
+ .to.eventually.rejectedWith( expected_error );
} );
@@ -116,7 +142,7 @@ describe( 'RatingService', () =>
}
}( logger, dao, server, raters );
- sut.request( request, response, quote, 'something', () => {} );
+ return sut.request( request, response, quote, 'something' );
} );
it( "calls getLastPremiumDate during #_performRating", done =>
@@ -157,7 +183,7 @@ describe( 'RatingService', () =>
return server;
};
- sut.request( request, response, quote, "", () => {} );
+ return sut.request( request, response, quote, "" );
} );
} );