Skip to content

Commit

Permalink
Fall back to HTML format errors if no suitable format is found
Browse files Browse the repository at this point in the history
  • Loading branch information
mde committed Sep 12, 2013
1 parent 3c9acc6 commit 791a105
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
22 changes: 20 additions & 2 deletions lib/controller/error_controller.js
Expand Up @@ -22,10 +22,27 @@ ErrorController.prototype = new BaseController();
ErrorController.prototype.constructor = ErrorController;

ErrorController.prototype._respond = ErrorController.prototype.respond;
ErrorController.prototype.__doContentNegotiation =
ErrorController.prototype._doContentNegotiation;
ErrorController.prototype._setTemplateAndLayout =
ErrorController.prototype.setTemplateAndLayout;
utils.mixin(ErrorController.prototype, new (function () {

// Override, need to fall back to HTML if no supported format
this._doContentNegotiation = function () {
var negotiated = this.__doContentNegotiation.apply(this, arguments);
// Return an HTML response for anybody who doesn't want something
// known and specific
if (!negotiated.format) {
negotiated = {
format: 'html'
, contentType: 'text/html'
};
}
return negotiated;
};

// Override, need to figure out the action based on what type of error
this.respond = function (err, opts) {
this.params.action = this._getAction(err);
// Include requested path on 404 message
Expand All @@ -38,13 +55,14 @@ utils.mixin(ErrorController.prototype, new (function () {
this._respond(err, opts);
};

// Override, fall back to errors/default view if no action-specific
// view found
this.setTemplateAndLayout = function (opts) {
var controllerFilename;
this._setTemplateAndLayout(opts);
// Errors will fall back to a single default error view
if (!geddy.templateRegistry[this.template]) {
controllerFilename = utils.string.getInflection(this.name, 'filename', 'plural')
this.template = 'app/views/' + controllerFilename + '/default'
this.template = 'app/views/errors/default'
}
};

Expand Down
7 changes: 2 additions & 5 deletions lib/controller/responder/negotiator.js
Expand Up @@ -25,9 +25,9 @@ Negotiator.prototype = new (function () {
calculateFormatAndContentType = function (formats, accepts) {
var format
, contentType
, match
, match;
// See if any format formats match the accept header
if (formats.length) {
if (formats && formats.length) {
// No accepts-header, or agent accepts wildcard
// Respond with controller's first choice
if (!accepts || accepts.indexOf('*/*') > -1) {
Expand Down Expand Up @@ -87,9 +87,6 @@ Negotiator.prototype = new (function () {
// Try to calculate a format/contentType that's acceptable to all parties
// ===
result = calculateFormatAndContentType(formats, accepts);
if (!result) {
throw new errors.InternalServerError('No valid format and content-type found.');
}

return result;
};
Expand Down

0 comments on commit 791a105

Please sign in to comment.