Improve multi-attempt logging

This commit is contained in:
mmorrison 2019-02-13 22:46:13 -06:00
parent ab6e9064d2
commit dd5dce21db
3 changed files with 34 additions and 42 deletions

View file

@ -70,37 +70,34 @@ class QueryRunner {
throw new Error("Could not determine port to query. Did you provide a port or gameid?");
}
if (attempts.length === 1) {
return await this._attempt(attempts[0]);
} else {
const errors = [];
for (const attempt of attempts) {
const numRetries = userOptions.maxAttempts || gameOptions.maxAttempts || defaultOptions.maxAttempts;
let attemptNum = 0;
const errors = [];
for (const attempt of attempts) {
for (let retry = 0; retry < numRetries; retry++) {
attemptNum++;
try {
return await this._attempt(attempt);
} catch(e) {
const e2 = new Error('Failed to query port ' + attempt.port);
e2.stack += "\nCaused by:\n" + e.stack;
errors.push(e2);
} catch (e) {
e.stack = 'Attempt #' + attemptNum + ' - Port=' + attempt.port + ' Retry=' + (retry) + ':\n' + e.stack;
errors.push(e);
}
}
const err = new Error('Failed all port attempts');
err.stack = errors.map(e => e.stack).join('\n');
throw err;
}
const err = new Error('Failed all ' + errors.length + ' attempts');
for (const e of errors) {
err.stack += '\n' + e.stack;
}
throw err;
}
async _attempt(options) {
if (options.debug) {
const logger = new Logger();
logger.debugEnabled = true;
logger.debug("Running attempt with options:");
logger.debug(options);
}
const core = this.protocolResolver.create(options.protocol);
core.options = options;
core.udpSocket = this.udpSocket;
return await core.runAllAttempts();
return await core.runOnceSafe();
}
}