views:

231

answers:

1

Further to my adventures with Erlang and ErlyDB. I am attempting to get ErlyDB working with BeepBeep

My ErlyDB setup works correctly when run outside of the BeepBeep environment (see Debugging ErlyDB and MySQL). I have basically take the working code and attempted to get it running inside BeepBeep.

I have the following code in my controller:

handle_request("index",[]) ->
  erlydb:start(mysql,Database),
  erlydb:code_gen(["thing.erl"],mysql), 
  NewThing = thing:new_with([{name, "name"},{value, "value"}]),
  thing:save(NewThing),
  {render,"home/index.html",[{data,"Hello World!"}]};

When I call the URL, the response outputs "Server Error". There is no other error or exception information reported.

I have tried wrapping the call in try/catch to see if there is an underlying error - there is definitely an exception at the call to thing:new_with(), but no further information is available.

The stacktrace reports:

{thing,new,[["name","value"]]}
{home_controller,create,1}
{home_controller,handle_request,3}
{beepbeep,process_request,4}
{test_web,loop,1}
{mochiweb_http,headers,4}
{proc_lib,init_p_do_apply,3}
+2  A: 

Use pattern matching to assert that things work up to the call to thing:new/1:

ok = erlydb:start(mysql,Database),
ok = erlydb:code_gen(["thing.erl"],mysql),

You include only the stack trace, look at the exception message as well. I suspect that the error is that you get an 'undef' exception. But check that it is so. The first line in the stack trace indicates that it is a problem with calling thing:new/1 with ["name", "value"] as argument.

It is slightly odd that you show one clause of handle_request that is not calling home_controller:create/1 as per {home_controller,create,1} in the stack-trace. What do the other clauses in your handle_request/2 function look like?

Christian
Thanks for the advice. Using the patterns as above works nicely.One of the problems seems to be that the exception is just swallowed by the server ... The browser reports server error, but the server command line shows no output at all. Makes things a bit tricky to debug.
Toby Hede
Why not use dbg with return value and process event trace then?
Zed
What is dbg?I am pretty new to Erlang.
Toby Hede
Erlang has an interface to debugging called dbg. See this question for some usage examples: http://stackoverflow.com/questions/1274681/query-an-erlang-process-for-its-state/1274939
Christian
See http://www.erlang.org/doc/man/dbg.html for dbg. For example trydbg:tracer(), dbg:tpl(ModuleName, [{'_', [], [{return_trace}]}]), dbg:p(all, c). to see all calls to a given module's functions, and their return values on the console. This is usually very helpful in figuring out what and when went wrong.
Zed