Skip to content

Commit

Permalink
core: new error with messages
Browse files Browse the repository at this point in the history
  • Loading branch information
nleush committed Oct 24, 2017
1 parent deeb256 commit 208bead
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 31 deletions.
40 changes: 23 additions & 17 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ function logErrors(err, req, res, next) {
next(err);
}

var errors = [401, 403, 408];

function respondWithError(req, res, code, msg) {
function respondWithError(req, res, code, msg, messages) {
var err = {
error: {
source: 'iframely',
Expand All @@ -72,6 +70,10 @@ function respondWithError(req, res, code, msg) {
}
};

if (messages) {
err.error.messages = messages;
}

var ttl;
if (code === 404) {
ttl = CONFIG.CACHE_TTL_PAGE_404;
Expand Down Expand Up @@ -104,41 +106,45 @@ function respondWithError(req, res, code, msg) {
}
}

var proxyErrorCodes = [401, 403, 408];

function errorHandler(err, req, res, next) {
if (err instanceof NotFound) {
respondWithError(req, res, 404, err.message);
respondWithError(req, res, 404, err.message, err.messages);
} else {
var code = err.code || 500;
errors.map(function(e) {
proxyErrorCodes.map(function(e) {
if (err.message.indexOf(e) > - 1) {
code = e;
}
});

var message = 'Server error';

if (err.message.indexOf('timeout') > -1) {
respondWithError(req, res, 408, 'Timeout');
message = 'Timeout';
code = 408;
}
else if (code === 401) {
respondWithError(req, res, 401, 'Unauthorized');
message = 'Unauthorized';
}
else if (code === 403) {
respondWithError(req, res, 403, 'Forbidden');
message = 'Forbidden';
}
else if (code === 404) {
respondWithError(req, res, 404, 'Not found');
}
message = 'Not found';
}
else if (code === 410) {
respondWithError(req, res, 410, 'Gone');
message = 'Gone';
}
else if (code === 415) {
respondWithError(req, res, 415, 'Unsupported Media Type');
}
message = 'Unsupported Media Type';
}
else if (code === 417) {
respondWithError(req, res, 417, 'Unsupported Media Type');
}
else {
respondWithError(req, res, code, 'Server error');
message = 'Unsupported Media Type';
}

respondWithError(req, res, code, message, err.messages);
}
}

Expand Down
45 changes: 37 additions & 8 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,21 @@
}
}

function getResultErrorMessages(result, allResults) {

if (!result) {
return false;
}

for(var i = 0; i < result.length; i++) {
var r = result[i];
if (r.error) {
// Get message from plugin error.
useMessage(allResults, r.error);
}
}
}

function useResult(usedMethods, context, pluginsContexts, allResults, result, options, asyncMethodCb) {

if (!result) {
Expand All @@ -818,10 +833,6 @@
usedPluginMethods[method.name] = usedPluginMethods[method.name] - 1;

if (r.error) {

// Get message from plugin error.
useMessage(allResults, r.error);

// Too much error will be in log.
//console.error(" -- Plugin error", method.pluginId, method.name, result.error);
}
Expand Down Expand Up @@ -1417,9 +1428,11 @@
return;
}

if (result) {
//console.log(' - call result:', error, result && result.map(function(r) {return r.method.pluginId;}).join(', '));
}
//if (result) {
// console.log(' - call result:', error, result && result.map(function(r) {return r.method.pluginId;}).join(', '));
//}

getResultErrorMessages(result, allResults);

// Find redirect command.
var redirect = findRedirectError(result);
Expand All @@ -1441,7 +1454,23 @@
if (errorResponseCode) {
abortCurrentRequest();
aborted = true;
return cb(errorResponseCode);

// TODO: better check?
if (options.v === '1.3') {

var error = {
responseCode: errorResponseCode
};

if (allResults.messages.length) {
error.messages = allResults.messages;
}

return cb(error);

} else {
return cb(errorResponseCode);
}
}

// Gather results.
Expand Down
14 changes: 10 additions & 4 deletions modules/api/views.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ function getIntParam(req, param) {

function handleIframelyError(error, res, next) {

if (error == 404 || error.code == 'ENOTFOUND') {
return next(new utils.NotFound('Page not found'));
var responseCode = error.responseCode;

if (responseCode === 404) {
return next(new utils.NotFound('Page not found', error.messages));
}

var code = (typeof error !== "number" || error >= 500) ? 417 : error;
next(new utils.HttpError(code, "Requested page error: " + error));
var code = (typeof responseCode !== "number" || responseCode >= 500) ? 417 : responseCode;
next(new utils.HttpError(code, "Requested page error: " + responseCode || error, error.messages));
}

module.exports = function(app) {
Expand All @@ -73,6 +75,7 @@ module.exports = function(app) {
function(cb) {

iframelyCore.run(uri, {
v: '1.3',
debug: getBooleanParam(req, 'debug'),
mixAllWithDomainPlugin: getBooleanParam(req, 'mixAllWithDomainPlugin'),
forceParams: req.query.meta === "true" ? ["meta", "oembed"] : null,
Expand Down Expand Up @@ -210,6 +213,7 @@ module.exports = function(app) {
cache.withCache('html:' + version + ':' + uri, function(cb) {

iframelyCore.run(uri, {
v: '1.3',
getWhitelistRecord: whitelist.findWhitelistRecordFor,
readability: true
}, function(error, data) {
Expand Down Expand Up @@ -267,6 +271,7 @@ module.exports = function(app) {
cache.withCache('render_link:' + version + ':' + uri, function(cb) {

iframelyCore.run(uri, {
v: '1.3',
getWhitelistRecord: whitelist.findWhitelistRecordFor
}, function(error, result) {

Expand Down Expand Up @@ -373,6 +378,7 @@ module.exports = function(app) {
function(cb) {

iframelyCore.run(uri, {
v: '1.3',
getWhitelistRecord: whitelist.findWhitelistRecordFor,
filterNonSSL: getBooleanParam(req, 'ssl'),
filterNonHTML5: getBooleanParam(req, 'html5'),
Expand Down
6 changes: 4 additions & 2 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
var whitelist = require('./lib/whitelist');
var pluginLoader = require('./lib/loader/pluginLoader');

function NotFound(message) {
function NotFound(message, messages) {

if (typeof message === 'object') {
this.meta = message;
Expand All @@ -27,20 +27,22 @@

this.name = this.constructor.name; //set our function’s name as error name.
this.message = message; //set the error message
this.messages = messages; //additional messages

}

NotFound.prototype.__proto__ = Error.prototype;

exports.NotFound = NotFound;

function HttpError(code, message) {
function HttpError(code, message, messages) {

Error.call(this); //super constructor
Error.captureStackTrace(this, this.constructor); //super helper method to include stack trace in error object

this.name = this.constructor.name; //set our function’s name as error name.
this.message = message; //set the error message
this.messages = messages; //additional messages
this.code = code; //set the error code

}
Expand Down

0 comments on commit 208bead

Please sign in to comment.