`WakuStore.queryHistory` throws when encountering an error

Instead of returning a `null` value.
This commit is contained in:
Franck Royer 2021-08-04 12:35:47 +10:00
parent b422c9a10b
commit 319f44a0b1
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
3 changed files with 83 additions and 97 deletions

View File

@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed ### Changed
- **Breaking**: The `WakuMessage` APIs have been changed to move `contentTopic` out of the optional parameters. - **Breaking**: The `WakuMessage` APIs have been changed to move `contentTopic` out of the optional parameters.
- **Breaking**: Move `contentTopics` out the `WakuStore.queryHistory`'s optional parameters. - **Breaking**: Move `contentTopics` out the `WakuStore.queryHistory`'s optional parameters.
- **Breaking**: `WakuStore.queryHistory` throws when encountering an error instead of returning a `null` value.
### Removed ### Removed
- Examples (web-chat): Remove broken `/fleet` command. - Examples (web-chat): Remove broken `/fleet` command.

View File

@ -62,13 +62,18 @@ async function retrieveStoreMessages(
setArchivedMessages(messages); setArchivedMessages(messages);
}; };
try {
const res = await waku.store.queryHistory([ChatContentTopic], { const res = await waku.store.queryHistory([ChatContentTopic], {
pageSize: 5, pageSize: 5,
direction: Direction.FORWARD, direction: Direction.FORWARD,
callback, callback,
}); });
return res ? res.length : 0; return res.length;
} catch {
console.log('Failed to retrieve messages');
return 0;
}
} }
export default function App() { export default function App() {

View File

@ -67,12 +67,12 @@ export class WakuStore {
* @param options.decryptionKeys Keys that will be used to decrypt messages. * @param options.decryptionKeys Keys that will be used to decrypt messages.
* It can be Asymmetric Private Keys and Symmetric Keys in the same array, all keys will be tried with both * It can be Asymmetric Private Keys and Symmetric Keys in the same array, all keys will be tried with both
* methods. * methods.
* @throws If not able to reach the peer to query. * @throws If not able to reach the peer to query or error when processing the reply.
*/ */
async queryHistory( async queryHistory(
contentTopics: string[], contentTopics: string[],
options?: QueryOptions options?: QueryOptions
): Promise<WakuMessage[] | null> { ): Promise<WakuMessage[]> {
const opts = Object.assign( const opts = Object.assign(
{ {
pubsubTopic: this.pubsubTopic, pubsubTopic: this.pubsubTopic,
@ -100,9 +100,7 @@ export class WakuStore {
const messages: WakuMessage[] = []; const messages: WakuMessage[] = [];
let cursor = undefined; let cursor = undefined;
while (true) { while (true) {
try {
const { stream } = await connection.newStream(StoreCodec); const { stream } = await connection.newStream(StoreCodec);
try {
const queryOpts = Object.assign(opts, { cursor }); const queryOpts = Object.assign(opts, { cursor });
const historyRpcQuery = HistoryRPC.createQuery(queryOpts); const historyRpcQuery = HistoryRPC.createQuery(queryOpts);
const res = await pipe( const res = await pipe(
@ -112,21 +110,18 @@ export class WakuStore {
lp.decode(), lp.decode(),
concat concat
); );
try {
const reply = HistoryRPC.decode(res.slice()); const reply = HistoryRPC.decode(res.slice());
const response = reply.response; const response = reply.response;
if (!response) { if (!response) {
console.log('No response in HistoryRPC'); throw 'History response misses response field';
return null;
} }
if ( if (
response.error && response.error &&
response.error === HistoryResponse_Error.ERROR_INVALID_CURSOR response.error === HistoryResponse_Error.ERROR_INVALID_CURSOR
) { ) {
console.log('Error in response: INVALID CURSOR'); throw 'History response contains an Error: INVALID CURSOR';
return null;
} }
if (!response.messages || !response.messages.length) { if (!response.messages || !response.messages.length) {
@ -178,21 +173,6 @@ export class WakuStore {
console.log('No cursor returned by peer.'); console.log('No cursor returned by peer.');
return messages; return messages;
} }
} catch (err) {
console.log('Failed to decode store reply', err);
return null;
}
} catch (err) {
console.log('Failed to send waku store query', err);
return null;
}
} catch (err) {
console.log(
'Failed to negotiate waku store protocol stream with peer',
err
);
return null;
}
} }
} }