diff --git a/src/server/token/MongoTokenDao.ts b/src/server/token/MongoTokenDao.ts index 539f43a..66c3c96 100644 --- a/src/server/token/MongoTokenDao.ts +++ b/src/server/token/MongoTokenDao.ts @@ -33,6 +33,7 @@ import { import { DocumentId } from "../../document/Document"; import { TokenId, TokenNamespace } from "./Token"; import { UnknownTokenError } from "./UnknownTokenError"; +import { context } from "../../error/ContextError"; /** @@ -188,8 +189,14 @@ export class MongoTokenDao implements TokenDao if ( !field[ ns ] ) { - reject( new UnknownTokenError( - `Unknown token namespace '${ns}' for document '${doc_id}` + reject( context( + new UnknownTokenError( + `Unknown token namespace '${ns}' for document '${doc_id}` + ), + { + doc_id: doc_id, + ns: ns, + } ) ); return; } @@ -227,9 +234,15 @@ export class MongoTokenDao implements TokenDao if ( !last ) { - throw new UnknownTokenError( - `Failed to locate last token for namespace '${ns}'` + - `on document '${doc_id}'` + throw context( + new UnknownTokenError( + `Failed to locate last token for namespace '${ns}'` + + `on document '${doc_id}'` + ), + { + doc_id: doc_id, + ns: ns, + }, ); } @@ -263,9 +276,16 @@ export class MongoTokenDao implements TokenDao if ( !reqtok ) { - throw new UnknownTokenError( - `Missing data for requested token '${ns}.${token_id}'` + - `for document '${doc_id}'` + throw context( + new UnknownTokenError( + `Missing data for requested token '${ns}.${token_id}'` + + `for document '${doc_id}'` + ), + { + doc_id: doc_id, + ns: ns, + token_id: token_id, + }, ); } diff --git a/test/server/token/MongoTokenDaoTest.ts b/test/server/token/MongoTokenDaoTest.ts index 47b1368..8fe0dff 100644 --- a/test/server/token/MongoTokenDaoTest.ts +++ b/test/server/token/MongoTokenDaoTest.ts @@ -34,6 +34,7 @@ import { import { DocumentId } from "../../../src/document/Document"; import { UnknownTokenError } from "../../../src/server/token/UnknownTokenError"; +import { hasContext } from "../../../src/error/ContextError"; import { expect, use as chai_use } from 'chai'; @@ -130,7 +131,7 @@ describe( 'server.token.TokenDao', () => data: "", }; - ( <[string, TokenId, TokenQueryResult, TokenData|null, any][]>[ + ( <[string, TokenId, TokenQueryResult, TokenData|null, any, any][]>[ [ 'retrieves token by id', 'tok123', @@ -152,6 +153,7 @@ describe( 'server.token.TokenDao', () => status: expected_status, }, null, + null, ], [ @@ -173,6 +175,11 @@ describe( 'server.token.TokenDao', () => }, null, `${ns}.tok123`, + { + doc_id: did, + ns: ns, + token_id: 'tok123', + }, ], [ @@ -183,6 +190,10 @@ describe( 'server.token.TokenDao', () => }, null, ns, + { + doc_id: did, + ns: ns, + }, ], [ @@ -206,6 +217,7 @@ describe( 'server.token.TokenDao', () => status: expected_status, }, null, + null, ], [ @@ -218,6 +230,10 @@ describe( 'server.token.TokenDao', () => }, null, ns, + { + doc_id: did, + ns: ns, + }, ], [ @@ -228,8 +244,12 @@ describe( 'server.token.TokenDao', () => }, null, ns, + { + doc_id: did, + ns: ns, + }, ], - ] ).forEach( ( [ label, tok_id, dbresult, expected, failure ] ) => + ] ).forEach( ( [ label, tok_id, dbresult, expected, fmsg, fcontext ] ) => it( label, () => { const coll: MongoCollection = { @@ -244,14 +264,26 @@ describe( 'server.token.TokenDao', () => const result = new Sut( coll, field, () => 0 ) .getToken( did, ns, tok_id ); - return ( failure !== null ) + return ( fmsg !== null ) ? Promise.all( [ expect( result ).to.eventually.be.rejectedWith( - UnknownTokenError, failure + UnknownTokenError, fmsg ), expect( result ).to.eventually.be.rejectedWith( UnknownTokenError, ''+did ), + result.catch( e => + { + if ( !hasContext( e ) ) + { + // TS will soon have type assertions and + // then this conditional and return can be + // removed + return expect.fail(); + } + + return expect( e.context ).to.deep.equal( fcontext ); + } ), ] ) : expect( result ).to.eventually.deep.equal( expected ); } )