Refined error reporting

pull/1091/head
Vincent 5 years ago
parent 345c7b12d9
commit 4033b70f5c

@ -758,9 +758,8 @@ class LokiSnodeAPI {
async getLnsMapping(lnsName, timeout) { async getLnsMapping(lnsName, timeout) {
// Returns { pubkey, error } // Returns { pubkey, error }
// pubkey is: // pubkey is
// null when there is confirmed to be no LNS mapping // undefined when unconfirmed or no mapping found
// undefined when unconfirmed
// string when found // string when found
// timeout parameter optional (ms) // timeout parameter optional (ms)
@ -780,16 +779,12 @@ class LokiSnodeAPI {
const output = await window.blake2b(input); const output = await window.blake2b(input);
const nameHash = dcodeIO.ByteBuffer.wrap(output).toString('base64'); const nameHash = dcodeIO.ByteBuffer.wrap(output).toString('base64');
const timeoutResponse = { timedOut: true }; // Timeouts
const maxTimeoutVal = 2 ** 31 - 1; const maxTimeoutVal = 2 ** 31 - 1;
const timeoutPromise = (cb, interval) => () => const timeoutPromise = () =>
new Promise(resolve => new Promise((_resolve, reject) =>
setTimeout(() => cb(resolve), interval || maxTimeoutVal) setTimeout(() => reject(), timeout || maxTimeoutVal)
); );
const onTimeout = timeoutPromise(
resolve => resolve(timeoutResponse),
timeout
);
// Get nodes capable of doing LNS // Get nodes capable of doing LNS
const lnsNodes = await this.getNodesMinVersion( const lnsNodes = await this.getNodesMinVersion(
@ -798,12 +793,13 @@ class LokiSnodeAPI {
// Enough nodes? // Enough nodes?
if (lnsNodes.length < numRequiredConfirms) { if (lnsNodes.length < numRequiredConfirms) {
error = window.i18n('lnsTooFewNodes'); error = { lnsTooFewNodes: window.i18n('lnsTooFewNodes') };
return { pubkey, error }; return { pubkey, error };
} }
const confirmedNodes = []; const confirmedNodes = [];
// Promise is only resolved when a consensus is found
let cipherResolve; let cipherResolve;
const cipherPromise = () => const cipherPromise = () =>
new Promise(resolve => { new Promise(resolve => {
@ -834,7 +830,7 @@ class LokiSnodeAPI {
if (confirmedNodes.length >= numRequiredConfirms) { if (confirmedNodes.length >= numRequiredConfirms) {
if (ciphertextHex) { if (ciphertextHex) {
// result already found, dont worry // Result already found, dont worry
return; return;
} }
@ -848,7 +844,7 @@ class LokiSnodeAPI {
// null represents no LNS mapping // null represents no LNS mapping
if (ciphertextHex === null) { if (ciphertextHex === null) {
error = window.i18n('lnsMappingNotFound'); error = { lnsMappingNotFound: window.i18n('lnsMappingNotFound') };
} }
cipherResolve({ ciphertextHex }); cipherResolve({ ciphertextHex });
@ -864,16 +860,16 @@ class LokiSnodeAPI {
// Timeouts (optional parameter) // Timeouts (optional parameter)
// Wait for cipher to be found; race against timeout // Wait for cipher to be found; race against timeout
const { timedOut } = await Promise.race( // eslint-disable-next-line more/no-then
[cipherPromise, onTimeout].map(f => f()) await Promise.race([cipherPromise, timeoutPromise].map(f => f()))
); .then(async () => {
if (ciphertextHex !== null) {
if (timedOut) { pubkey = await decryptHex(ciphertextHex);
error = window.i18n('lnsLookupTimeout'); }
return { pubkey, error }; })
} .catch(() => {
error = { lnsLookupTimeout: window.i18n('lnsLookupTimeout') };
pubkey = ciphertextHex === null ? null : await decryptHex(ciphertextHex); });
return { pubkey, error }; return { pubkey, error };
} }

7
ts/global.d.ts vendored

@ -63,3 +63,10 @@ interface Window {
interface Promise<T> { interface Promise<T> {
ignore(): void; ignore(): void;
} }
// Types also correspond to messages.json keys
enum LnsLookupErrorType {
lnsTooFewNodes,
lnsLookupTimeout,
lnsMappingNotFound,
}

Loading…
Cancel
Save