kataras / iris

The fastest HTTP/2 Go Web Framework. New, modern and easy to learn. Fast development with Code you control. Unbeatable cost-performance ratio :rocket:
https://www.iris-go.com
BSD 3-Clause "New" or "Revised" License
25.22k stars 2.47k forks source link

[BUG] iris return error for jet view #1723

Open Dexus opened 3 years ago

Dexus commented 3 years ago

Describe the bug runtime error: invalid memory address or nil pointer dereference

To Reproduce run app like https://github.com/kataras/iris/tree/master/_examples/view/template_jet_3

Expected behavior no panic because of runtime error: invalid memory address or nil pointer dereference

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

Please make sure the bug is reproducible over the master branch:

$ cd PROJECT
$ go get -u github.com/kataras/iris/v12@master
$ go run .

Additional context Maybe a update to V6 of jet would be help

kataras commented 3 years ago

Hello @Dexus, v5 should work too, I will take a look. I see v6.0.2 was released some days ago, we will upgrade to that.

EDIT: it looks like the https://github.com/kataras/iris/blob/master/_examples/view/template_jet_3/main.go runs as expected

image

Can you give me ur code snippet?

UPDATE: Jet is upgrated to version 6 as requested, all examples ran and worked, if you still having issues with that please share me a code snippet, thanks!

Dexus commented 3 years ago

package server

import (
    "reflect"
    "strings"

    "git.OG-CUT/server/controller"
    "github.com/kataras/iris/v12"
    "github.com/kataras/iris/v12/view"
)

// ViewBuilder ....
type ViewBuiler struct {
}

func (ViewBuiler) Asset(a view.JetArguments) reflect.Value {
    path := a.Get(0).String()
    // fmt.Println(os.Getenv("APP_URL"))
    return reflect.ValueOf(path)
}

func (ViewBuiler) Style(a view.JetArguments) reflect.Value {
    path := a.Get(0).String()
    s := fmt.Sprintf(`<link href="%v" rel="stylesheet">`, path)
    return reflect.ValueOf(s)
}

func (ViewBuiler) Script(a view.JetArguments) reflect.Value {
    path := a.Get(0).String()
    s := fmt.Sprintf(`<script src="%v"></script>`, path)
    return reflect.ValueOf(s)
}

//RegisterJet ...
func RegisterJet() *view.JetEngine {
    tmpl := iris.Jet("server/views", ".jet")
    tmpl.Reload(true)

    val := reflect.ValueOf(ViewBuiler{})
    fns := val.Type()
    for i := 0; i < fns.NumMethod(); i++ {
        method := fns.Method(i)
        tmpl.AddFunc(strings.ToLower(method.Name), val.Method(i).Interface())
    }
    return tmpl
}

// Start Server
func Start() error {

    app := iris.New()
    app.RegisterView(RegisterJet())

    app.Use(iris.Minify)

    app.Get("/jet", func(ctx iris.Context) {
        ctx.View("index.jet")
    })

    return app.Listen(":8080",
        iris.WithLogLevel("debug"),
        iris.WithCharset("UTF-8"),
        iris.WithOptimizations,
        iris.WithoutServerError(iris.ErrServerClosed),
        iris.WithPostMaxMemory(1*iris.MB),
    )
}

index.jet

<!DOCTYPE html>

<html>

<head>
    <meta charset="UTF-8">
    <title>Hello</title>
</head>

<body>
    <p>Huhu</p>
</body>

</html>

ERROR:

[DBUG] 2021/02/14 12:39 Application: view engine "Jet" is registered
[DBUG] 2021/02/14 12:39 API: 3 registered routes (3 GET)
GET: / (./server/server.go:35)
     • iris.Minify (/home/josef/go/pkg/mod/github.com/kataras/iris/v12@v12.2.0-alpha2.0.20210213192119-d26b9bfbedfd/iris.go:366)
