use meta.code to emulate proper http response from proxy, logging style updates

pull/1061/head
Ryan Tharp 5 years ago
parent f6233b91a9
commit 078e95f8c9

@ -42,7 +42,7 @@ const sendToProxy = async (
) => { ) => {
if (!srvPubKey) { if (!srvPubKey) {
log.error( log.error(
'loki_app_dot_net: sendToProxy called without a server public key' 'loki_app_dot_net:::sendToProxy - called without a server public key'
); );
return {}; return {};
} }
@ -145,7 +145,7 @@ const sendToProxy = async (
randSnode randSnode
); );
log.warn( log.warn(
`loki_app_dot_net: Marking random snode bad, internet address ${ `loki_app_dot_net:::sendToProxy - Marking random snode bad, internet address ${
randSnode.ip randSnode.ip
}:${ }:${
randSnode.port randSnode.port
@ -161,7 +161,7 @@ const sendToProxy = async (
response = JSON.parse(txtResponse); response = JSON.parse(txtResponse);
} catch (e) { } catch (e) {
log.warn( log.warn(
`loki_app_dot_net: sendToProxy Could not parse outer JSON [${txtResponse}]`, `loki_app_dot_net:::sendToProxy - Could not parse outer JSON [${txtResponse}]`,
endpoint, endpoint,
'on', 'on',
url url
@ -185,7 +185,7 @@ const sendToProxy = async (
response = options.textResponse ? respStr : JSON.parse(respStr); response = options.textResponse ? respStr : JSON.parse(respStr);
} catch (e) { } catch (e) {
log.warn( log.warn(
`loki_app_dot_net: sendToProxy Could not parse inner JSON [${respStr}]`, `loki_app_dot_net:::sendToProxy - Could not parse inner JSON [${respStr}]`,
endpoint, endpoint,
'on', 'on',
url url
@ -193,7 +193,7 @@ const sendToProxy = async (
} }
} else { } else {
log.warn( log.warn(
'loki_app_dot_net: file server secure_rpc gave an non-200 response: ', 'loki_app_dot_net:::sendToProxy - file server secure_rpc gave an non-200 response: ',
response, response,
` txtResponse[${txtResponse}]`, ` txtResponse[${txtResponse}]`,
endpoint endpoint
@ -239,7 +239,11 @@ const serverRequest = async (endpoint, options = {}) => {
fetchOptions.agent = snodeHttpsAgent; fetchOptions.agent = snodeHttpsAgent;
} }
} catch (e) { } catch (e) {
log.info('serverRequest set up error:', e.code, e.message); log.error(
'loki_app_dot_net:::serverRequest - set up error:',
e.code,
e.message
);
return { return {
err: e, err: e,
}; };
@ -257,10 +261,10 @@ const serverRequest = async (endpoint, options = {}) => {
FILESERVER_HOSTS.includes(host) FILESERVER_HOSTS.includes(host)
) { ) {
mode = 'sendToProxy'; mode = 'sendToProxy';
const search = url.search ? `?${url.search}` : ''; // url.search automatically includes the ? part
const search = url.search ? `${url.search}` : '';
// strip first slash // strip first slash
const endpointWithQS = `${url.pathname}${search}`.replace(/^\//, ''); const endpointWithQS = `${url.pathname}${search}`.replace(/^\//, '');
// log.info('endpointWithQS', endpointWithQS)
({ response, txtResponse, result } = await sendToProxy( ({ response, txtResponse, result } = await sendToProxy(
srvPubKey, srvPubKey,
endpointWithQS, endpointWithQS,
@ -278,11 +282,16 @@ const serverRequest = async (endpoint, options = {}) => {
txtResponse = await result.text(); txtResponse = await result.text();
// cloudflare timeouts (504s) will be html... // cloudflare timeouts (504s) will be html...
response = options.textResponse ? txtResponse : JSON.parse(txtResponse); response = options.textResponse ? txtResponse : JSON.parse(txtResponse);
// result.status will always be 200
// emulate the correct http code if available
if (response && response.meta && response.meta.code) {
result.status = response.meta.code;
}
} }
} catch (e) { } catch (e) {
if (txtResponse) { if (txtResponse) {
log.info( log.error(
`serverRequest ${mode} error`, `loki_app_dot_net:::serverRequest - ${mode} error`,
e.code, e.code,
e.message, e.message,
`json: ${txtResponse}`, `json: ${txtResponse}`,
@ -290,8 +299,8 @@ const serverRequest = async (endpoint, options = {}) => {
url url
); );
} else { } else {
log.info( log.error(
`serverRequest ${mode} error`, `loki_app_dot_net:::serverRequest - ${mode} error`,
e.code, e.code,
e.message, e.message,
'attempting connection to', 'attempting connection to',
@ -1609,12 +1618,12 @@ class LokiPublicChannelAPI {
if (res.err || !res.response) { if (res.err || !res.response) {
log.error( log.error(
'Could not get messages from', `app_dot_net:::pollOnceForMessages - Could not get messages from`,
this.serverAPI.baseServerUrl, this.serverAPI.baseServerUrl,
this.baseChannelUrl this.baseChannelUrl
); );
if (res.err) { if (res.err) {
log.error('pollOnceForMessages receive error', res.err); log.error(`app_dot_net:::pollOnceForMessages - receive error`, res.err);
} }
this.messagesPollLock = false; this.messagesPollLock = false;
return; return;

Loading…
Cancel
Save