TokenDao: Add error context
parent
fe10578949
commit
4454e40e19
|
@ -33,6 +33,7 @@ import {
|
||||||
import { DocumentId } from "../../document/Document";
|
import { DocumentId } from "../../document/Document";
|
||||||
import { TokenId, TokenNamespace } from "./Token";
|
import { TokenId, TokenNamespace } from "./Token";
|
||||||
import { UnknownTokenError } from "./UnknownTokenError";
|
import { UnknownTokenError } from "./UnknownTokenError";
|
||||||
|
import { context } from "../../error/ContextError";
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -188,8 +189,14 @@ export class MongoTokenDao implements TokenDao
|
||||||
|
|
||||||
if ( !field[ ns ] )
|
if ( !field[ ns ] )
|
||||||
{
|
{
|
||||||
reject( new UnknownTokenError(
|
reject( context(
|
||||||
`Unknown token namespace '${ns}' for document '${doc_id}`
|
new UnknownTokenError(
|
||||||
|
`Unknown token namespace '${ns}' for document '${doc_id}`
|
||||||
|
),
|
||||||
|
{
|
||||||
|
doc_id: doc_id,
|
||||||
|
ns: ns,
|
||||||
|
}
|
||||||
) );
|
) );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -227,9 +234,15 @@ export class MongoTokenDao implements TokenDao
|
||||||
|
|
||||||
if ( !last )
|
if ( !last )
|
||||||
{
|
{
|
||||||
throw new UnknownTokenError(
|
throw context(
|
||||||
`Failed to locate last token for namespace '${ns}'` +
|
new UnknownTokenError(
|
||||||
`on document '${doc_id}'`
|
`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 )
|
if ( !reqtok )
|
||||||
{
|
{
|
||||||
throw new UnknownTokenError(
|
throw context(
|
||||||
`Missing data for requested token '${ns}.${token_id}'` +
|
new UnknownTokenError(
|
||||||
`for document '${doc_id}'`
|
`Missing data for requested token '${ns}.${token_id}'` +
|
||||||
|
`for document '${doc_id}'`
|
||||||
|
),
|
||||||
|
{
|
||||||
|
doc_id: doc_id,
|
||||||
|
ns: ns,
|
||||||
|
token_id: token_id,
|
||||||
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -34,6 +34,7 @@ import {
|
||||||
|
|
||||||
import { DocumentId } from "../../../src/document/Document";
|
import { DocumentId } from "../../../src/document/Document";
|
||||||
import { UnknownTokenError } from "../../../src/server/token/UnknownTokenError";
|
import { UnknownTokenError } from "../../../src/server/token/UnknownTokenError";
|
||||||
|
import { hasContext } from "../../../src/error/ContextError";
|
||||||
|
|
||||||
|
|
||||||
import { expect, use as chai_use } from 'chai';
|
import { expect, use as chai_use } from 'chai';
|
||||||
|
@ -130,7 +131,7 @@ describe( 'server.token.TokenDao', () =>
|
||||||
data: "",
|
data: "",
|
||||||
};
|
};
|
||||||
|
|
||||||
( <[string, TokenId, TokenQueryResult, TokenData|null, any][]>[
|
( <[string, TokenId, TokenQueryResult, TokenData|null, any, any][]>[
|
||||||
[
|
[
|
||||||
'retrieves token by id',
|
'retrieves token by id',
|
||||||
<TokenId>'tok123',
|
<TokenId>'tok123',
|
||||||
|
@ -152,6 +153,7 @@ describe( 'server.token.TokenDao', () =>
|
||||||
status: expected_status,
|
status: expected_status,
|
||||||
},
|
},
|
||||||
null,
|
null,
|
||||||
|
null,
|
||||||
],
|
],
|
||||||
|
|
||||||
[
|
[
|
||||||
|
@ -173,6 +175,11 @@ describe( 'server.token.TokenDao', () =>
|
||||||
},
|
},
|
||||||
null,
|
null,
|
||||||
`${ns}.tok123`,
|
`${ns}.tok123`,
|
||||||
|
{
|
||||||
|
doc_id: did,
|
||||||
|
ns: ns,
|
||||||
|
token_id: 'tok123',
|
||||||
|
},
|
||||||
],
|
],
|
||||||
|
|
||||||
[
|
[
|
||||||
|
@ -183,6 +190,10 @@ describe( 'server.token.TokenDao', () =>
|
||||||
},
|
},
|
||||||
null,
|
null,
|
||||||
ns,
|
ns,
|
||||||
|
{
|
||||||
|
doc_id: did,
|
||||||
|
ns: ns,
|
||||||
|
},
|
||||||
],
|
],
|
||||||
|
|
||||||
[
|
[
|
||||||
|
@ -206,6 +217,7 @@ describe( 'server.token.TokenDao', () =>
|
||||||
status: expected_status,
|
status: expected_status,
|
||||||
},
|
},
|
||||||
null,
|
null,
|
||||||
|
null,
|
||||||
],
|
],
|
||||||
|
|
||||||
[
|
[
|
||||||
|
@ -218,6 +230,10 @@ describe( 'server.token.TokenDao', () =>
|
||||||
},
|
},
|
||||||
null,
|
null,
|
||||||
ns,
|
ns,
|
||||||
|
{
|
||||||
|
doc_id: did,
|
||||||
|
ns: ns,
|
||||||
|
},
|
||||||
],
|
],
|
||||||
|
|
||||||
[
|
[
|
||||||
|
@ -228,8 +244,12 @@ describe( 'server.token.TokenDao', () =>
|
||||||
},
|
},
|
||||||
null,
|
null,
|
||||||
ns,
|
ns,
|
||||||
|
{
|
||||||
|
doc_id: did,
|
||||||
|
ns: ns,
|
||||||
|
},
|
||||||
],
|
],
|
||||||
] ).forEach( ( [ label, tok_id, dbresult, expected, failure ] ) =>
|
] ).forEach( ( [ label, tok_id, dbresult, expected, fmsg, fcontext ] ) =>
|
||||||
it( label, () =>
|
it( label, () =>
|
||||||
{
|
{
|
||||||
const coll: MongoCollection = {
|
const coll: MongoCollection = {
|
||||||
|
@ -244,14 +264,26 @@ describe( 'server.token.TokenDao', () =>
|
||||||
const result = new Sut( coll, field, () => <UnixTimestamp>0 )
|
const result = new Sut( coll, field, () => <UnixTimestamp>0 )
|
||||||
.getToken( did, ns, tok_id );
|
.getToken( did, ns, tok_id );
|
||||||
|
|
||||||
return ( failure !== null )
|
return ( fmsg !== null )
|
||||||
? Promise.all( [
|
? Promise.all( [
|
||||||
expect( result ).to.eventually.be.rejectedWith(
|
expect( result ).to.eventually.be.rejectedWith(
|
||||||
UnknownTokenError, failure
|
UnknownTokenError, fmsg
|
||||||
),
|
),
|
||||||
expect( result ).to.eventually.be.rejectedWith(
|
expect( result ).to.eventually.be.rejectedWith(
|
||||||
UnknownTokenError, ''+did
|
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 );
|
: expect( result ).to.eventually.deep.equal( expected );
|
||||||
} )
|
} )
|
||||||
|
|
Loading…
Reference in New Issue