# Method invocation error on v0.6

**URL:** <https://discourse.julialang.org/t/method-invocation-error-on-v0-6/4476>\
**Category:** General Usage\
**Created:** [June 26, 2017, 8:41pm UTC](https://discourse.julialang.org/t/method-invocation-error-on-v0-6/4476 "2017-06-26T20:41:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)\
**Post date:** [June 26, 2017, 8:41pm UTC](https://discourse.julialang.org/t/method-invocation-error-on-v0-6/4476/1 "2017-06-26T20:41:49Z")

</div>

I have some code that looks like this:

```julia
route("/") do
  "hello"
end

```

Later on, the function passed into `route` is put in a variable `to` and the following code is run:

```julia
if isa(to, Function)
  to() |> to_response
end

```

Works perfectly on v0.5, but on v0.6 it throws an error

```julia
MethodError(#17, (), 0x000000000000598d)

```

I have no idea why as I can’t spot anything relevant in the NEWS for v0.6

Please help, I’ve been pulling my hair for the last 2 hours with this - once I run out of hair, my last resort is to crawl under my desk and cry as I’m running out of ideas.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 26, 2017, 9:12pm UTC](https://discourse.julialang.org/t/method-invocation-error-on-v0-6/4476/2 "2017-06-26T21:12:32Z")

</div>

> [@essenciary](#):
>
> Please help, I’ve been pulling my hair for the last 2 hours with this - once I run out of hair, my last resort is to crawl under my desk and cry as I’m running out of ideas.

Before you do that, please post more complete code and maybe the full error message.

---

<div class="post-metadata">

**Author:** ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)\
**Post date:** [June 26, 2017, 9:38pm UTC](https://discourse.julialang.org/t/method-invocation-error-on-v0-6/4476/3 "2017-06-26T21:38:24Z")

</div>

Thanks @yuyichao

Here are the relevant bits:

```julia
const _routes = Dict{Symbol,Any}()
const Route = Tuple{Tuple{String,String,Union{String,Function}},Dict{Symbol,Dict{Any,Any}}}

function route(action::Function, path::String; method = GET, with::Dict = Dict{Symbol,Any}(), named::Symbol = :__anonymous_route) :: Route
  route(path, action, method = method, with = with, named = named)
end

function route(path::String, action::Union{String,Function}; method = GET, with::Dict = Dict{Symbol,Any}(), named::Symbol = :__anonymous_route) :: Route
  route_parts = (method, path, action)

  extra_route_parts = Dict(:with => with)
  named = named == :__anonymous_route ? route_name(route_parts) : named

  _routes[named] = (route_parts, extra_route_parts)
end

function routes() :: Vector{Route}
  collect(values(_routes))
end

function match_routes(req::Request, res::Response, session::Sessions.Session, params::Params) :: Response
  for r in routes()
    route_def, extra_params = r
    protocol, route, to = route_def

    # more code in here that doesn't matter

    return try
              if isa(to, Function)
                to() |> to_response
              end
            catch ex
              Logger.log("Failed invoking controller", :err)
              Logger.log(string(ex), :err)
              Logger.log("$(@ __FILE__ ):$(@ __LINE__ )", :err)

              rethrow(ex)
            end
  end
  serve_error_file(404, "Not found")
end

```

And the error:

```julia
2017-06-26T14:33:46.329 - critical: MethodError(#17, (), 0x0000000000005992)

2017-06-26T14:33:46.631 - critical:
Stacktrace:
 [1] match_routes(::HttpCommon.Request, ::HttpCommon.Response, ::Sessions.Session, ::Router.Params{Any}) at /Users/adrian/.julia/v0.6/Genie/src/Router.jl:461
 [2] route_request(::HttpCommon.Request, ::HttpCommon.Response, ::IPv4) at /Users/adrian/.julia/v0.6/Genie/src/Router.jl:86
 [3] handle_request(::HttpCommon.Request, ::HttpCommon.Response, ::IPv4) at /Users/adrian/.julia/v0.6/Genie/src/AppServer.jl:122
 [4] (::AppServer.##1#9)(::HttpCommon.Request, ::HttpCommon.Response) at /Users/adrian/.julia/v0.6/Genie/src/AppServer.jl:25
 [5] (::HttpServer.#on_message_complete#14{HttpServer.Server,HttpServer.Client{TCPSocket},Bool})(::HttpCommon.Request) at /Users/adrian/.julia/v0.6/HttpServer/src/HttpServer.jl:427
 [6] on_message_complete(::Ptr{HttpParser.Parser}) at /Users/adrian/.julia/v0.6/HttpServer/src/RequestParser.jl:113
 [7] http_parser_execute(::HttpParser.Parser, ::HttpParser.ParserSettings, ::Array{UInt8,1}) at /Users/adrian/.julia/v0.6/HttpParser/src/HttpParser.jl:115
 [8] process_client(::HttpServer.Server, ::HttpServer.Client{TCPSocket}, ::Bool) at /Users/adrian/.julia/v0.6/HttpServer/src/HttpServer.jl:389
 [9] (::HttpServer.##7#8{HttpServer.Server,Bool})() at ./task.jl:335

```

---

<div class="post-metadata">

**Author:** ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)\
**Post date:** [June 26, 2017, 9:49pm UTC](https://discourse.julialang.org/t/method-invocation-error-on-v0-6/4476/4 "2017-06-26T21:49:14Z")

</div>

It seems that I get the error only in the context of executing the code through HttpServer 😲

This is what is looks like in my REPL - on top is the output from running a request through the browser. Then it’s me running the same code through the REPL.

```julia
2017-06-26T14:46:37.46 - error: Failed invoking controller

2017-06-26T14:46:37.46 - error: MethodError(#21, (), 0x0000000000005994)

2017-06-26T14:46:37.46 - error: /Users/adrian/.julia/v0.6/Genie/src/Router.jl:468

2017-06-26T14:46:37.461 - critical: MethodError(#21, (), 0x0000000000005994)

2017-06-26T14:46:37.491 - critical:
Stacktrace:
 [1] match_routes(::HttpCommon.Request, ::HttpCommon.Response, ::Sessions.Session, ::Router.Params{Any}) at /Users/adrian/.julia/v0.6/Genie/src/Router.jl:461
 [2] route_request(::HttpCommon.Request, ::HttpCommon.Response, ::IPv4) at /Users/adrian/.julia/v0.6/Genie/src/Router.jl:86
 [3] handle_request(::HttpCommon.Request, ::HttpCommon.Response, ::IPv4) at /Users/adrian/.julia/v0.6/Genie/src/AppServer.jl:122
 [4] (::AppServer.##1#9)(::HttpCommon.Request, ::HttpCommon.Response) at /Users/adrian/.julia/v0.6/Genie/src/AppServer.jl:25
 [5] (::HttpServer.#on_message_complete#14{HttpServer.Server,HttpServer.Client{TCPSocket},Bool})(::HttpCommon.Request) at /Users/adrian/.julia/v0.6/HttpServer/src/HttpServer.jl:427
 [6] on_message_complete(::Ptr{HttpParser.Parser}) at /Users/adrian/.julia/v0.6/HttpServer/src/RequestParser.jl:113
 [7] http_parser_execute(::HttpParser.Parser, ::HttpParser.ParserSettings, ::Array{UInt8,1}) at /Users/adrian/.julia/v0.6/HttpParser/src/HttpParser.jl:115
 [8] process_client(::HttpServer.Server, ::HttpServer.Client{TCPSocket}, ::Bool) at /Users/adrian/.julia/v0.6/HttpServer/src/HttpServer.jl:389
 [9] (::HttpServer.##7#8{HttpServer.Server,Bool})() at ./task.jl:335

2017-06-26T14:46:37.491 - critical: /Users/adrian/.julia/v0.6/Genie/src/AppServer.jl:29

genie> for r in Router.routes()
       route_def, extra_params = r
       protocol, route, to = route_def
       @show to()
       end
to() = "hello"

```

---

<div class="post-metadata">

**Author:** ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)\
**Post date:** [June 26, 2017, 10:04pm UTC](https://discourse.julialang.org/t/method-invocation-error-on-v0-6/4476/5 "2017-06-26T22:04:13Z")

</div>

With a bit more debugging info

```julia
@show current_module()
if isa(to, Function)
  @show @which to()
  @show methods(to)
  @show to

  to() |> to_response
end

```

I get

```julia
current_module() = Main
@which(to()) = (::##17#18)() in Main at /Users/adrian/Dropbox/Projects/_test_app3/config/routes.jl:4
methods(to) = # 1 method for generic function "(::#17)":
(::##17#18)() in Main at /Users/adrian/Dropbox/Projects/_test_app3/config/routes.jl:4
to = #17

2017-06-26T14:59:53.217 - error: Failed invoking controller

2017-06-26T14:59:53.217 - error: MethodError(#17, (), 0x0000000000005992)

2017-06-26T14:59:53.218 - error: /Users/adrian/.julia/v0.6/Genie/src/Router.jl:473

2017-06-26T14:59:53.218 - critical: MethodError(#17, (), 0x0000000000005992)

2017-06-26T14:59:53.515 - critical:
Stacktrace:
 [1] match_routes(::HttpCommon.Request, ::HttpCommon.Response, ::Sessions.Session, ::Router.Params{Any}) at /Users/adrian/.julia/v0.6/Genie/src/Router.jl:466
 [2] route_request(::HttpCommon.Request, ::HttpCommon.Response, ::IPv4) at /Users/adrian/.julia/v0.6/Genie/src/Router.jl:86
 [3] handle_request(::HttpCommon.Request, ::HttpCommon.Response, ::IPv4) at /Users/adrian/.julia/v0.6/Genie/src/AppServer.jl:122
 [4] (::AppServer.##1#9)(::HttpCommon.Request, ::HttpCommon.Response) at /Users/adrian/.julia/v0.6/Genie/src/AppServer.jl:25
 [5] (::HttpServer.#on_message_complete#14{HttpServer.Server,HttpServer.Client{TCPSocket},Bool})(::HttpCommon.Request) at /Users/adrian/.julia/v0.6/HttpServer/src/HttpServer.jl:427
 [6] on_message_complete(::Ptr{HttpParser.Parser}) at /Users/adrian/.julia/v0.6/HttpServer/src/RequestParser.jl:113
 [7] http_parser_execute(::HttpParser.Parser, ::HttpParser.ParserSettings, ::Array{UInt8,1}) at /Users/adrian/.julia/v0.6/HttpParser/src/HttpParser.jl:115
 [8] process_client(::HttpServer.Server, ::HttpServer.Client{TCPSocket}, ::Bool) at /Users/adrian/.julia/v0.6/HttpServer/src/HttpServer.jl:389
 [9] (::HttpServer.##7#8{HttpServer.Server,Bool})() at ./task.jl:335

```

I don’t get it… Julia knows that it is a function, it finds it, but then it can’t invoke it…

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 26, 2017, 10:10pm UTC](https://discourse.julialang.org/t/method-invocation-error-on-v0-6/4476/6 "2017-06-26T22:10:46Z")

</div>

Likely `HttpServer` is not correctly executing the method in the latest world after `eval` code that defined the function. It should also showerror the error instead of just showing it’s content.

---

<div class="post-metadata">

**Author:** ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)\
**Post date:** [June 26, 2017, 10:12pm UTC](https://discourse.julialang.org/t/method-invocation-error-on-v0-6/4476/7 "2017-06-26T22:12:27Z")

</div>

Thanks, I’ll open up an issue with `HttpServer`
