improve logging

pull/787/head
Ryan Tharp 6 years ago
parent 3bba0571a0
commit 99afd33fb4

@ -13,16 +13,18 @@ const LOKI_EPHEMKEY_HEADER = 'X-Loki-EphemKey';
const endpointBase = '/storage_rpc/v1'; const endpointBase = '/storage_rpc/v1';
const decryptResponse = async (response, address) => { const decryptResponse = async (response, address) => {
let plaintext = false;
try { try {
const ciphertext = await response.text(); const ciphertext = await response.text();
const plaintext = await libloki.crypto.snodeCipher.decrypt( plaintext = await libloki.crypto.snodeCipher.decrypt(address, ciphertext);
address,
ciphertext
);
const result = plaintext === '' ? {} : JSON.parse(plaintext); const result = plaintext === '' ? {} : JSON.parse(plaintext);
return result; return result;
} catch (e) { } 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 {}; return {};
}; };
@ -80,11 +82,21 @@ const sendToProxy = async (options = {}, targetNode) => {
const textDecoder = new TextDecoder(); const textDecoder = new TextDecoder();
const plaintext = textDecoder.decode(plaintextBuffer); const plaintext = textDecoder.decode(plaintextBuffer);
try {
const jsonRes = JSON.parse(plaintext); const jsonRes = JSON.parse(plaintext);
// emulate nodeFetch response...
jsonRes.json = () => JSON.parse(jsonRes.body); jsonRes.json = () => JSON.parse(jsonRes.body);
return jsonRes; 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 // A small wrapper around node-fetch which deserializes response

Loading…
Cancel
Save