GET: /jet (./server/server.go:39)
     • iris.Minify (/home/josef/go/pkg/mod/github.com/kataras/iris/v12@v12.2.0-alpha2.0.20210213192119-d26b9bfbedfd/iris.go:366)
GET: /overview.php (./server/server.go:43)
     • iris.Minify (/home/josef/go/pkg/mod/github.com/kataras/iris/v12@v12.2.0-alpha2.0.20210213192119-d26b9bfbedfd/iris.go:366)

[DBUG] 2021/02/14 12:39 Host: server will ignore the following errors: [http: Server closed]
Now listening on: http://localhost:8080
Application started. Press CTRL+C to shut down.
[HTTP Server] http: panic serving [::1]:39554: runtime error: invalid memory address or nil pointer dereference
goroutine 22 [running]:
net/http.(*conn).serve.func1(0xc000546000)
        /usr/lib/go-1.15/src/net/http/server.go:1801 +0x147
panic(0xce8640, 0x14659c0)
        /usr/lib/go-1.15/src/runtime/panic.go:975 +0x47a
github.com/CloudyKit/jet/v6.(*Set).getTemplate(0x0, 0xc000530200, 0xa, 0xc000530201, 0xa, 0x41a81e, 0x7fa82da01e00)
        /home/josef/go/pkg/mod/github.com/!cloudy!kit/jet/v6@v6.0.2/set.go:133 +0x26
github.com/CloudyKit/jet/v6.(*Set).getSiblingTemplate(0x0, 0xdcfe58, 0x9, 0xdc4936, 0x1, 0x1, 0x0, 0x0, 0x0)
        /home/josef/go/pkg/mod/github.com/!cloudy!kit/jet/v6@v6.0.2/set.go:128 +0x59
github.com/CloudyKit/jet/v6.(*Set).GetTemplate(...)
        /home/josef/go/pkg/mod/github.com/!cloudy!kit/jet/v6@v6.0.2/set.go:118
github.com/kataras/iris/v12/view.(*JetEngine).ExecuteWriter(0xc0000e45a0, 0xf154a0, 0xc0005460a0, 0xdcfe58, 0x9, 0x0, 0x0, 0x0, 0x0, 0xdd8637, ...)
        /home/josef/go/pkg/mod/github.com/kataras/iris/v12@v12.2.0-alpha2.0.20210213192119-d26b9bfbedfd/view/jet.go:322 +0x78
