1
0
Fork 0

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.
master
Mike Gerwitz 2019-10-24 15:10:16 -04:00
parent 1aa69c2a56
commit 1ed8ad1cd7
3 changed files with 53 additions and 31 deletions

View File

@ -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 );
}

View File

@ -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
return new Promise( ( resolve, reject ) =>
{
this._performRating( request, program, quote, cmd, callback );
}
catch ( err )
{
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 );
}
} );
}

View File

@ -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, "" );
} );
} );