Merge pull request #2036 from thelounge/xpaw/prefetch-errors

Report server errors when preview fetch fails
This commit is contained in:
Jérémie Astori 2018-02-20 22:21:39 -05:00 committed by GitHub
commit 0602a9495a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 4 deletions

View File

@ -1365,6 +1365,10 @@ kbd {
display: block;
}
#chat .prefetch-error {
display: none;
}
#chat .toggle-content .thumb {
max-width: 48px;
max-height: 38px;

View File

@ -37,6 +37,9 @@
to open it in a new window.
</em>
{{/equal}}
{{#equal error "message"}}
<em>There was an error when loading this image. <small class="prefetch-error">({{message}})</small></em>
{{/equal}}
{{/equal}}
</div>
{{/preview}}

View File

@ -38,7 +38,14 @@ module.exports = function(client, chan, msg) {
})).slice(0, 5); // Only preview the first 5 URLs in message to avoid abuse
msg.previews.forEach((preview) => {
fetch(preview.link, function(res) {
fetch(preview.link, function(res, err) {
if (err) {
preview.type = "error";
preview.error = "message";
preview.message = err.message;
handlePreview(client, msg, preview, res);
}
if (res === null) {
return;
}
@ -204,7 +211,7 @@ function fetch(uri, cb) {
},
});
} catch (e) {
return cb(null);
return cb(null, e);
}
const buffers = [];
@ -229,7 +236,7 @@ function fetch(uri, cb) {
limit = 1024 * 50;
}
})
.on("error", () => cb(null))
.on("error", (e) => cb(null, e))
.on("data", (data) => {
length += data.length;
buffers.push(data);
@ -240,7 +247,7 @@ function fetch(uri, cb) {
})
.on("end", () => {
if (req.response.statusCode < 200 || req.response.statusCode > 299) {
return cb(null);
return cb(null, new Error(`HTTP ${req.response.statusCode}`));
}
let type = "";