代码之家  ›  专栏  ›  技术社区  ›  Dmitry Bubnenkov

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

  •  1
  • Dmitry Bubnenkov  · 技术社区  · 8 年前

    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'

    1 回复  |  直到 8 年前
        1
  •  1
  •   John Smith    8 年前

    如果您查看上面的错误 error instantiating vibe.d tries to call init 父类的方法,其中 render!

    这意味着当前无法在调用的函数中呈现任何模板 errorDisplay 在课堂之外。事实上,当经过的时候 &((new NewWebService).myStuff router.any , 错误显示 存储库使用具有 错误显示


    你可以包起来 getStuff showPageNotFound 在课堂上,但是 router.any("*", ... 不可能,因为它仅适用于单个函数,并且 @path registerWebInterface .

    解决方法不是抛出异常,而是在内部呈现错误 myStuff 错误显示 .

    更好的解决方案是在 通过 req 调用函数的参数 错误显示 (然后修复一个bug?那就是 @路径 与一起使用时 .

    推荐文章