Skip to content

Commit

Permalink
add error log handler (#218)
Browse files Browse the repository at this point in the history
* add error log handler

* use fs/promises
  • Loading branch information
iamarifdev authored Feb 6, 2023
1 parent 2598cb1 commit 03a1423
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 1 deletion.
28 changes: 28 additions & 0 deletions lib/util/dump_error_log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict'

const os = require('os')
const { appendFile, mkdir } = require('fs/promises')
const { existsSync } = require('fs')
const send = require('../util/ws/send')

const LOG_DIR_PATH = `${os.tmpdir()}/bfx-hf-ui-logs`
const APP_LOG_PATH = `${LOG_DIR_PATH}/app.log`

/**
* Store the error log in app.log file
* @param {object} ws
* @param {string} errorLog - json string
* @returns {void}
*/
module.exports = async (ws, errorLog) => {
try {
if (!existsSync(LOG_DIR_PATH)) {
await mkdir(LOG_DIR_PATH)
}

await appendFile(APP_LOG_PATH, `${errorLog}${os.EOL}`)
} catch (e) {
console.error('ERR_APP_LOG', e)
send(ws, ['data.app_log_error', e.message])
}
}
19 changes: 19 additions & 0 deletions lib/ws_servers/api/handlers/on_dump_error_log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict'

const send = require('../../../util/ws/send')
const validateParams = require('../../../util/ws/validate_params')
const dumpErrorLog = require('../../../util/dump_error_log')

module.exports = async (server, ws, msg) => {
const [, errorLog] = msg

const validRequest = validateParams(ws, {
errorLog: { type: 'string', v: errorLog }
})

if (!validRequest) {
return send(ws, ['data.app_log', 'failed'])
}

await dumpErrorLog(ws, errorLog)
}
6 changes: 5 additions & 1 deletion lib/ws_servers/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ const onFeatureFlagsRequest = require('./handlers/on_feature_flags_request')

const onChangeMode = require('./handlers/on_change_mode')

const onDumpErrorLog = require('./handlers/on_dump_error_log')

const getUserSettings = require('../../util/user_settings')
const algoHost = require('bfx-hf-algo')

Expand Down Expand Up @@ -118,7 +120,9 @@ module.exports = class APIWSServer extends WSServer {
'algo_order.remove': onAlgoOrderRemove,
'algo_order.pause': onAlgoPause,
'settings.update': onSettingsUpdate,
'favourite_trading_pairs.save': onSaveFavouriteTradingPairs
'favourite_trading_pairs.save': onSaveFavouriteTradingPairs,

'error_log.dump': onDumpErrorLog
}
})

Expand Down
77 changes: 77 additions & 0 deletions test/unit/lib/util/dump_error_log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* eslint-disable no-unused-expressions */
/* eslint-env mocha */
'use strict'

const chai = require('chai')
const sinon = require('sinon')
const proxyquire = require('proxyquire')
const fs = require('fs')
const { expect } = chai

describe('dump error log', () => {
let mkdirStub
let appendFileStub
let existsSyncStub
let sendStub
let dumpErrorLog

beforeEach(() => {
mkdirStub = sinon.stub(fs.promises, 'mkdir')
appendFileStub = sinon.stub(fs.promises, 'appendFile')
existsSyncStub = sinon.stub(fs, 'existsSync')
sendStub = sinon.stub()

dumpErrorLog = proxyquire('../../../../lib/util/dump_error_log', {
'fs/promises': {
mkdir: mkdirStub,
appendFile: appendFileStub
},
fs: {
existsSync: existsSyncStub
},
'./ws/send': sendStub
})
})

afterEach(() => {
mkdirStub.restore()
appendFileStub.restore()
existsSyncStub.restore()
sendStub.reset()
})

it('should create the log directory if it does not exist', async () => {
existsSyncStub.returns(false)

await dumpErrorLog({}, {})

expect(mkdirStub.calledOnce).to.be.true
})

it('should not create the log directory if it is already exist', async () => {
existsSyncStub.returns(true)

await dumpErrorLog({}, {})

expect(mkdirStub.notCalled).to.be.true
})

it('should append the error log to the app.log file', async () => {
existsSyncStub.returns(true)

const errorLog = JSON.stringify({ error: 'app error' })
await dumpErrorLog({}, errorLog)

expect(appendFileStub.calledOnce).to.be.true
expect(appendFileStub.firstCall.args[1]).to.contain(errorLog)
})

it('should send a ws message if an error occurs', async () => {
existsSyncStub.returns(false)
mkdirStub.rejects(new Error('MKDIR_ERROR'))

await dumpErrorLog({}, {})

expect(sendStub.calledWith({}, ['data.app_log_error', 'MKDIR_ERROR'])).to.be.true
})
})

0 comments on commit 03a1423

Please sign in to comment.