diff options
Diffstat (limited to 'server/helpers/utils.js')
-rw-r--r-- | server/helpers/utils.js | 35 |
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 | ||
3 | const crypto = require('crypto') | 3 | const crypto = require('crypto') |
4 | const retry = require('async/retry') | ||
4 | 5 | ||
5 | const logger = require('./logger') | 6 | const logger = require('./logger') |
6 | 7 | ||
7 | const utils = { | 8 | const utils = { |
9 | badRequest, | ||
8 | cleanForExit, | 10 | cleanForExit, |
9 | generateRandomString, | 11 | generateRandomString, |
10 | isTestInstance | 12 | isTestInstance, |
13 | getFormatedObjects, | ||
14 | transactionRetryer | ||
15 | } | ||
16 | |||
17 | function badRequest (req, res, next) { | ||
18 | res.type('json').status(400).end() | ||
11 | } | 19 | } |
12 | 20 | ||
13 | function generateRandomString (size, callback) { | 21 | function 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 | ||
38 | function 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 | |||
51 | function 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 | ||
32 | module.exports = utils | 65 | module.exports = utils |