aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/utils.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/utils.js')
-rw-r--r--server/helpers/utils.js35
1 files changed, 34 insertions, 1 deletions
diff --git a/server/helpers/utils.js b/server/helpers/utils.js
index 9f27671b6..a902850cd 100644
--- a/server/helpers/utils.js
+++ b/server/helpers/utils.js
@@ -1,13 +1,21 @@
1'use strict' 1'use strict'
2 2
3const crypto = require('crypto') 3const crypto = require('crypto')
4const retry = require('async/retry')
4 5
5const logger = require('./logger') 6const logger = require('./logger')
6 7
7const utils = { 8const utils = {
9 badRequest,
8 cleanForExit, 10 cleanForExit,
9 generateRandomString, 11 generateRandomString,
10 isTestInstance 12 isTestInstance,
13 getFormatedObjects,
14 transactionRetryer
15}
16
17function badRequest (req, res, next) {
18 res.type('json').status(400).end()
11} 19}
12 20
13function generateRandomString (size, callback) { 21function generateRandomString (size, callback) {
@@ -27,6 +35,31 @@ function isTestInstance () {
27 return (process.env.NODE_ENV === 'test') 35 return (process.env.NODE_ENV === 'test')
28} 36}
29 37
38function getFormatedObjects (objects, objectsTotal) {
39 const formatedObjects = []
40
41 objects.forEach(function (object) {
42 formatedObjects.push(object.toFormatedJSON())
43 })
44
45 return {
46 total: objectsTotal,
47 data: formatedObjects
48 }
49}
50
51function transactionRetryer (func, callback) {
52 retry({
53 times: 5,
54
55 errorFilter: function (err) {
56 const willRetry = (err.name === 'SequelizeDatabaseError')
57 logger.debug('Maybe retrying the transaction function.', { willRetry })
58 return willRetry
59 }
60 }, func, callback)
61}
62
30// --------------------------------------------------------------------------- 63// ---------------------------------------------------------------------------
31 64
32module.exports = utils 65module.exports = utils