UNPKG

1.41 kBJavaScriptView Raw
1const _ = require('lodash');
2const path = require('path');
3const HttpStatus = require('http-status');
4const bytes = require('byte-converter').converterBase2;
5
6const parsePath = (input) => {
7 if (path.isAbsolute(input)) {
8 return input;
9 } else {
10 return path.join(path.resolve('.'), input);
11 }
12};
13
14const isNonZeroString = (input) => {
15 return _.isString(input) && input.length > 0;
16};
17
18const processError = function (error, res) {
19 const log = require('../config').log;
20
21 let message = '';
22 let code = HttpStatus.BAD_REQUEST;
23 if (_.isUndefined(error)) {
24 log.error('undefined error caught', error);
25 message = 'error triggered, but error is undefined';
26 } else if (_.isString(error.message) && error.message.indexOf('does not exist') !== -1) {
27 message = error;
28 code = HttpStatus.NOT_FOUND;
29 } else if (_.isString(error)) {
30 log.warn('error', error);
31 message = error;
32 } else if (_.isString(error.message)) {
33 log.warn('error', error);
34 message = error.message;
35 } else {
36 log.error('error caught, but of unknown format');
37 message = 'error caught, but of unknown format';
38 }
39
40 res.status(code).json({error: message});
41};
42
43const to_bytes = (value, unit) => bytes(value, unit, 'B');
44
45module.exports = {
46 parsePath: parsePath,
47 isNonZeroString: isNonZeroString,
48 processError: processError,
49 to_bytes: to_bytes,
50};
\No newline at end of file