-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add error log handler * use fs/promises
- Loading branch information
1 parent
2598cb1
commit 03a1423
Showing
4 changed files
with
129 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
}) |