UNPKG

waffle

Version:

シンプルなWEBアプリケーションフレームワークです。(ALL YOUR NODE ARE BELONG TO US)

72 lines (64 loc) 2.06 kB
/* * Copyright 2012 Katsunori Koyanagi * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ /** * @overview デフォルトのエラーページ用のコントローラです。 */ "use strict"; var Lang = require("../utils/Lang"); var ejs = require("ejs"); var fs = require("fs"); var statusCodes = require("http").STATUS_CODES; var path = fs.join(__dirname, "../assets/error.ejs"); var template = fs.readFileSync(path, "UTF-8"); var compiled = ejs.compile(template, { filename : path }); /** * 全てのエラーをフックして標準のエラーページのHTMLを出力するコントローラです。 * * @example <code> * var waffle = require("waffle"); * config.router.on(waffle.entensions.defaultError); * </code> * @name Extensions#defaultError * @type Function */ function controller(context) { var title = context.res.statusCode + " " + statusCodes[context.res.statusCode]; var error = context.errorInfo; var stack = Lang.isError(error) ? error.stack : ""; var message = Lang.isError(error) ? error.message : error ? String(error) : title; var locals = { title : title, stack : stack, message : message }; var buf = new Buffer(compiled.call(context, locals)); context.res.setHeader("Content-Length", buf.length); context.res.write(buf); context.complete(); } // // expose // module.exports = function(parameter, context) { if (Lang.isNumber(parameter) && context.errorInfo != null) { return controller; } return null; };