Skip to content

Commit 87eeb58

Browse files
author
喵喵大人
authored
Merge pull request #78 from CatLib/1.1
修复了路由不能正确定位异常位置的bug
2 parents a9349a1 + 2648bfd commit 87eeb58

File tree

6 files changed

+112
-32
lines changed

6 files changed

+112
-32
lines changed

CatLib.Framework.sln

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
4-
VisualStudioVersion = 15.0.27004.2002
4+
VisualStudioVersion = 15.0.26430.4
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CatLib.Framework", "src\CatLib.Framework\CatLib.Framework.csproj", "{BBB2DAE2-638B-4419-9591-3CECCA312E4E}"
77
EndProject

src/CatLib.Framework.Tests/CatLib.Framework.Tests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
<Compile Include="Network\ReceiveStateTests.cs" />
7373
<Compile Include="Network\TcpServer.cs" />
7474
<Compile Include="Random\RandomProviderTests.cs" />
75+
<Compile Include="Routing\TestMiddlewareException.cs" />
7576
<Compile Include="Socket\KcpConnectorTests.cs" />
7677
<Compile Include="Socket\KcpTestsServer.cs" />
7778
<Compile Include="Socket\SocketProviderTests.cs" />

src/CatLib.Framework.Tests/Routing/RouterExceptionTests.cs

+48-4
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,18 @@ public void TestNestedNoSchemeThrowException()
123123

124124
ExceptionAssert.Throws<NotFoundRouteException>(() =>
125125
{
126-
router.Dispatch("ui://helloworld/call");
126+
try
127+
{
128+
router.Dispatch("ui://helloworld/call");
129+
}
130+
catch (Exception ex)
131+
{
132+
while (ex.InnerException != null)
133+
{
134+
ex = ex.InnerException;
135+
}
136+
throw ex;
137+
}
127138
});
128139

129140
Assert.AreEqual(1, throwNotFound);
@@ -155,7 +166,18 @@ public void TestSimpleException()
155166

156167
ExceptionAssert.Throws<ArgumentNullException>(() =>
157168
{
158-
router.Dispatch("ui://helloworld/call");
169+
try
170+
{
171+
router.Dispatch("ui://helloworld/call");
172+
}
173+
catch (Exception ex)
174+
{
175+
while (ex.InnerException != null)
176+
{
177+
ex = ex.InnerException;
178+
}
179+
throw ex;
180+
}
159181
});
160182

161183
Assert.AreEqual(1, throwError);
@@ -206,7 +228,18 @@ public void TestNestedException()
206228

207229
ExceptionAssert.Throws<ArgumentNullException>(() =>
208230
{
209-
router.Dispatch("ui://helloworld/call");
231+
try
232+
{
233+
router.Dispatch("ui://helloworld/call");
234+
}
235+
catch (Exception ex)
236+
{
237+
while (ex.InnerException != null)
238+
{
239+
ex = ex.InnerException;
240+
}
241+
throw ex;
242+
}
210243
});
211244

212245
Assert.AreEqual(12, throwError);
@@ -287,7 +320,18 @@ public void TestNestedNotFoundRouteTest()
287320
{
288321
ExceptionAssert.Throws<NotFoundRouteException>(() =>
289322
{
290-
router.Dispatch("ui://helloworld/call2");
323+
try
324+
{
325+
router.Dispatch("ui://helloworld/call2");
326+
}
327+
catch (Exception ex)
328+
{
329+
while (ex.InnerException != null)
330+
{
331+
ex = ex.InnerException;
332+
}
333+
throw ex;
334+
}
291335
});
292336
res.SetContext("helloworld");
293337
});

src/CatLib.Framework.Tests/Routing/RouterTests.cs

+12-1
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,18 @@ public void RoutingCircularDependencyCall()
419419

420420
ExceptionAssert.Throws<RuntimeException>(() =>
421421
{
422-
router.Dispatch("lambda://call/RoutingCircularDependencyCall-1");
422+
try
423+
{
424+
router.Dispatch("lambda://call/RoutingCircularDependencyCall-1");
425+
}
426+
catch (Exception ex)
427+
{
428+
while (ex.InnerException != null)
429+
{
430+
ex = ex.InnerException;
431+
}
432+
throw ex;
433+
}
423434
});
424435
}
425436

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* This file is part of the CatLib package.
3+
*
4+
* (c) Yu Bin <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*
9+
* Document: http://catlib.io/
10+
*/
11+
12+
using System;
13+
using System.Reflection;
14+
using CatLib.API.Routing;
15+
using CatLib.Routing;
16+
using Microsoft.VisualStudio.TestTools.UnitTesting;
17+
18+
namespace CatLib.Tests.Routing
19+
{
20+
[TestClass]
21+
public class TestMiddlewareException
22+
{
23+
[TestMethod]
24+
[ExpectedException(typeof(TargetInvocationException))]
25+
public void TestMiddlewareThrow()
26+
{
27+
var app = new Application();
28+
app.Bootstrap();
29+
app.OnFindType((t) =>
30+
{
31+
return Type.GetType(t);
32+
});
33+
app.Register(new RoutingProvider());
34+
var router = app.Make<IRouter>();
35+
router.Reg("test://throw", () =>
36+
{
37+
string str = null;
38+
var l = str.Length;
39+
});
40+
41+
router.Dispatch("test://throw");
42+
}
43+
}
44+
}

src/CatLib.Framework/Routing/Route.cs

+6-26
Original file line numberDiff line numberDiff line change
@@ -310,37 +310,17 @@ private void DispatchToAction(Request request, Response response)
310310
/// <param name="callback">完成中间件的回调</param>
311311
private void ThroughRouteMiddleware(Request request, Response response, object context, Action<Request, Response, object> callback)
312312
{
313-
try
313+
var middleware = GatherMiddleware();
314+
if (middleware != null)
314315
{
315-
var middleware = GatherMiddleware();
316-
if (middleware != null)
317-
{
318-
middleware.Do(request, response, (req, res) =>
319-
{
320-
callback.Invoke(request, response, context);
321-
});
322-
}
323-
else
316+
middleware.Do(request, response, (req, res) =>
324317
{
325318
callback.Invoke(request, response, context);
326-
}
319+
});
327320
}
328-
catch (Exception ex)
321+
else
329322
{
330-
var count = 0;
331-
while (count++ < 255)
332-
{
333-
if (ex.InnerException == null)
334-
{
335-
throw ex;
336-
}
337-
ex = ex.InnerException;
338-
}
339-
340-
if (count >= 255)
341-
{
342-
throw new RuntimeException("Route Error stack overflow. There may be an infinite loop. url: [" + request.Uri + "]");
343-
}
323+
callback.Invoke(request, response, context);
344324
}
345325
}
346326

0 commit comments

Comments
 (0)