From 99afd33fb44ad186fb7a78c8bbeb9aad1aea37a0 Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Wed, 5 Feb 2020 01:08:24 -0800 Subject: [PATCH] improve logging --- js/modules/loki_rpc.js | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/js/modules/loki_rpc.js b/js/modules/loki_rpc.js index 9bd613a4e..1cd99b9c7 100644 --- a/js/modules/loki_rpc.js +++ b/js/modules/loki_rpc.js @@ -13,16 +13,18 @@ const LOKI_EPHEMKEY_HEADER = 'X-Loki-EphemKey'; const endpointBase = '/storage_rpc/v1'; const decryptResponse = async (response, address) => { + let plaintext = false; try { const ciphertext = await response.text(); - const plaintext = await libloki.crypto.snodeCipher.decrypt( - address, - ciphertext - ); + plaintext = await libloki.crypto.snodeCipher.decrypt(address, ciphertext); const result = plaintext === '' ? {} : JSON.parse(plaintext); return result; } catch (e) { - log.warn(`Could not decrypt response from ${address}`, e); + log.warn( + `Could not decrypt response [${plaintext}] from [${address}],`, + e.code, + e.message + ); } return {}; }; @@ -80,11 +82,21 @@ const sendToProxy = async (options = {}, targetNode) => { const textDecoder = new TextDecoder(); const plaintext = textDecoder.decode(plaintextBuffer); - const jsonRes = JSON.parse(plaintext); - - jsonRes.json = () => JSON.parse(jsonRes.body); - - return jsonRes; + try { + const jsonRes = JSON.parse(plaintext); + // emulate nodeFetch response... + jsonRes.json = () => JSON.parse(jsonRes.body); + return jsonRes; + } catch (e) { + log.error( + 'lokiRpc sendToProxy error', + e.code, + e.message, + 'json', + plaintext + ); + } + return false; }; // A small wrapper around node-fetch which deserializes response