diff --git a/core/server/utils/cached-image-size-from-url.js b/core/server/utils/cached-image-size-from-url.js index a4f10e633c..4f2b5bed2d 100644 --- a/core/server/utils/cached-image-size-from-url.js +++ b/core/server/utils/cached-image-size-from-url.js @@ -1,7 +1,8 @@ -var imageSizeCache = {}, - size = require('./image-size-from-url'), - Promise = require('bluebird'), - getImageSizeFromUrl = size.getImageSizeFromUrl; +var Promise = require('bluebird'), + size = require('./image-size-from-url'), + logging = require('../logging'), + getImageSizeFromUrl = size.getImageSizeFromUrl, + imageSizeCache = {}; /** * Get cached image size from URL @@ -18,13 +19,12 @@ function getCachedImageSizeFromUrl(url) { // image size is not in cache if (!imageSizeCache[url]) { - return getImageSizeFromUrl(url).then(function (res) { + return getImageSizeFromUrl(url, 6000).then(function (res) { imageSizeCache[url] = res; return Promise.resolve(imageSizeCache[url]); - }).catch(function () { - // @ToDo: add real error handling here as soon as we have error logging - // logger.error({err:err}); + }).catch(function (err) { + logging.error(err); // in case of error we just attach the url return Promise.resolve(imageSizeCache[url] = url); diff --git a/core/server/utils/image-size-from-url.js b/core/server/utils/image-size-from-url.js index efd0838822..a304f5736e 100644 --- a/core/server/utils/image-size-from-url.js +++ b/core/server/utils/image-size-from-url.js @@ -20,6 +20,7 @@ var sizeOf = require('image-size'), http = require('http'), https = require('https'), utils = require('../utils'), + errors = require('../errors'), dimensions, request, requestHandler; @@ -35,6 +36,9 @@ module.exports.getImageSizeFromUrl = function getImageSizeFromUrl(imagePath, tim var imageObject = {}, options; + // set default timeout if called without option. Otherwise node will use default timeout of 120 sec. + timeout = timeout ? timeout : 10000; + imageObject.url = imagePath; // check if we got an url without any protocol @@ -71,20 +75,21 @@ module.exports.getImageSizeFromUrl = function getImageSizeFromUrl(imagePath, tim return resolve(imageObject); } catch (err) { - // @ToDo: add real error handling here as soon as we have error logging - return reject(err); + return reject(new errors.InternalServerError({ + code: 'IMAGE_SIZE', + err: err, + context: imagePath + })); } } else { - // @ToDo: add real error handling here as soon as we have error logging - var err = new Error(); - err.message = imagePath; - err.statusCode = res.statusCode; - - return reject(err); + return reject(new errors.InternalServerError({ + code: 'IMAGE_SIZE', + statusCode: res.statusCode, + context: imagePath + })); } }); }).on('socket', function (socket) { - // don't set timeout if no timeout give as argument if (timeout) { socket.setTimeout(timeout); socket.on('timeout', function () { @@ -92,9 +97,11 @@ module.exports.getImageSizeFromUrl = function getImageSizeFromUrl(imagePath, tim }); } }).on('error', function (err) { - // @ToDo: add real error handling here as soon as we have error logging - - return reject(err); + return reject(new errors.InternalServerError({ + code: 'IMAGE_SIZE', + err: err, + context: imagePath + })); }); }); };