-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle NoSuchKey error gracefully #15
Conversation
if (err.name === 'NoSuchKey') { | ||
res.status(404).send('Image not found'); | ||
} else { | ||
res.status(500); | ||
next(err); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't checked it against Ghost source, but are we supposed to finalize the response in this handler by using send
? If so, should we also finalize it in 500
case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question! Generally, I would expect Ghost to handle it, but I have just done some tests which would indicate the opposite.
Once I finalize the response with the .send()
, there is no timeout.
So, the simplest solution to the timeout error would indeed be removal of the next(err)
and implementation of send()
.
In my eyes, the 500 error does, however, not accurately reflect the issue at hand – the missing key.
After thinking about this once again, the more elegant (and extendable) solution would probably be this:
} catch (err) {
switch (err.name) {
case 'NoSuchKey':
res.status(404).send('Specified key does not exist');
break;
default:
res.status(500).send('Internal server error');
}
}
Let me know what you think. Happy to adapt :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just checked Ghost's own local file storage adapter, it seems we should call next()
with the error type defined in @tryghost/errors
.
I think we should use that approach instead of finalizing the response.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your contribution, but let's do it the correct way:
we should call
next()
with the error type defined in@tryghost/errors
.
Thank you for your contribution from a while ago! I've mended this PR to use the official helper packages and it's landed in #21 with your original commit included. |
I am using Ghos3 for all Ghost instances on magicpages.co and noticed an issue a few weeks ago:
When a Ghost export (JSON) is imported into Ghost, images aren't included. Usually, Ghost would simply show a broken image, since it resolves to a 404 error. With Ghos3, however, missing images weren't locally stored and Ghos3 would look for them in the attached S3 storage.
Now, what happens if the image isn't found is that a
NoSuchKey
error is thrown – but Ghost doesn't know how to handle this. Therefore, it runs into a timeout, which temporarily makes the Ghost instances unusable.I have implemented a graceful error handling for the
NoSuchKey
error, which solves the problem and correctly resolves the image as not found.The Ghost logs before:
The logs after implementing my changes:
I am already using this successfully from my fork and think this can benefit all Ghos3 users.