1

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);
}

错误是:

source\app.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'

4

1 回答 1

1

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

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


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

对此的解决方案不是抛出异常,而是将错误呈现在myStuff. 尽管它很差,但我认为您想使用errorDisplay.

更好的解决方案是实现功能vibe.d以将req参数传递给由调用的函数errorDisplay(并修复错误?errorDisplay不能在类外使用),或者更好的是,在@pathregisterWebInterface.

于 2017-06-28T11:23:04.257 回答