From 1662622c232c4d26d2552e344d27a6dec23779c3 Mon Sep 17 00:00:00 2001 From: Miguel Serrano Date: Wed, 3 Jun 2020 11:14:44 +0200 Subject: [PATCH] Updated OError code to v3 --- services/spelling/app/js/ASpell.js | 12 ++-- services/spelling/app/js/ASpellWorker.js | 68 +++++++------------ services/spelling/app/js/ASpellWorkerPool.js | 3 +- .../spelling/app/js/HealthCheckController.js | 5 +- .../spelling/app/js/LearnedWordsManager.js | 3 +- .../spelling/app/js/SpellingAPIController.js | 17 +++-- .../spelling/app/js/SpellingAPIManager.js | 11 +-- 7 files changed, 54 insertions(+), 65 deletions(-) diff --git a/services/spelling/app/js/ASpell.js b/services/spelling/app/js/ASpell.js index 4f2b251d25..67b37c54f9 100644 --- a/services/spelling/app/js/ASpell.js +++ b/services/spelling/app/js/ASpell.js @@ -14,6 +14,7 @@ const fs = require('fs') const settings = require('settings-sharelatex') const Path = require('path') const { promisify } = require('util') +const OError = require('@overleaf/o-error') const OneMinute = 60 * 1000 const opts = { max: 10000, maxAge: OneMinute * 60 * 10 } @@ -27,8 +28,9 @@ try { const oldCache = fs.readFileSync(cacheFsPath) cache.load(JSON.parse(oldCache)) } catch (error) { - const err = error - logger.log({ err, cacheFsPath }, 'could not load the cache file') + logger.log( + OError.tag(error, 'could not load the cache file', { cacheFsPath }) + ) } // write the cache every 30 minutes @@ -36,12 +38,12 @@ const cacheDump = setInterval(function() { const dump = JSON.stringify(cache.dump()) return fs.writeFile(cacheFsPathTmp, dump, function(err) { if (err != null) { - logger.log({ err }, 'error writing cache file') + logger.log(OError.tag(err, 'error writing cache file')) return fs.unlink(cacheFsPathTmp) } else { fs.rename(cacheFsPathTmp, cacheFsPath, err => { if (err) { - logger.error({ err }, 'error renaming cache file') + logger.error(OError.tag(err, 'error renaming cache file')) } else { logger.log({ len: dump.length, cacheFsPath }, 'wrote cache file') } @@ -57,7 +59,7 @@ class ASpellRunner { } return this.runAspellOnWords(language, words, (error, output) => { if (error != null) { - return callback(error) + return callback(OError.tag(error)) } // output = @removeAspellHeader(output) const suggestions = this.getSuggestions(language, output) diff --git a/services/spelling/app/js/ASpellWorker.js b/services/spelling/app/js/ASpellWorker.js index 834917b755..0148620805 100644 --- a/services/spelling/app/js/ASpellWorker.js +++ b/services/spelling/app/js/ASpellWorker.js @@ -49,9 +49,9 @@ class ASpellWorker { this.state = 'closed' } if (this.callback != null) { - const err = new OError({ - message: 'aspell worker closed output streams with uncalled callback', - info: { + const err = new OError( + 'aspell worker closed output streams with uncalled callback', + { process: this.pipe.pid, lang: this.language, stdout: output.slice(-1024), @@ -60,7 +60,7 @@ class ASpellWorker { previousWorkerState, closeReason: this.closeReason } - }) + ) this.callback(err, []) this.callback = null } @@ -70,7 +70,7 @@ class ASpellWorker { if (this.state !== 'killed') { this.state = 'error' } - const errInfo = { + OError.tag(err, 'aspell worker error', { process: this.pipe.pid, stdout: output.slice(-1024), stderr: error.slice(-1024), @@ -78,22 +78,13 @@ class ASpellWorker { workerState: this.state, previousWorkerState, closeReason: this.closeReason - } + }) if (this.callback != null) { - this.callback( - new OError({ - message: 'aspell worker error', - info: errInfo - }).withCause(err), - [] - ) + this.callback(err, []) this.callback = null } else { - logger.warn( - Object.assign({ error: err }, errInfo), - 'aspell worker error' - ) + logger.warn(err) } }) this.pipe.stdin.on('error', err => { @@ -101,7 +92,8 @@ class ASpellWorker { if (this.state !== 'killed') { this.state = 'error' } - const errInfo = { + + OError.tag(err, 'aspell worker error on stdin', { process: this.pipe.pid, stdout: output.slice(-1024), stderr: error.slice(-1024), @@ -109,22 +101,13 @@ class ASpellWorker { workerState: this.state, previousWorkerState, closeReason: this.closeReason - } + }) if (this.callback != null) { - this.callback( - new OError({ - message: 'aspell worker error on stdin', - info: errInfo - }).withCause(err), - [] - ) + this.callback(err, []) this.callback = null } else { - logger.warn( - Object.assign({ error: err }, errInfo), - 'aspell worker error on stdin' - ) + logger.warn(err) } }) @@ -145,12 +128,14 @@ class ASpellWorker { this.callback = null // only allow one callback in use } else { logger.err( - { - process: this.pipe.pid, - lang: this.language, - workerState: this.state - }, - 'end of data marker received when callback already used' + new OError( + 'end of data marker received when callback already used', + { + process: this.pipe.pid, + lang: this.language, + workerState: this.state + } + ) ) } this.state = 'ready' @@ -180,13 +165,10 @@ class ASpellWorker { if (this.callback != null) { // only allow one callback in use return this.callback( - new OError({ - message: 'Aspell callback already in use - SHOULD NOT HAPPEN', - info: { - process: this.pipe.pid, - lang: this.language, - workerState: this.state - } + new OError('Aspell callback already in use - SHOULD NOT HAPPEN', { + process: this.pipe.pid, + lang: this.language, + workerState: this.state }) ) } diff --git a/services/spelling/app/js/ASpellWorkerPool.js b/services/spelling/app/js/ASpellWorkerPool.js index a0daf8dee2..eb9bc32a99 100644 --- a/services/spelling/app/js/ASpellWorkerPool.js +++ b/services/spelling/app/js/ASpellWorkerPool.js @@ -11,6 +11,7 @@ const ASpellWorker = require('./ASpellWorker') const _ = require('underscore') const logger = require('logger-sharelatex') const metrics = require('metrics-sharelatex') +const OError = require('@overleaf/o-error') class ASpellWorkerPool { static initClass() { @@ -75,7 +76,7 @@ class ASpellWorkerPool { if (worker == null) { // return error if too many workers - callback(new Error('no worker available')) + callback(new OError('no worker available')) return } diff --git a/services/spelling/app/js/HealthCheckController.js b/services/spelling/app/js/HealthCheckController.js index 802cf073c6..0557e75a62 100644 --- a/services/spelling/app/js/HealthCheckController.js +++ b/services/spelling/app/js/HealthCheckController.js @@ -1,6 +1,7 @@ const request = require('request') const logger = require('logger-sharelatex') const settings = require('settings-sharelatex') +const OError = require('@overleaf/o-error') module.exports = { healthCheck(req, res) { @@ -28,7 +29,9 @@ module.exports = { logger.log('health check passed') res.sendStatus(200) } else { - logger.err({ body, numberOfSuggestions }, 'health check failed') + logger.err( + new OError('health check failed', { body, numberOfSuggestions }) + ) res.sendStatus(500) } }) diff --git a/services/spelling/app/js/LearnedWordsManager.js b/services/spelling/app/js/LearnedWordsManager.js index 2b57697014..4ead300dd8 100644 --- a/services/spelling/app/js/LearnedWordsManager.js +++ b/services/spelling/app/js/LearnedWordsManager.js @@ -3,6 +3,7 @@ const mongoCache = require('./MongoCache') const logger = require('logger-sharelatex') const metrics = require('metrics-sharelatex') const { promisify } = require('util') +const OError = require('@overleaf/o-error') const LearnedWordsManager = { learnWord(userToken, word, callback) { @@ -58,7 +59,7 @@ const LearnedWordsManager = { preferences ) { if (error != null) { - return callback(error) + return callback(OError.tag(error)) } let words = (preferences != null ? preferences.learnedWords : undefined) || [] diff --git a/services/spelling/app/js/SpellingAPIController.js b/services/spelling/app/js/SpellingAPIController.js index f771a7a567..a061d57abd 100644 --- a/services/spelling/app/js/SpellingAPIController.js +++ b/services/spelling/app/js/SpellingAPIController.js @@ -1,6 +1,7 @@ const SpellingAPIManager = require('./SpellingAPIManager') const logger = require('logger-sharelatex') const metrics = require('metrics-sharelatex') +const OError = require('@overleaf/o-error') function extractCheckRequestData(req) { const token = req.params ? req.params.user_id : undefined @@ -22,13 +23,11 @@ module.exports = { logger.info({ token, wordCount }, 'running check') SpellingAPIManager.runRequest(token, req.body, function(error, result) { if (error != null) { - logger.err( - { - err: error, + logger.error( + OError.tag(error, 'error processing spelling request', { user_id: token, wordCount - }, - 'error processing spelling request' + }) ) return res.sendStatus(500) } @@ -42,7 +41,7 @@ module.exports = { logger.info({ token, word }, 'learning word') SpellingAPIManager.learnWord(token, req.body, function(error) { if (error != null) { - return next(error) + return next(OError.tag(error)) } res.sendStatus(204) }) @@ -54,7 +53,7 @@ module.exports = { logger.info({ token, word }, 'unlearning word') SpellingAPIManager.unlearnWord(token, req.body, function(error) { if (error != null) { - return next(error) + return next(OError.tag(error)) } res.sendStatus(204) }) @@ -65,7 +64,7 @@ module.exports = { logger.log({ token, word }, 'deleting user dictionary') SpellingAPIManager.deleteDic(token, function(error) { if (error != null) { - return next(error) + return next(OError.tag(error)) } res.sendStatus(204) }) @@ -81,7 +80,7 @@ module.exports = { ) SpellingAPIManager.getDic(token, function(error, words) { if (error != null) { - return next(error) + return next(OError.tag(error)) } res.send(words) }) diff --git a/services/spelling/app/js/SpellingAPIManager.js b/services/spelling/app/js/SpellingAPIManager.js index 3c437d929f..89ee27eb30 100644 --- a/services/spelling/app/js/SpellingAPIManager.js +++ b/services/spelling/app/js/SpellingAPIManager.js @@ -9,6 +9,7 @@ const ASpell = require('./ASpell') const LearnedWordsManager = require('./LearnedWordsManager') const { callbackify } = require('util') +const OError = require('@overleaf/o-error') // The max number of words checked in a single request const REQUEST_LIMIT = 10000 @@ -21,10 +22,10 @@ const SpellingAPIManager = { callback = () => {} } if (request.word == null) { - return callback(new Error('malformed JSON')) + return callback(new OError('malformed JSON')) } if (token == null) { - return callback(new Error('no token provided')) + return callback(new OError('no token provided')) } return LearnedWordsManager.learnWord(token, request.word, callback) @@ -35,10 +36,10 @@ const SpellingAPIManager = { callback = () => {} } if (request.word == null) { - return callback(new Error('malformed JSON')) + return callback(new OError('malformed JSON')) } if (token == null) { - return callback(new Error('no token provided')) + return callback(new OError('no token provided')) } return LearnedWordsManager.unlearnWord(token, request.word, callback) @@ -56,7 +57,7 @@ const SpellingAPIManager = { const promises = { async runRequest(token, request) { if (!request.words) { - throw new Error('malformed JSON') + throw new OError('malformed JSON') } const lang = request.language || 'en'