github.com/kataras/iris/v12/view.(*View).ExecuteWriter(0xc0000fe4d8, 0xf154a0, 0xc0005460a0, 0xdcfe58, 0x9, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
        /home/josef/go/pkg/mod/github.com/kataras/iris/v12@v12.2.0-alpha2.0.20210213192119-d26b9bfbedfd/view/view.go:64 +0xfd
github.com/kataras/iris/v12.(*Application).View(0xc0000fe480, 0xf154a0, 0xc0005460a0, 0xdcfe58, 0x9, 0x0, 0x0, 0x0, 0x0, 0x5, ...)
        /home/josef/go/pkg/mod/github.com/kataras/iris/v12@v12.2.0-alpha2.0.20210213192119-d26b9bfbedfd/iris.go:413 +0xa9
github.com/kataras/iris/v12/context.(*Context).renderView(0xc0005460a0, 0xdcfe58, 0x9, 0x0, 0x0, 0x0, 0x18, 0x0)
        /home/josef/go/pkg/mod/github.com/kataras/iris/v12@v12.2.0-alpha2.0.20210213192119-d26b9bfbedfd/context/context.go:3311 +0x169
github.com/kataras/iris/v12/context.(*Context).View(0xc0005460a0, 0xdcfe58, 0x9, 0x0, 0x0, 0x0, 0xa0350f, 0xc000210930)
        /home/josef/go/pkg/mod/github.com/kataras/iris/v12@v12.2.0-alpha2.0.20210213192119-d26b9bfbedfd/context/context.go:3274 +0xa7
git.OG-CUT/server.Start.func2(0xc0005460a0)
        /home/josef/Dokumente/git/OceanGame/server/server.go:40 +0x51
github.com/kataras/iris/v12/context.(*Context).Next(0xc0005460a0)
        /home/josef/go/pkg/mod/github.com/kataras/iris/v12@v12.2.0-alpha2.0.20210213192119-d26b9bfbedfd/context/context.go:618 +0x73
github.com/kataras/iris/v12.Minify(0xc0005460a0)
        /home/josef/go/pkg/mod/github.com/kataras/iris/v12@v12.2.0-alpha2.0.20210213192119-d26b9bfbedfd/iris.go:376 +0xff
github.com/kataras/iris/v12/context.(*Context).Do(...)
        /home/josef/go/pkg/mod/github.com/kataras/iris/v12@v12.2.0-alpha2.0.20210213192119-d26b9bfbedfd/context/context.go:483
github.com/kataras/iris/v12/core/router.(*routerHandler).HandleRequest(0xc00029c540, 0xc0005460a0)
        /home/josef/go/pkg/mod/github.com/kataras/iris/v12@v12.2.0-alpha2.0.20210213192119-d26b9bfbedfd/core/router/handler.go:440 +0x21d
github.com/kataras/iris/v12/core/router.(*Router).BuildRouter.func1(0xf23420, 0xc0001f40e0, 0xc00054c000)
        /home/josef/go/pkg/mod/github.com/kataras/iris/v12@v12.2.0-alpha2.0.20210213192119-d26b9bfbedfd/core/router/router.go:219 +0x82
github.com/kataras/iris/v12/core/router.(*Router).ServeHTTP(0xc000223620, 0xf23420, 0xc0001f40e0, 0xc00054c000)
        /home/josef/go/pkg/mod/github.com/kataras/iris/v12@v12.2.0-alpha2.0.20210213192119-d26b9bfbedfd/core/router/router.go:319 +0x48
net/http.serverHandler.ServeHTTP(0xc0005ee7e0, 0xf23420, 0xc0001f40e0, 0xc00054c000)
        /usr/lib/go-1.15/src/net/http/server.go:2843 +0xa3
net/http.(*conn).serve(0xc000546000, 0xf25ce0, 0xc0001b8400)
        /usr/lib/go-1.15/src/net/http/server.go:1925 +0x8ad
created by net/http.(*Server).Serve
        /usr/lib/go-1.15/src/net/http/server.go:2969 +0x36c
[HTTP Server] http: panic serving [::1]:39564: runtime error: invalid memory address or nil pointer dereference
goroutine 25 [running]:
net/http.(*conn).serve.func1(0xc00024c0a0)
        /usr/lib/go-1.15/src/net/http/server.go:1801 +0x147
panic(0xce8640, 0x14659c0)
        /usr/lib/go-1.15/src/runtime/panic.go:975 +0x47a
github.com/CloudyKit/jet/v6.(*Set).getTemplate(0x0, 0xc00026f5d0, 0xa, 0xc00026f501, 0xa, 0x0, 0x0)
        /home/josef/go/pkg/mod/github.com/!cloudy!kit/jet/v6@v6.0.2/set.go:133 +0x26
github.com/CloudyKit/jet/v6.(*Set).getSiblingTemplate(0x0, 0xdcfe58, 0x9, 0xdc4936, 0x1, 0x1, 0x0, 0x0, 0x0)
        /home/josef/go/pkg/mod/github.com/!cloudy!kit/jet/v6@v6.0.2/set.go:128 +0x59
github.com/CloudyKit/jet/v6.(*Set).GetTemplate(...)
        /home/josef/go/pkg/mod/github.com/!cloudy!kit/jet/v6@v6.0.2/set.go:118
github.com/kataras/iris/v12/view.(*JetEngine).ExecuteWriter(0xc0000e45a0, 0xf154a0, 0xc00024c280, 0xdcfe58, 0x9, 0x0, 0x0, 0x0, 0x0, 0xdd8637, ...)
        /home/josef/go/pkg/mod/github.com/kataras/iris/v12@v12.2.0-alpha2.0.20210213192119-d26b9bfbedfd/view/jet.go:322 +0x78
github.com/kataras/iris/v12/view.(*View).ExecuteWriter(0xc0000fe4d8, 0xf154a0, 0xc00024c280, 0xdcfe58, 0x9, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
        /home/josef/go/pkg/mod/github.com/kataras/iris/v12@v12.2.0-alpha2.0.20210213192119-d26b9bfbedfd/view/view.go:64 +0xfd
github.com/kataras/iris/v12.(*Application).View(0xc0000fe480, 0xf154a0, 0xc00024c280, 0xdcfe58, 0x9, 0x0, 0x0, 0x0, 0x0, 0x5, ...)
        /home/josef/go/pkg/mod/github.com/kataras/iris/v12@v12.2.0-alpha2.0.20210213192119-d26b9bfbedfd/iris.go:413 +0xa9
github.com/kataras/iris/v12/context.(*Context).renderView(0xc00024c280, 0xdcfe58, 0x9, 0x0, 0x0, 0x0, 0xd2a340, 0x0)
        /home/josef/go/pkg/mod/github.com/kataras/iris/v12@v12.2.0-alpha2.0.20210213192119-d26b9bfbedfd/context/context.go:3311 +0x169
github.com/kataras/iris/v12/context.(*Context).View(0xc00024c280, 0xdcfe58, 0x9, 0x0, 0x0, 0x0, 0xa0350f, 0xc00024aa20)
        /home/josef/go/pkg/mod/github.com/kataras/iris/v12@v12.2.0-alpha2.0.20210213192119-d26b9bfbedfd/context/context.go:3274 +0xa7
git.OG-CUT/server.Start.func2(0xc00024c280)
        /home/josef/Dokumente/git/OceanGame/server/server.go:40 +0x51
github.com/kataras/iris/v12/context.(*Context).Next(0xc00024c280)
        /home/josef/go/pkg/mod/github.com/kataras/iris/v12@v12.2.0-alpha2.0.20210213192119-d26b9bfbedfd/context/context.go:618 +0x73
github.com/kataras/iris/v12.Minify(0xc00024c280)
        /home/josef/go/pkg/mod/github.com/kataras/iris/v12@v12.2.0-alpha2.0.20210213192119-d26b9bfbedfd/iris.go:376 +0xff
github.com/kataras/iris/v12/context.(*Context).Do(...)
        /home/josef/go/pkg/mod/github.com/kataras/iris/v12@v12.2.0-alpha2.0.20210213192119-d26b9bfbedfd/context/context.go:483
github.com/kataras/iris/v12/core/router.(*routerHandler).HandleRequest(0xc00029c540, 0xc00024c280)
        /home/josef/go/pkg/mod/github.com/kataras/iris/v12@v12.2.0-alpha2.0.20210213192119-d26b9bfbedfd/core/router/handler.go:440 +0x21d
github.com/kataras/iris/v12/core/router.(*Router).BuildRouter.func1(0xf23420, 0xc0005ee1c0, 0xc0000e8000)
        /home/josef/go/pkg/mod/github.com/kataras/iris/v12@v12.2.0-alpha2.0.20210213192119-d26b9bfbedfd/core/router/router.go:219 +0x82
github.com/kataras/iris/v12/core/router.(*Router).ServeHTTP(0xc000223620, 0xf23420, 0xc0005ee1c0, 0xc0000e8000)
        /home/josef/go/pkg/mod/github.com/kataras/iris/v12@v12.2.0-alpha2.0.20210213192119-d26b9bfbedfd/core/router/router.go:319 +0x48
net/http.serverHandler.ServeHTTP(0xc0005ee7e0, 0xf23420, 0xc0005ee1c0, 0xc0000e8000)
        /usr/lib/go-1.15/src/net/http/server.go:2843 +0xa3
net/http.(*conn).serve(0xc00024c0a0, 0xf25ce0, 0xc0002580c0)
        /usr/lib/go-1.15/src/net/http/server.go:1925 +0x8ad
created by net/http.(*Server).Serve
        /usr/lib/go-1.15/src/net/http/server.go:2969 +0x36c
[HTTP Server] http: panic serving [::1]:39606: runtime error: invalid memory address or nil pointer dereference
goroutine 9 [running]:
net/http.(*conn).serve.func1(0xc000546140)
        /usr/lib/go-1.15/src/net/http/server.go:1801 +0x147
panic(0xce8640, 0x14659c0)
        /usr/lib/go-1.15/src/runtime/panic.go:975 +0x47a
github.com/CloudyKit/jet/v6.(*Set).getTemplate(0x0, 0xc00019a340, 0xa, 0xc00019a301, 0xa, 0xc0002fd710, 0x41a305)
        /home/josef/go/pkg/mod/github.com/!cloudy!kit/jet/v6@v6.0.2/set.go:133 +0x26
github.com/CloudyKit/jet/v6.(*Set).getSiblingTemplate(0x0, 0xdcfe58, 0x9, 0xdc4936, 0x1, 0x1, 0x0, 0x0, 0x0)
        /home/josef/go/pkg/mod/github.com/!cloudy!kit/jet/v6@v6.0.2/set.go:128 +0x59
github.com/CloudyKit/jet/v6.(*Set).GetTemplate(...)
        /home/josef/go/pkg/mod/github.com/!cloudy!kit/jet/v6@v6.0.2/set.go:118
github.com/kataras/iris/v12/view.(*JetEngine).ExecuteWriter(0xc0000e45a0, 0xf154a0, 0xc0005461e0, 0xdcfe58, 0x9, 0x0, 0x0, 0x0, 0x0, 0xdd8637, ...)
        /home/josef/go/pkg/mod/github.com/kataras/iris/v12@v12.2.0-alpha2.0.20210213192119-d26b9bfbedfd/view/jet.go:322 +0x78
github.com/kataras/iris/v12/view.(*View).ExecuteWriter(0xc0000fe4d8, 0xf154a0, 0xc0005461e0, 0xdcfe58, 0x9, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
        /home/josef/go/pkg/mod/github.com/kataras/iris/v12@v12.2.0-alpha2.0.20210213192119-d26b9bfbedfd/view/view.go:64 +0xfd
github.com/kataras/iris/v12.(*Application).View(0xc0000fe480, 0xf154a0, 0xc0005461e0, 0xdcfe58, 0x9, 0x0, 0x0, 0x0, 0x0, 0x5, ...)
        /home/josef/go/pkg/mod/github.com/kataras/iris/v12@v12.2.0-alpha2.0.20210213192119-d26b9bfbedfd/iris.go:413 +0xa9
github.com/kataras/iris/v12/context.(*Context).renderView(0xc0005461e0, 0xdcfe58, 0x9, 0x0, 0x0, 0x0, 0xd2a340, 0x0)
        /home/josef/go/pkg/mod/github.com/kataras/iris/v12@v12.2.0-alpha2.0.20210213192119-d26b9bfbedfd/context/context.go:3311 +0x169
github.com/kataras/iris/v12/context.(*Context).View(0xc0005461e0, 0xdcfe58, 0x9, 0x0, 0x0, 0x0, 0xa0350f, 0xc000183530)
        /home/josef/go/pkg/mod/github.com/kataras/iris/v12@v12.2.0-alpha2.0.20210213192119-d26b9bfbedfd/context/context.go:3274 +0xa7
git.OG-CUT/server.Start.func2(0xc0005461e0)
        /home/josef/Dokumente/git/OceanGame/server/server.go:40 +0x51
github.com/kataras/iris/v12/context.(*Context).Next(0xc0005461e0)
        /home/josef/go/pkg/mod/github.com/kataras/iris/v12@v12.2.0-alpha2.0.20210213192119-d26b9bfbedfd/context/context.go:618 +0x73
github.com/kataras/iris/v12.Minify(0xc0005461e0)
        /home/josef/go/pkg/mod/github.com/kataras/iris/v12@v12.2.0-alpha2.0.20210213192119-d26b9bfbedfd/iris.go:376 +0xff
github.com/kataras/iris/v12/context.(*Context).Do(...)
        /home/josef/go/pkg/mod/github.com/kataras/iris/v12@v12.2.0-alpha2.0.20210213192119-d26b9bfbedfd/context/context.go:483
github.com/kataras/iris/v12/core/router.(*routerHandler).HandleRequest(0xc00029c540, 0xc0005461e0)
        /home/josef/go/pkg/mod/github.com/kataras/iris/v12@v12.2.0-alpha2.0.20210213192119-d26b9bfbedfd/core/router/handler.go:440 +0x21d
github.com/kataras/iris/v12/core/router.(*Router).BuildRouter.func1(0xf23420, 0xc0001f41c0, 0xc00054c200)
        /home/josef/go/pkg/mod/github.com/kataras/iris/v12@v12.2.0-alpha2.0.20210213192119-d26b9bfbedfd/core/router/router.go:219 +0x82
github.com/kataras/iris/v12/core/router.(*Router).ServeHTTP(0xc000223620, 0xf23420, 0xc0001f41c0, 0xc00054c200)
        /home/josef/go/pkg/mod/github.com/kataras/iris/v12@v12.2.0-alpha2.0.20210213192119-d26b9bfbedfd/core/router/router.go:319 +0x48
net/http.serverHandler.ServeHTTP(0xc0005ee7e0, 0xf23420, 0xc0001f41c0, 0xc00054c200)
        /usr/lib/go-1.15/src/net/http/server.go:2843 +0xa3
net/http.(*conn).serve(0xc000546140, 0xf25ce0, 0xc0002c4000)
        /usr/lib/go-1.15/src/net/http/server.go:1925 +0x8ad
created by net/http.(*Server).Serve
        /usr/lib/go-1.15/src/net/http/server.go:2969 +0x36c

@kataras sorry for the late reply jut married and for got to recheck until now.

kataras commented 3 years ago

Hello @Dexus, first of all Congratulations and happy marriage!!

I have a "gift" for you, just pushed a commit which allows to pass custom error handlers when a path type parameter evaluator was failed:

app.Macros().Get("uuid").HandleError(func(ctx iris.Context, err error) {
    ctx.StatusCode(iris.StatusBadRequest)
    ctx.JSON(iris.Map{
        "error":   err.Error(),
        "message": "invalid path parameter",
    })
})

app.Get("/users/{id:uuid}", getUser)

I will check the above code snippet you posted later on and will come back for a solution.

kataras commented 3 years ago

@Dexus the code snippet you sent me is working here. Can you please provide one which reproduces the issue? Maybe you have one with a template file which calls a template function and makes the jet parser to fail at runtime?

image

Dexus commented 3 years ago

I will try it tomorrow again, I'm not sure yet what triggers the error, but because it is currently just a new basic initiated app, I think it's iris related, may it make a different if the initial cmd is build in ./cmd/GameBinary from the root and the main server is in ./server/? I run allways from root dir go run ./cmd/GameBinary/main.go and then it panics.

Dexus commented 3 years ago

Example Code here: https://nc.josef-froehle.de/nextcloud/index.php/s/N2QFbm7s9bkd3kD (Available until: 2020-03-22)

I can't find the problem, maybe I a bit code blinde....