0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00

Update errors across lib/image and lib/request

- swap common.errors for @tryghost/errors
- doing this in batches across small parts of the codebase to reduce disruption
This commit is contained in:
Hannah Wolfe 2020-03-25 10:25:25 +00:00
parent 72db5a67ca
commit 10ee38683d
7 changed files with 31 additions and 28 deletions

View file

@ -3,7 +3,8 @@ var sizeOf = require('image-size'),
_ = require('lodash'), _ = require('lodash'),
path = require('path'), path = require('path'),
config = require('../../config'), config = require('../../config'),
common = require('../common'), {i18n} = require('../common'),
errors = require('@tryghost/errors'),
urlUtils = require('../../lib/url-utils'), urlUtils = require('../../lib/url-utils'),
settingsCache = require('../../services/settings/cache'), settingsCache = require('../../services/settings/cache'),
storageUtils = require('../../adapters/storage/utils'), storageUtils = require('../../adapters/storage/utils'),
@ -41,8 +42,8 @@ getIconDimensions = function getIconDimensions(path) {
height: dimensions.height height: dimensions.height
}); });
} catch (err) { } catch (err) {
return reject(new common.errors.ValidationError({ return reject(new errors.ValidationError({
message: common.i18n.t('errors.utils.blogIcon.error', { message: i18n.t('errors.utils.blogIcon.error', {
file: path, file: path,
error: err.message error: err.message
}) })

View file

@ -1,6 +1,7 @@
var debug = require('ghost-ignition').debug('utils:image-size-cache'), var debug = require('ghost-ignition').debug('utils:image-size-cache'),
imageSize = require('./image-size'), imageSize = require('./image-size'),
common = require('../common'), errors = require('@tryghost/errors'),
{logging} = require('../common'),
cache = {}; cache = {};
/** /**
@ -24,7 +25,7 @@ function getCachedImageSizeFromUrl(url) {
debug('Cached image:', url); debug('Cached image:', url);
return cache[url]; return cache[url];
}).catch(common.errors.NotFoundError, function () { }).catch(errors.NotFoundError, function () {
debug('Cached image (not found):', url); debug('Cached image (not found):', url);
// in case of error we just attach the url // in case of error we just attach the url
cache[url] = url; cache[url] = url;
@ -32,7 +33,7 @@ function getCachedImageSizeFromUrl(url) {
return cache[url]; return cache[url];
}).catch(function (err) { }).catch(function (err) {
debug('Cached image (error):', url); debug('Cached image (error):', url);
common.logging.error(err); logging.error(err);
// in case of error we just attach the url // in case of error we just attach the url
cache[url] = url; cache[url] = url;

View file

@ -6,7 +6,8 @@ const Promise = require('bluebird');
const _ = require('lodash'); const _ = require('lodash');
const request = require('../request'); const request = require('../request');
const urlUtils = require('../../lib/url-utils'); const urlUtils = require('../../lib/url-utils');
const common = require('../common'); const {i18n} = require('../common');
const errors = require('@tryghost/errors');
const config = require('../../config'); const config = require('../../config');
const storage = require('../../adapters/storage'); const storage = require('../../adapters/storage');
const storageUtils = require('../../adapters/storage/utils'); const storageUtils = require('../../adapters/storage/utils');
@ -54,7 +55,7 @@ function _probeImageSizeFromUrl(url) {
// probe-image-size uses `request` npm module which doesn't have our `got` // probe-image-size uses `request` npm module which doesn't have our `got`
// override with custom URL validation so it needs duplicating here // override with custom URL validation so it needs duplicating here
if (_.isEmpty(url) || !validator.isURL(url)) { if (_.isEmpty(url) || !validator.isURL(url)) {
return Promise.reject(new common.errors.InternalServerError({ return Promise.reject(new errors.InternalServerError({
message: 'URL empty or invalid.', message: 'URL empty or invalid.',
code: 'URL_MISSING_INVALID', code: 'URL_MISSING_INVALID',
context: url context: url
@ -147,32 +148,32 @@ const getImageSizeFromUrl = (imagePath) => {
height: dimensions.height height: dimensions.height
}; };
}).catch({code: 'URL_MISSING_INVALID'}, (err) => { }).catch({code: 'URL_MISSING_INVALID'}, (err) => {
return Promise.reject(new common.errors.InternalServerError({ return Promise.reject(new errors.InternalServerError({
message: err.message, message: err.message,
code: 'IMAGE_SIZE_URL', code: 'IMAGE_SIZE_URL',
statusCode: err.statusCode, statusCode: err.statusCode,
context: err.url || imagePath context: err.url || imagePath
})); }));
}).catch({code: 'ETIMEDOUT'}, {statusCode: 408}, (err) => { }).catch({code: 'ETIMEDOUT'}, {statusCode: 408}, (err) => {
return Promise.reject(new common.errors.InternalServerError({ return Promise.reject(new errors.InternalServerError({
message: 'Request timed out.', message: 'Request timed out.',
code: 'IMAGE_SIZE_URL', code: 'IMAGE_SIZE_URL',
statusCode: err.statusCode, statusCode: err.statusCode,
context: err.url || imagePath context: err.url || imagePath
})); }));
}).catch({code: 'ENOENT'}, {statusCode: 404}, (err) => { }).catch({code: 'ENOENT'}, {statusCode: 404}, (err) => {
return Promise.reject(new common.errors.NotFoundError({ return Promise.reject(new errors.NotFoundError({
message: 'Image not found.', message: 'Image not found.',
code: 'IMAGE_SIZE_URL', code: 'IMAGE_SIZE_URL',
statusCode: err.statusCode, statusCode: err.statusCode,
context: err.url || imagePath context: err.url || imagePath
})); }));
}).catch(function (err) { }).catch(function (err) {
if (common.errors.utils.isIgnitionError(err)) { if (errors.utils.isIgnitionError(err)) {
return Promise.reject(err); return Promise.reject(err);
} }
return Promise.reject(new common.errors.InternalServerError({ return Promise.reject(new errors.InternalServerError({
message: 'Unknown Request error.', message: 'Unknown Request error.',
code: 'IMAGE_SIZE_URL', code: 'IMAGE_SIZE_URL',
statusCode: err.statusCode, statusCode: err.statusCode,
@ -221,7 +222,7 @@ const getImageSizeFromStoragePath = (imagePath) => {
}; };
}) })
.catch({code: 'ENOENT'}, (err) => { .catch({code: 'ENOENT'}, (err) => {
return Promise.reject(new common.errors.NotFoundError({ return Promise.reject(new errors.NotFoundError({
message: err.message, message: err.message,
code: 'IMAGE_SIZE_STORAGE', code: 'IMAGE_SIZE_STORAGE',
err: err, err: err,
@ -232,11 +233,11 @@ const getImageSizeFromStoragePath = (imagePath) => {
} }
})); }));
}).catch((err) => { }).catch((err) => {
if (common.errors.utils.isIgnitionError(err)) { if (errors.utils.isIgnitionError(err)) {
return Promise.reject(err); return Promise.reject(err);
} }
return Promise.reject(new common.errors.InternalServerError({ return Promise.reject(new errors.InternalServerError({
message: err.message, message: err.message,
code: 'IMAGE_SIZE_STORAGE', code: 'IMAGE_SIZE_STORAGE',
err: err, err: err,
@ -279,8 +280,8 @@ const getImageSizeFromPath = (path) => {
height: dimensions.height height: dimensions.height
}); });
} catch (err) { } catch (err) {
return reject(new common.errors.ValidationError({ return reject(new errors.ValidationError({
message: common.i18n.t('errors.utils.images.invalidDimensions', { message: i18n.t('errors.utils.images.invalidDimensions', {
file: path, file: path,
error: err.message error: err.message
}) })

View file

@ -1,5 +1,5 @@
const Promise = require('bluebird'); const Promise = require('bluebird');
const common = require('../common'); const errors = require('@tryghost/errors');
const fs = require('fs-extra'); const fs = require('fs-extra');
/** /**
@ -46,14 +46,14 @@ const makeSafe = fn => (...args) => {
try { try {
require('sharp'); require('sharp');
} catch (err) { } catch (err) {
return Promise.reject(new common.errors.InternalServerError({ return Promise.reject(new errors.InternalServerError({
message: 'Sharp wasn\'t installed', message: 'Sharp wasn\'t installed',
code: 'SHARP_INSTALLATION', code: 'SHARP_INSTALLATION',
err: err err: err
})); }));
} }
return fn(...args).catch((err) => { return fn(...args).catch((err) => {
throw new common.errors.InternalServerError({ throw new errors.InternalServerError({
message: 'Unable to manipulate image.', message: 'Unable to manipulate image.',
err: err, err: err,
code: 'IMAGE_PROCESSING' code: 'IMAGE_PROCESSING'

View file

@ -1,7 +1,7 @@
var got = require('got'), var got = require('got'),
_ = require('lodash'), _ = require('lodash'),
validator = require('../data/validation').validator, validator = require('../data/validation').validator,
common = require('./common'), errors = require('@tryghost/errors'),
ghostVersion = require('./ghost-version'); ghostVersion = require('./ghost-version');
var defaultOptions = { var defaultOptions = {
@ -12,7 +12,7 @@ var defaultOptions = {
module.exports = function request(url, options) { module.exports = function request(url, options) {
if (_.isEmpty(url) || !validator.isURL(url)) { if (_.isEmpty(url) || !validator.isURL(url)) {
return Promise.reject(new common.errors.InternalServerError({ return Promise.reject(new errors.InternalServerError({
message: 'URL empty or invalid.', message: 'URL empty or invalid.',
code: 'URL_MISSING_INVALID', code: 'URL_MISSING_INVALID',
context: url context: url

View file

@ -5,7 +5,7 @@ const should = require('should'),
path = require('path'), path = require('path'),
configUtils = require('../../../utils/configUtils'), configUtils = require('../../../utils/configUtils'),
urlUtils = require('../../../../server/lib/url-utils'), urlUtils = require('../../../../server/lib/url-utils'),
common = require('../../../../server/lib/common'), errors = require('@tryghost/errors'),
storage = require('../../../../server/adapters/storage'); storage = require('../../../../server/adapters/storage');
describe('lib/image: image size', function () { describe('lib/image: image size', function () {
@ -508,7 +508,7 @@ describe('lib/image: image size', function () {
imageSize.getImageSizeFromStoragePath(url) imageSize.getImageSizeFromStoragePath(url)
.catch(function (err) { .catch(function (err) {
should.exist(err); should.exist(err);
(err instanceof common.errors.NotFoundError).should.eql(true); (err instanceof errors.NotFoundError).should.eql(true);
done(); done();
}); });
}); });

View file

@ -1,7 +1,7 @@
const should = require('should'); const should = require('should');
const sinon = require('sinon'); const sinon = require('sinon');
const fs = require('fs-extra'); const fs = require('fs-extra');
const common = require('../../../../server/lib/common'); const errors = require('@tryghost/errors');
const manipulator = require('../../../../server/lib/image/manipulator'); const manipulator = require('../../../../server/lib/image/manipulator');
const testUtils = require('../../../utils'); const testUtils = require('../../../utils');
@ -110,7 +110,7 @@ describe('lib/image: manipulator', function () {
'1'.should.eql(1, 'Expected to fail'); '1'.should.eql(1, 'Expected to fail');
}) })
.catch((err) => { .catch((err) => {
(err instanceof common.errors.InternalServerError).should.be.true; (err instanceof errors.InternalServerError).should.be.true;
err.code.should.eql('IMAGE_PROCESSING'); err.code.should.eql('IMAGE_PROCESSING');
}); });
}); });
@ -127,7 +127,7 @@ describe('lib/image: manipulator', function () {
'1'.should.eql(1, 'Expected to fail'); '1'.should.eql(1, 'Expected to fail');
}) })
.catch((err) => { .catch((err) => {
(err instanceof common.errors.InternalServerError).should.be.true(); (err instanceof errors.InternalServerError).should.be.true();
err.code.should.eql('SHARP_INSTALLATION'); err.code.should.eql('SHARP_INSTALLATION');
}); });
}); });