From 37c61e2413b78fa49c11f1a12ff60be0ff9bbe52 Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Mon, 5 Feb 2018 10:46:05 +0200 Subject: [PATCH] Report server errors when preview fetch fails --- client/css/style.css | 4 ++++ client/views/msg_preview.tpl | 3 +++ src/plugins/irc-events/link.js | 15 +++++++++++---- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/client/css/style.css b/client/css/style.css index 68ce4c4c..061a0b97 100644 --- a/client/css/style.css +++ b/client/css/style.css @@ -1365,6 +1365,10 @@ kbd { display: block; } +#chat .prefetch-error { + display: none; +} + #chat .toggle-content .thumb { max-width: 48px; max-height: 38px; diff --git a/client/views/msg_preview.tpl b/client/views/msg_preview.tpl index 51e199de..12240a8a 100644 --- a/client/views/msg_preview.tpl +++ b/client/views/msg_preview.tpl @@ -37,6 +37,9 @@ to open it in a new window. {{/equal}} + {{#equal error "message"}} + There was an error when loading this image. ({{message}}) + {{/equal}} {{/equal}} {{/preview}} diff --git a/src/plugins/irc-events/link.js b/src/plugins/irc-events/link.js index e0c738ad..037b769b 100644 --- a/src/plugins/irc-events/link.js +++ b/src/plugins/irc-events/link.js @@ -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 = "";