From a05b8d1ba28aeb3964adc0a2b9938dacd767a73f Mon Sep 17 00:00:00 2001 From: Michel Weststrate Date: Thu, 20 Feb 2020 04:30:43 -0800 Subject: [PATCH] Prevent extensive logging of failed network requests Summary: If the browser makes a network request, and it fails, this is _always_ logged to the console, despite any error handling being in place. (see https://stackoverflow.com/questions/43012334/silence-neterr-connection-refused) This diffs doesn't use the browser network stack, but the node network stack instead, which doesn't suffer from the same fate. Reviewed By: passy Differential Revision: D19995684 fbshipit-source-id: 4ffc12b820620c5310c140c1a3af63e5d2053a50 --- src/dispatcher/metroDevice.tsx | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/dispatcher/metroDevice.tsx b/src/dispatcher/metroDevice.tsx index d0a5bf68a..433004012 100644 --- a/src/dispatcher/metroDevice.tsx +++ b/src/dispatcher/metroDevice.tsx @@ -12,6 +12,7 @@ import {Logger} from '../fb-interfaces/Logger'; import {registerDeviceCallbackOnPlugins} from '../utils/onRegisterDevice'; import MetroDevice from '../devices/MetroDevice'; import {ArchivedDevice} from 'flipper'; +import http from 'http'; const METRO_PORT = 8081; const METRO_HOST = 'localhost'; @@ -22,13 +23,26 @@ const QUERY_INTERVAL = 5000; const METRO_DEVICE_ID = 'metro'; // there is always only one activve async function isMetroRunning(): Promise { - try { - // TODO: this prints a log error without connection, fix that - const contents = await (await global.fetch(METRO_URL)).text(); - return METRO_MESSAGE.some(msg => contents.includes(msg)); - } catch (e) { - return false; - } + return new Promise(resolve => { + // We use Node's http library, rather than fetch api, as the latter cannot supress network errors being shown in the devtools console + // which generates a lot of noise + http + .get(METRO_URL, resp => { + let data = ''; + resp + .on('data', chunk => { + data += chunk; + }) + .on('end', () => { + const isMetro = METRO_MESSAGE.some(msg => data.includes(msg)); + resolve(isMetro); + }); + }) + .on('error', err => { + console.debug('Could not connect to METRO ' + err); + resolve(false); + }); + }); } async function registerDevice(