【发布时间】:2014-09-22 00:40:05
【问题描述】:
我在 Web 服务器中有一些代码可以将路由映射到处理程序中,如下所示:
final Map<String, Handler> handlers = {
r'/index.html': StaticFileHandler('./web'),
r'/test/(\d+)/(\d+)': MyTestHandler
};
MyTestHandler(HttpRequest request, int number1, int number2) {
request.response.headers.contentType = new ContentType('text', 'html');
request.response.write('<h1>$number1 ($number2)</h1>');
request.response.close();
}
为了支持正则表达式作为参数,我必须在提取参数后使用Function.apply;这意味着没有针对处理程序的开发时间检查或路由。如果您得到错误数量的正则表达式组与处理程序参数;它像这样爆炸:
Unhandled exception:
Uncaught Error: Closure call with mismatched arguments: function 'call'
NoSuchMethodError: incorrect number of arguments passed to method named 'call'
Receiver: Closure: (HttpRequest, int) => dynamic from Function 'MyTestHandler': static.
Tried calling: call(Instance of '_HttpRequest', "2", "3")
Found: call(request, number)
Stack Trace:
这对开发人员来说并不完全清楚出了什么问题;所以我宁愿抛出一个更容易解释问题的自定义错误。
有没有一种简单的方法可以检测到这种失败(例如,获取函数期望的参数数量):
- 不使用像镜子这样的重物
- 不涉及捕获所有异常;因为这将阻碍调试,并且用户处理代码可能会从处理程序中调用相同的错误;而且我不想干扰该错误消息
【问题讨论】:
标签: dart