Skip to content

Commit 9153587

Browse files
authoredJul 14, 2020
feat: remove re-routing from handler (#1847)
BREAKING CHANGE: deprecates and removes re-routing when passing a string parameter to `next()`
1 parent 71ac3a0 commit 9153587

File tree

3 files changed

+13
-228
lines changed

3 files changed

+13
-228
lines changed
 

‎lib/chain.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,13 @@ function call(handler, err, req, res, _next) {
169169
function resolve(value) {
170170
if (value && req.log) {
171171
// logs resolved value
172-
req.log.debug({ value }, 'Async handler resolved with a value');
172+
req.log.warn(
173+
{ value },
174+
'Discarded returned value from async handler'
175+
);
173176
}
174177

175-
return next(value);
178+
return next();
176179
}
177180

178181
function reject(error) {

‎lib/server.js

-21
Original file line numberDiff line numberDiff line change
@@ -1242,27 +1242,6 @@ Server.prototype._onHandlerError = function _onHandlerError(
12421242
) {
12431243
var self = this;
12441244

1245-
// Call route by name
1246-
if (!isUncaught && _.isString(err)) {
1247-
var routeName = err;
1248-
var routeHandler = self.router.lookupByName(routeName, req, res);
1249-
1250-
// Cannot find route by name, called when next('route-name') doesn't
1251-
// find any route, it's a 5xx error as it's a programatic error
1252-
if (!routeHandler) {
1253-
var routeByNameErr = new customErrorTypes.RouteMissingError(
1254-
"Route by name doesn't exist"
1255-
);
1256-
routeByNameErr.code = 'ENOEXIST';
1257-
self._afterRoute(routeByNameErr, req, res);
1258-
return;
1259-
}
1260-
routeHandler(req, res, function afterRouter(routeErr) {
1261-
self._afterRoute(routeErr, req, res);
1262-
});
1263-
return;
1264-
}
1265-
12661245
// Handlers don't continue when error happen
12671246
res._handlersFinished = true;
12681247

‎test/server.test.js

+8-205
Original file line numberDiff line numberDiff line change
@@ -884,93 +884,6 @@ test('GH #704: Route with an invalid RegExp params', function(t) {
884884
});
885885
});
886886

887-
test('gh-193 basic', function(t) {
888-
SERVER.get(
889-
{
890-
name: 'foo',
891-
path: '/foo'
892-
},
893-
function(req, res, next) {
894-
next('bar');
895-
}
896-
);
897-
898-
SERVER.get(
899-
{
900-
name: 'bar',
901-
path: '/bar'
902-
},
903-
function(req, res, next) {
904-
res.send(200);
905-
next();
906-
}
907-
);
908-
909-
CLIENT.get('/foo', function(err, _, res) {
910-
t.ifError(err);
911-
t.equal(res.statusCode, 200);
912-
t.end();
913-
});
914-
});
915-
916-
test('gh-193 route name normalization', function(t) {
917-
SERVER.get(
918-
{
919-
name: 'foo',
920-
path: '/foo'
921-
},
922-
function(req, res, next) {
923-
next('b-a-r');
924-
}
925-
);
926-
927-
SERVER.get(
928-
{
929-
name: 'b-a-r',
930-
path: '/bar'
931-
},
932-
function(req, res, next) {
933-
res.send(200);
934-
next();
935-
}
936-
);
937-
938-
CLIENT.get('/foo', function(err, _, res) {
939-
t.ifError(err);
940-
t.equal(res.statusCode, 200);
941-
t.end();
942-
});
943-
});
944-
945-
test('gh-193 route ENOEXIST', function(t) {
946-
SERVER.get(
947-
{
948-
name: 'foo',
949-
path: '/foo'
950-
},
951-
function(req, res, next) {
952-
next('baz');
953-
}
954-
);
955-
956-
SERVER.get(
957-
{
958-
name: 'bar',
959-
path: '/bar'
960-
},
961-
function(req, res, next) {
962-
res.send(200);
963-
next();
964-
}
965-
);
966-
967-
CLIENT.get('/foo', function(err, _, res) {
968-
t.ok(err);
969-
t.equal(res.statusCode, 500);
970-
t.end();
971-
});
972-
});
973-
974887
test('run param only with existing req.params', function(t) {
975888
var count = 0;
976889

@@ -1023,91 +936,7 @@ test('run param only with existing req.params', function(t) {
1023936
});
1024937
});
1025938

1026-
test('gh-193 route only run use once', function(t) {
1027-
var count = 0;
1028-
1029-
SERVER.use(function(req, res, next) {
1030-
count++;
1031-
next();
1032-
});
1033-
1034-
SERVER.get(
1035-
{
1036-
name: 'foo',
1037-
path: '/foo'
1038-
},
1039-
function(req, res, next) {
1040-
next('bar');
1041-
}
1042-
);
1043-
1044-
SERVER.get(
1045-
{
1046-
name: 'bar',
1047-
path: '/bar'
1048-
},
1049-
function(req, res, next) {
1050-
res.send(200);
1051-
next();
1052-
}
1053-
);
1054-
1055-
CLIENT.get('/foo', function(err, _, res) {
1056-
t.ifError(err);
1057-
t.equal(res.statusCode, 200);
1058-
t.equal(count, 1);
1059-
t.end();
1060-
});
1061-
});
1062-
1063-
test('gh-193 route chained', function(t) {
1064-
var count = 0;
1065-
1066-
SERVER.use(function addCounter(req, res, next) {
1067-
count++;
1068-
next();
1069-
});
1070-
1071-
SERVER.get(
1072-
{
1073-
name: 'foo',
1074-
path: '/foo'
1075-
},
1076-
function getFoo(req, res, next) {
1077-
next('bar');
1078-
}
1079-
);
1080-
1081-
SERVER.get(
1082-
{
1083-
name: 'bar',
1084-
path: '/bar'
1085-
},
1086-
function getBar(req, res, next) {
1087-
next('baz');
1088-
}
1089-
);
1090-
1091-
SERVER.get(
1092-
{
1093-
name: 'baz',
1094-
path: '/baz'
1095-
},
1096-
function getBaz(req, res, next) {
1097-
res.send(200);
1098-
next();
1099-
}
1100-
);
1101-
1102-
CLIENT.get('/foo', function(err, _, res) {
1103-
t.ifError(err);
1104-
t.equal(res.statusCode, 200);
1105-
t.equal(count, 1);
1106-
t.end();
1107-
});
1108-
});
1109-
1110-
test('gh-193 route params basic', function(t) {
939+
test('next("string") returns InternalServer', function(t) {
1111940
var count = 0;
1112941

1113942
SERVER.use(function(req, res, next) {
@@ -1126,27 +955,15 @@ test('gh-193 route params basic', function(t) {
1126955
}
1127956
);
1128957

1129-
SERVER.get(
1130-
{
1131-
name: 'bar',
1132-
path: '/bar/:baz'
1133-
},
1134-
function(req, res, next) {
1135-
t.notOk(req.params.baz);
1136-
res.send(200);
1137-
next();
1138-
}
1139-
);
1140-
1141958
CLIENT.get('/foo/blah', function(err, _, res) {
1142-
t.ifError(err);
1143-
t.equal(res.statusCode, 200);
959+
t.ok(err);
960+
t.equal(res.statusCode, 500);
1144961
t.equal(count, 1);
1145962
t.end();
1146963
});
1147964
});
1148965

1149-
test('gh-193 next("route") from a use plugin', function(t) {
966+
test('next("string") from a use plugin returns InternalServer', function(t) {
1150967
var count = 0;
1151968

1152969
SERVER.use(function plugin(req, res, next) {
@@ -1160,25 +977,14 @@ test('gh-193 next("route") from a use plugin', function(t) {
1160977
path: '/foo'
1161978
},
1162979
function getFoo(req, res, next) {
1163-
res.send(500);
1164-
next();
1165-
}
1166-
);
1167-
1168-
SERVER.get(
1169-
{
1170-
name: 'bar',
1171-
path: '/bar'
1172-
},
1173-
function getBar(req, res, next) {
1174980
res.send(200);
1175981
next();
1176982
}
1177983
);
1178984

1179985
CLIENT.get('/foo', function(err, _, res) {
1180-
t.ifError(err);
1181-
t.equal(res.statusCode, 200);
986+
t.ok(err);
987+
t.equal(res.statusCode, 500);
1182988
t.equal(count, 1);
1183989
t.end();
1184990
});
@@ -3000,14 +2806,11 @@ test('async handler without next', function(t) {
30002806
});
30012807
});
30022808

3003-
test('async handler resolved with string should re-route', function(t) {
2809+
test('async handler should discard value', function(t) {
30042810
SERVER.get('/hello/:name', async function tester(req, res) {
30052811
await helper.sleep(10);
3006-
return 'getredirected';
3007-
});
3008-
3009-
SERVER.get('/redirected', async function tester(req, res) {
30102812
res.send(req.params.name);
2813+
return 'foo';
30112814
});
30122815

30132816
CLIENT.get('/hello/mark', function(err, _, res) {

0 commit comments

Comments
 (0)
Please sign in to comment.