Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitff07629

Browse files
committed
feat(connect): エラーハンドリングを追加
1 parent8556137 commitff07629

File tree

3 files changed

+49
-8
lines changed

3 files changed

+49
-8
lines changed

‎src/connect/connect-example.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"use strict";
2+
importerrorHandlerfrom"./errorHandler";
23
importhellofrom"./hello";
34
importnosnifffrom"./nosniff";
45
importassertfrom"assert";
@@ -7,6 +8,7 @@ import http from "http";
78
importfetchfrom"node-fetch";
89
constresponseText="response text";
910
varapp=connect();
11+
app.use(errorHandler());
1012
app.use(nosniff());
1113
app.use(hello(responseText));
1214

‎src/connect/errorHandler.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"use strict";
2+
exportdefaultfunctionerrorHandler(){
3+
returnfunction(err,req,res,next){
4+
console.error(err.stack);
5+
res.status(500).send(err.message);
6+
next();
7+
};
8+
}

‎test/connect/hello-test.js

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,45 @@
22
"use strict";
33
importassertfrom"power-assert";
44
importconnectfrom"connect";
5+
importerrorHandlerfrom"../../src/connect/errorHandler";
56
importnosnifffrom"../../src/connect/nosniff";
67
importhellofrom"../../src/connect/hello";
78
importhttpfrom"http";
89
importfetchfrom"node-fetch";
910
describe("connect",function(){
1011
varresponseText="test";
1112
varserver;
12-
before(function(done){
13-
varapp=connect();
14-
app.use(nosniff());
15-
app.use(hello(responseText));
16-
server=http.createServer(app).listen(3000,done);
17-
});
18-
after(function(){
19-
server.close();
13+
describe("errorHandler",function(){
14+
beforeEach(function(done){
15+
varapp=connect();
16+
app.use(errorHandler());
17+
app.use((req,res,next)=>{
18+
next(newError("wrong"));
19+
});
20+
server=http.createServer(app).listen(3000,done);
21+
});
22+
afterEach(function(){
23+
server&&server.close();
24+
});
25+
it("should return 500 status response",function(){
26+
returnfetch("http://localhost:3000")
27+
.then(res=>res.status)
28+
.then(status=>{
29+
assert(status,500);
30+
});
31+
});
32+
2033
});
2134
describe("hello",function(){
35+
beforeEach(function(done){
36+
varapp=connect();
37+
app.use(errorHandler());
38+
app.use(hello(responseText));
39+
server=http.createServer(app).listen(3000,done);
40+
});
41+
afterEach(function(){
42+
server&&server.close();
43+
});
2244
it("should return response text",function(){
2345
returnfetch("http://localhost:3000")
2446
.then(res=>res.text())
@@ -28,6 +50,15 @@ describe("connect", function () {
2850
});
2951
});
3052
describe("sniff",function(){
53+
beforeEach(function(done){
54+
varapp=connect();
55+
app.use(nosniff());
56+
app.use(hello(responseText));
57+
server=http.createServer(app).listen(3000,done);
58+
});
59+
afterEach(function(){
60+
server&&server.close();
61+
});
3162
it("should return response has `X-Content-Type-Options` header",function(){
3263
returnfetch("http://localhost:3000")
3364
.then(res=>{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp