Skip to content

Commit

Permalink
add missing log events (#220)
Browse files Browse the repository at this point in the history
* add missing log events

* refactor event messages and include stack trace
  • Loading branch information
iamarifdev authored Feb 23, 2023
1 parent 80fe2cc commit 2a23ffa
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 43 deletions.
36 changes: 22 additions & 14 deletions lib/ws_servers/api/handlers/on_auth_reset.js
Original file line number Diff line number Diff line change
@@ -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])
}
}
7 changes: 5 additions & 2 deletions lib/ws_servers/api/handlers/on_auth_submit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand All @@ -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')
Expand Down
7 changes: 5 additions & 2 deletions lib/ws_servers/api/handlers/on_save_api_credentials.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand All @@ -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) {
Expand Down
49 changes: 27 additions & 22 deletions lib/ws_servers/api/handlers/on_settings_update.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
,
Expand Down Expand Up @@ -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])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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)
})

Expand Down

0 comments on commit 2a23ffa

Please sign in to comment.