Skip to content

Commit

Permalink
Removed all remaining bluebird catch predicates
Browse files Browse the repository at this point in the history
refs: TryGhost#14882

- The use of predicates is deprecated, and we're working to remove them from everywhere, so that we can remove bluebird
- This should be the final piece of the puzzle in terms of predicates, from here we can start removing bluebird without concern that a predicate somewhere will explode
- Note: some of this code is poorly tested, but the refactors are very straightforward and minimal
  • Loading branch information
ErisDS committed Aug 24, 2022
1 parent 8bcdfb8 commit 2163b55
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 83 deletions.
15 changes: 8 additions & 7 deletions ghost/core/core/server/lib/image/gravatar.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,16 @@ class Gravatar {
image: imageUrl
};
})
.catch({statusCode: 404}, function () {
return {
image: undefined
};
})
.catch(function () {
.catch(function (err) {
if (err.statusCode === 404) {
return {
image: undefined
};
}

// ignore error, just resolve with no image url
});
}
}

module.exports = Gravatar;
module.exports = Gravatar;
116 changes: 60 additions & 56 deletions ghost/core/core/server/lib/image/image-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,39 +163,41 @@ class ImageSize {
width: dimensions.width,
height: dimensions.height
};
}).catch({code: 'URL_MISSING_INVALID'}, (err) => {
return Promise.reject(new errors.InternalServerError({
message: err.message,
code: 'IMAGE_SIZE_URL',
statusCode: err.statusCode,
context: err.url || imagePath
}));
}).catch({code: 'ETIMEDOUT'}, {code: 'ESOCKETTIMEDOUT'}, {code: 'ECONNRESET'}, {statusCode: 408}, (err) => {
return Promise.reject(new errors.InternalServerError({
message: 'Request timed out.',
code: 'IMAGE_SIZE_URL',
statusCode: err.statusCode,
context: err.url || imagePath
}));
}).catch({code: 'ENOENT'}, {code: 'ENOTFOUND'}, {statusCode: 404}, (err) => {
return Promise.reject(new errors.NotFoundError({
message: 'Image not found.',
code: 'IMAGE_SIZE_URL',
statusCode: err.statusCode,
context: err.url || imagePath
}));
}).catch(function (err) {
if (errors.utils.isGhostError(err)) {
return Promise.reject(err);
}
}).catch((err) => {
if (err.code === 'URL_MISSING_INVALID') {
return Promise.reject(new errors.InternalServerError({
message: err.message,
code: 'IMAGE_SIZE_URL',
statusCode: err.statusCode,
context: err.url || imagePath
}));
} else if (err.code === 'ETIMEDOUT' || err.code === 'ESOCKETTIMEDOUT' || err.code === 'ECONNRESET' || err.statusCode === 408) {
return Promise.reject(new errors.InternalServerError({
message: 'Request timed out.',
code: 'IMAGE_SIZE_URL',
statusCode: err.statusCode,
context: err.url || imagePath
}));
} else if (err.code === 'ENOENT' || err.code === 'ENOTFOUND' || err.statusCode === 404) {
return Promise.reject(new errors.NotFoundError({
message: 'Image not found.',
code: 'IMAGE_SIZE_URL',
statusCode: err.statusCode,
context: err.url || imagePath
}));
} else {
if (errors.utils.isGhostError(err)) {
return Promise.reject(err);
}

return Promise.reject(new errors.InternalServerError({
message: 'Unknown Request error.',
code: 'IMAGE_SIZE_URL',
statusCode: err.statusCode,
context: err.url || imagePath,
err: err
}));
return Promise.reject(new errors.InternalServerError({
message: 'Unknown Request error.',
code: 'IMAGE_SIZE_URL',
statusCode: err.statusCode,
context: err.url || imagePath,
err: err
}));
}
});
}

Expand Down Expand Up @@ -237,32 +239,34 @@ class ImageSize {
height: dimensions.height
};
})
.catch({code: 'ENOENT'}, (err) => {
return Promise.reject(new errors.NotFoundError({
message: err.message,
code: 'IMAGE_SIZE_STORAGE',
err: err,
context: filePath,
errorDetails: {
originalPath: imagePath,
reqFilePath: filePath
.catch((err) => {
if (err.code === 'ENOENT') {
return Promise.reject(new errors.NotFoundError({
message: err.message,
code: 'IMAGE_SIZE_STORAGE',
err: err,
context: filePath,
errorDetails: {
originalPath: imagePath,
reqFilePath: filePath
}
}));
} else {
if (errors.utils.isGhostError(err)) {
return Promise.reject(err);
}
}));
}).catch((err) => {
if (errors.utils.isGhostError(err)) {
return Promise.reject(err);
}

return Promise.reject(new errors.InternalServerError({
message: err.message,
code: 'IMAGE_SIZE_STORAGE',
err: err,
context: filePath,
errorDetails: {
originalPath: imagePath,
reqFilePath: filePath
}
}));
return Promise.reject(new errors.InternalServerError({
message: err.message,
code: 'IMAGE_SIZE_STORAGE',
err: err,
context: filePath,
errorDetails: {
originalPath: imagePath,
reqFilePath: filePath
}
}));
}
});
}

Expand Down
3 changes: 0 additions & 3 deletions ghost/core/core/server/services/auth/passwordreset.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,6 @@ function doReset(options, tokenParts, settingsAPI) {
updatedUser.set('status', 'active');
return updatedUser.save(options);
})
.catch(errors.ValidationError, (err) => {
return Promise.reject(err);
})
.catch((err) => {
if (errors.utils.isGhostError(err)) {
return Promise.reject(err);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,25 @@ class DefaultSettingsManager {
const defaultFilePath = path.join(this.sourceFolderPath, defaultFileName);

return Promise.resolve(fs.readFile(destinationFilePath, 'utf8'))
.catch({code: 'ENOENT'}, () => {
// CASE: file doesn't exist, copy it from our defaults
return fs.copy(
defaultFilePath,
destinationFilePath
).then(() => {
debug(`'${defaultFileName}' copied to ${this.destinationFolderPath}.`);
});
}).catch((error) => {
// CASE: we might have a permission error, as we can't access the directory
throw new errors.InternalServerError({
message: tpl(messages.ensureSettings, {
path: this.destinationFolderPath
}),
err: error,
context: error.path
});
.catch((err) => {
if (err.code === 'ENOENT') {
// CASE: file doesn't exist, copy it from our defaults
return fs.copy(
defaultFilePath,
destinationFilePath
).then(() => {
debug(`'${defaultFileName}' copied to ${this.destinationFolderPath}.`);
});
} else {
// CASE: we might have a permission error, as we can't access the directory
throw new errors.InternalServerError({
message: tpl(messages.ensureSettings, {
path: this.destinationFolderPath
}),
err: err,
context: err.path
});
}
});
}
}
Expand Down

0 comments on commit 2163b55

Please sign in to comment.