From 2a23ffaeefaefc2dd562e15ef666e40b5f7c481b Mon Sep 17 00:00:00 2001 From: Ariful Islam Date: Thu, 23 Feb 2023 20:55:14 +0600 Subject: [PATCH] add missing log events (#220) * add missing log events * refactor event messages and include stack trace --- lib/ws_servers/api/handlers/on_auth_reset.js | 36 ++++++++------ lib/ws_servers/api/handlers/on_auth_submit.js | 7 ++- .../on_decrypted_saved_strategies_request.js | 7 ++- .../api/handlers/on_save_api_credentials.js | 7 ++- .../api/handlers/on_settings_update.js | 49 ++++++++++--------- .../api/handlers/on_save_api_credentials.js | 4 +- 6 files changed, 67 insertions(+), 43 deletions(-) diff --git a/lib/ws_servers/api/handlers/on_auth_reset.js b/lib/ws_servers/api/handlers/on_auth_reset.js index 2d725baa..ccbcc811 100644 --- a/lib/ws_servers/api/handlers/on_auth_reset.js +++ b/lib/ws_servers/api/handlers/on_auth_reset.js @@ -1,27 +1,35 @@ 'use strict' const send = require('../../../util/ws/send') -const { notifyInfo } = require('../../../util/ws/notify') +const { notifyInfo, notifyError } = require('../../../util/ws/notify') module.exports = async (server, ws, msg) => { const { d, db, algoDB, strategyExecutionDB } = server const { Credential, Strategy, UserSettings, FavouriteTradingPairs, AlgoOrderParams } = db const { StrategyExecution } = strategyExecutionDB const { AlgoOrder } = algoDB + const { sendDataToMetricsServer } = ws - await Promise.all([ - AlgoOrder.rmAll(), - Credential.rmAll(), - Strategy.rmAll(), - UserSettings.rmAll(), - FavouriteTradingPairs.rmAll(), - AlgoOrderParams.rmAll(), - StrategyExecution.rmAll() - ]) + try { + await Promise.all([ + AlgoOrder.rmAll(), + Credential.rmAll(), + Strategy.rmAll(), + UserSettings.rmAll(), + FavouriteTradingPairs.rmAll(), + AlgoOrderParams.rmAll(), + StrategyExecution.rmAll() + ]) - send(ws, ['info.auth_configured', false]) - send(ws, ['info.auth_token', null]) + send(ws, ['info.auth_configured', false]) + send(ws, ['info.auth_token', null]) - notifyInfo(ws, 'Cleared user credentials & data', ['clearedUserCredentialsAndData']) - d('reset user credentials') + notifyInfo(ws, 'Cleared user credentials & data', ['clearedUserCredentialsAndData']) + d('reset user credentials') + sendDataToMetricsServer(['clear_data_success', '[on_auth_reset] Cleared user credentials & data']) + } catch (e) { + notifyError(ws, 'Failed to clear user credentials & data', ['clearedUserCredentialsAndDataFailed']) + d('failed to reset user credentials %s', e.stack) + sendDataToMetricsServer(['clear_data_failed', e.stack]) + } } diff --git a/lib/ws_servers/api/handlers/on_auth_submit.js b/lib/ws_servers/api/handlers/on_auth_submit.js index 46dc66d3..559dcc8f 100644 --- a/lib/ws_servers/api/handlers/on_auth_submit.js +++ b/lib/ws_servers/api/handlers/on_auth_submit.js @@ -22,6 +22,7 @@ module.exports = async (server, ws, msg) => { authenticating = true + const { sendDataToMetricsServer } = ws const { d, db, restURL } = server const [, password, mode, dmsScope] = msg const validRequest = validateParams(ws, { @@ -43,12 +44,14 @@ module.exports = async (server, ws, msg) => { if (!authControl) { authenticating = false - return notifyError(ws, 'Invalid password', ['invalidPassword']) + notifyError(ws, 'Invalid password', ['invalidPassword']) + return sendDataToMetricsServer(['app_login_failed', '[on_auth_submit] Invalid password']) } } catch (e) { authenticating = false capture.exception(e) - return notifyInternalError(ws) + notifyInternalError(ws) + return sendDataToMetricsServer(['app_login_failed', e.stack]) } ws.authPassword = hashedPassword diff --git a/lib/ws_servers/api/handlers/on_decrypted_saved_strategies_request.js b/lib/ws_servers/api/handlers/on_decrypted_saved_strategies_request.js index 8be7b4bf..b5c7fe40 100644 --- a/lib/ws_servers/api/handlers/on_decrypted_saved_strategies_request.js +++ b/lib/ws_servers/api/handlers/on_decrypted_saved_strategies_request.js @@ -17,6 +17,7 @@ module.exports = async (server, ws, msg) => { authenticating = true + const { sendDataToMetricsServer } = ws const { d, db } = server const [, password, mode] = msg const validRequest = validateParams(ws, { @@ -38,12 +39,14 @@ module.exports = async (server, ws, msg) => { if (!authControl) { authenticating = false - return notifyError(ws, 'Invalid password', ['invalidPassword']) + notifyError(ws, 'Invalid password', ['invalidPassword']) + return sendDataToMetricsServer(['app_login_failed', '[on_decrypted_saved_strategies_request] Invalid password']) } } catch (e) { authenticating = false capture.exception(e) - return notifyInternalError(ws) + notifyInternalError(ws) + return sendDataToMetricsServer(['app_login_failed', e.stack]) } d('identified') diff --git a/lib/ws_servers/api/handlers/on_save_api_credentials.js b/lib/ws_servers/api/handlers/on_save_api_credentials.js index 030c22eb..1e6eaed8 100644 --- a/lib/ws_servers/api/handlers/on_save_api_credentials.js +++ b/lib/ws_servers/api/handlers/on_save_api_credentials.js @@ -20,6 +20,7 @@ const connManager = require('../start_connections') const exID = 'bitfinex' // legacy support module.exports = async (server, ws, msg) => { + const { sendDataToMetricsServer } = ws const { d, db, restURL } = server const [, authToken, apiKey, apiSecret, formSent, mode, dmsScope] = msg const validRequest = validateParams(ws, { @@ -44,11 +45,13 @@ module.exports = async (server, ws, msg) => { authControl = await verifyPassword(db, ws.authPassword) if (!authControl || ws.authControl !== authControl) { - return notifyError(ws, 'Invalid password', ['invalidPassword']) + notifyError(ws, 'Invalid password', ['invalidPassword']) + return sendDataToMetricsServer(['app_login_failed', '[on_save_api_credentials] Invalid password']) } } catch (e) { capture.exception(e) - return notifyInternalError(ws) + notifyInternalError(ws) + return sendDataToMetricsServer(['app_login_failed', e.stack]) } if (formSent) { diff --git a/lib/ws_servers/api/handlers/on_settings_update.js b/lib/ws_servers/api/handlers/on_settings_update.js index 4cd34992..2a476912 100644 --- a/lib/ws_servers/api/handlers/on_settings_update.js +++ b/lib/ws_servers/api/handlers/on_settings_update.js @@ -7,13 +7,12 @@ const send = require('../../../util/ws/send') const sendError = require('../../../util/ws/send_error') const validateParams = require('../../../util/ws/validate_params') const isAuthorized = require('../../../util/ws/is_authorized') -const { - notifySuccess -} = require('../../../util/ws/notify') +const { notifySuccess, notifyError } = require('../../../util/ws/notify') const connectionManager = require('../start_connections') module.exports = async (server, ws, msg) => { const { d, db } = server + const { sendDataToMetricsServer } = ws const { UserSettings } = db const [ , @@ -47,31 +46,37 @@ module.exports = async (server, ws, msg) => { return sendError(ws, 'Unauthorized') } - const { userSettings: oldSettings = DEFAULT_USER_SETTINGS } = await UserSettings.getAll() - const settings = { - dms: isDmsEnabled, - theme, - affiliateCode, - joinBetaProgram, - showAlgoPauseInfo, - showOnlyFavoritePairs, - hideOnClose, - fullScreen - } + try { + const { userSettings: oldSettings = DEFAULT_USER_SETTINGS } = await UserSettings.getAll() + const settings = { + dms: isDmsEnabled, + theme, + affiliateCode, + joinBetaProgram, + showAlgoPauseInfo, + showOnlyFavoritePairs, + hideOnClose, + fullScreen + } - await UserSettings.set(settings) + await UserSettings.set(settings) - d('UI settings has been updated') + d('UI settings has been updated') - notifySuccess(ws, 'Settings successfully updated', ['settingsSuccessfullyUpdated']) + notifySuccess(ws, 'Settings successfully updated', ['settingsSuccessfullyUpdated']) - send(ws, ['data.settings.updated', settings]) + send(ws, ['data.settings.updated', settings]) - if (oldSettings.dms !== settings.dms) { - d('issuing API & Algo reconnect due to DMS change [dms %s]', settings.dms) + if (oldSettings.dms !== settings.dms) { + d('issuing API & Algo reconnect due to DMS change [dms %s]', settings.dms) - await connectionManager.updateDms(server, ws, isDmsEnabled, dmsScope) + await connectionManager.updateDms(server, ws, isDmsEnabled, dmsScope) - notifySuccess(ws, 'Reconnecting with new DMS setting...', ['reconnectingWithNewDmsSetting']) + notifySuccess(ws, 'Reconnecting with new DMS setting...', ['reconnectingWithNewDmsSetting']) + } + } catch (e) { + d('failed to update the settings %s', e.stack) + notifyError(ws, 'Failed to update settings', ['settingsUpdateFailed']) + sendDataToMetricsServer(['setting_update_fail', e.stack]) } } diff --git a/test/unit/lib/ws_servers/api/handlers/on_save_api_credentials.js b/test/unit/lib/ws_servers/api/handlers/on_save_api_credentials.js index ae818eda..0085a1af 100644 --- a/test/unit/lib/ws_servers/api/handlers/on_save_api_credentials.js +++ b/test/unit/lib/ws_servers/api/handlers/on_save_api_credentials.js @@ -75,7 +75,8 @@ describe('on save api credentials', () => { authControl: 'auth control', closeMode: sandbox.stub(), authenticateSession: sandbox.stub(), - setCredentialsForMode: sandbox.stub() + setCredentialsForMode: sandbox.stub(), + sendDataToMetricsServer: sandbox.stub() } const authToken = 'authToken' const apiKey = 'apiKey' @@ -117,6 +118,7 @@ describe('on save api credentials', () => { assert.calledWithExactly(stubVerifyPassword, server.db, ws.authPassword) assert.calledWithExactly(stubNotifyError, ws, 'Invalid password', ['invalidPassword']) + assert.calledWithExactly(ws.sendDataToMetricsServer, ['app_login_failed', '[on_save_api_credentials] Invalid password']) assert.notCalled(stubEncryptApiCred) })