Skip to content

Commit 81f30f0

Browse files
committed
iinit
0 parents  commit 81f30f0

File tree

10 files changed

+609
-0
lines changed

10 files changed

+609
-0
lines changed

.gitignore

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Compiled source #
2+
###################
3+
*.com
4+
*.class
5+
*.dll
6+
*.exe
7+
*.o
8+
*.so
9+
10+
# Packages #
11+
############
12+
# it's better to unpack these files and commit the raw source
13+
# git has its own built in compression methods
14+
*.7z
15+
*.dmg
16+
*.gz
17+
*.iso
18+
*.jar
19+
*.rar
20+
*.tar
21+
*.zip
22+
23+
# Logs and databases #
24+
######################
25+
*.log
26+
*.sql
27+
*.sqlite
28+
29+
# OS generated files #
30+
######################
31+
.DS_Store*
32+
ehthumbs.db
33+
Icon?
34+
Thumbs.db
35+
36+
# Node.js #
37+
###########
38+
lib-cov
39+
*.seed
40+
*.log
41+
*.csv
42+
*.dat
43+
*.out
44+
*.pid
45+
*.gz
46+
47+
pids
48+
logs
49+
results
50+
51+
node_modules
52+
npm-debug.log
53+
54+
# Git #
55+
#######
56+
*.orig
57+
*.BASE.*
58+
*.BACKUP.*
59+
*.LOCAL.*
60+
*.REMOTE.*
61+
62+
# Components #
63+
##############
64+
65+
/build
66+
/components

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_js:
2+
- "0.10"
3+
- "0.11"
4+
language: node_js

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
test:
2+
@NODE_ENV=test ./node_modules/.bin/mocha \
3+
--reporter spec \
4+
--require should
5+
6+
.PHONY: test

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Error Handler
2+
3+
Previously `connect.errorHandler()`.
4+
5+
Usage:
6+
7+
```js
8+
var app = require('connect');
9+
app.use(require('errorhandler')())
10+
```
11+
12+
## License
13+
14+
The MIT License (MIT)
15+
16+
Copyright (c) 2014 Jonathan Ong [email protected]
17+
18+
Permission is hereby granted, free of charge, to any person obtaining a copy
19+
of this software and associated documentation files (the "Software"), to deal
20+
in the Software without restriction, including without limitation the rights
21+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
22+
copies of the Software, and to permit persons to whom the Software is
23+
furnished to do so, subject to the following conditions:
24+
25+
The above copyright notice and this permission notice shall be included in
26+
all copies or substantial portions of the Software.
27+
28+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
31+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
33+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
34+
THE SOFTWARE.

index.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*!
2+
* Connect - errorHandler
3+
* Copyright(c) 2010 Sencha Inc.
4+
* Copyright(c) 2011 TJ Holowaychuk
5+
* MIT Licensed
6+
*/
7+
8+
/**
9+
* Module dependencies.
10+
*/
11+
12+
var fs;
13+
try {
14+
fs = require('graceful-fs');
15+
} catch (_) {
16+
fs = require('fs');
17+
}
18+
19+
// environment
20+
21+
var env = process.env.NODE_ENV || 'development';
22+
23+
/**
24+
* Error handler:
25+
*
26+
* Development error handler, providing stack traces
27+
* and error message responses for requests accepting text, html,
28+
* or json.
29+
*
30+
* Text:
31+
*
32+
* By default, and when _text/plain_ is accepted a simple stack trace
33+
* or error message will be returned.
34+
*
35+
* JSON:
36+
*
37+
* When _application/json_ is accepted, connect will respond with
38+
* an object in the form of `{ "error": error }`.
39+
*
40+
* HTML:
41+
*
42+
* When accepted connect will output a nice html stack trace.
43+
*
44+
* @return {Function}
45+
* @api public
46+
*/
47+
48+
exports = module.exports = function errorHandler(){
49+
return function errorHandler(err, req, res, next){
50+
if (err.status) res.statusCode = err.status;
51+
if (res.statusCode < 400) res.statusCode = 500;
52+
if ('test' != env) console.error(err.stack);
53+
var accept = req.headers.accept || '';
54+
// html
55+
if (~accept.indexOf('html')) {
56+
fs.readFile(__dirname + '/public/style.css', 'utf8', function(e, style){
57+
fs.readFile(__dirname + '/public/error.html', 'utf8', function(e, html){
58+
var stack = (err.stack || '')
59+
.split('\n').slice(1)
60+
.map(function(v){ return '<li>' + v + '</li>'; }).join('');
61+
html = html
62+
.replace('{style}', style)
63+
.replace('{stack}', stack)
64+
.replace('{title}', exports.title)
65+
.replace('{statusCode}', res.statusCode)
66+
.replace(/\{error\}/g, escapeHTML(err.toString().replace(/\n/g, '<br/>')));
67+
res.setHeader('Content-Type', 'text/html; charset=utf-8');
68+
res.end(html);
69+
});
70+
});
71+
// json
72+
} else if (~accept.indexOf('json')) {
73+
var error = { message: err.message, stack: err.stack };
74+
for (var prop in err) error[prop] = err[prop];
75+
var json = JSON.stringify({ error: error });
76+
res.setHeader('Content-Type', 'application/json');
77+
res.end(json);
78+
// plain text
79+
} else {
80+
res.setHeader('Content-Type', 'text/plain');
81+
res.end(err.stack);
82+
}
83+
};
84+
};
85+
86+
/**
87+
* Template title, framework authors may override this value.
88+
*/
89+
90+
exports.title = 'Connect';
91+
92+
93+
/**
94+
* Escape the given string of `html`.
95+
*
96+
* @param {String} html
97+
* @return {String}
98+
* @api private
99+
*/
100+
101+
function escapeHTML(html){
102+
return String(html)
103+
.replace(/&(?!\w+;)/g, '&amp;')
104+
.replace(/</g, '&lt;')
105+
.replace(/>/g, '&gt;')
106+
.replace(/"/g, '&quot;');
107+
};

package.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "errorhandler",
3+
"description": "connect's default error handler page",
4+
"version": "1.0.0",
5+
"author": {
6+
"name": "Jonathan Ong",
7+
"email": "[email protected]",
8+
"url": "http://jongleberry.com",
9+
"twitter": "https://twitter.com/jongleberry"
10+
},
11+
"license": "MIT",
12+
"repository": {
13+
"type": "git",
14+
"url": "https://github.com/expressjs/errorhandler.git"
15+
},
16+
"bugs": {
17+
"mail": "[email protected]",
18+
"url": "https://github.com/expressjs/errorhandler/issues"
19+
},
20+
"devDependencies": {
21+
"mocha": "^1.17.0",
22+
"should": "^3.0.0",
23+
"supertest": "*",
24+
"connect": "*"
25+
},
26+
"scripts": {
27+
"test": "make test"
28+
}
29+
}

public/error.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<html>
2+
<head>
3+
<meta charset='utf-8'>
4+
<title>{error}</title>
5+
<style>{style}</style>
6+
</head>
7+
<body>
8+
<div id="wrapper">
9+
<h1>{title}</h1>
10+
<h2><em>{statusCode}</em> {error}</h2>
11+
<ul id="stacktrace">{stack}</ul>
12+
</div>
13+
</body>
14+
</html>

0 commit comments

Comments
 (0)