d - 错误:模板实例实例化时出错



如果req太短,我正在尝试显示错误消息。这是代码:

import std.stdio;
import vibe.d;
Database mydatabase;
void main()
{
// ...
router.get("*", &myStuff); // all other request
listenHTTP(settings, router);
runApplication();
}
@errorDisplay!showPageNotFound
void myStuff(HTTPServerRequest req, HTTPServerResponse res) // I need this to handle any accessed URLs
{
if(req.path.length > 10) 
{
// ...
}
else
{
throw new Exception("Nothing do not found");
}
}
void showPageNotFound(string _error = null)
{
render!("error.dt", _error);
}

错误是:

sourceapp.d(80,2): Error: template instance app.showPageNotFound.render!("error.dt", _error).render!("app", "app.showPageNotFound") error instantiating

如果我正在做:

void showPageNotFound(string _error = null)
{
res.render!("error.dt", _error);
}

我收到错误:Error: undefined identifier 'res'

如果您查看error instantiating错误上方的错误,您会发现vibe.d尝试调用render!的父类init方法,但是您的代码没有父类。

这意味着目前您无法在类外部的errorDisplay调用的函数中呈现任何模板。事实上,当&((new NewWebService).myStuff传递给router.any时,errorDisplay根本不起作用(错误?存储库中的所有示例都使用带有errorDisplayvibe.d的类。


您可以将getStuffshowPageNotFound包装在类中,router.any("*", ...但这是不可能的,因为它仅适用于单个函数,并且@path属性在与registerWebInterface一起使用时不支持通配符。

对此的解决方案不是抛出异常,而是在myStuff中呈现错误。虽然很差,但我认为你想用errorDisplay.

更好的解决方案是实现功能,vibe.dreq参数传递给errorDisplay调用的函数(并修复errorDisplay不能在类外使用的错误),或者更好的是,在与registerWebInterface一起使用时支持通配符@path

最新更新