json encoding in Chicago Boss tutorial doesn't work

334 views Asked by At

The Chicago Boss tutorial

http://www.chicagoboss.org/tutorial.pdf

gives an example of a BossRecord automatically being converted to JSON in a controller response to a GET like this:

pull('GET', [LastTimestamp]) ->
    {ok, Timestamp, Greetings} = boss_mq:pull("new-greetings", list_to_integer(LastTimestamp)),
    {json, [{timestamp, Timestamp}, {greetings, Greetings}]}.

However, this code doesn't work. boss_json will not convert "Greetings" record it gets from the boss_mq:pull, and gives me an unhandled error. I'm having a hard time figuring out what it wants.

16:50:45.634 [error] Unhandled Error: error:function_clause. 
Stacktrace: [{boss_json,json_data1,[[{greeting,"greeting-77","zz"}],[],[]],
[{file,"src/boss/boss_json.erl"},{line,31}]},
{boss_json,json_data1,3,[{file,"src/boss/boss_json.erl"},{line,42}]},
{boss_json,encode,2,[{file,"src/boss/boss_json.erl"},{line,16}]},
{boss_web_controller_render,process_action_result,4,[{file,"src/boss/boss_web_controller_render.erl"},{line,171}]},
{boss_web_controller,execute_action_inner,9,[{file,"src/boss/boss_web_controller.erl"},{line,337}]},
{boss_web_controller_handle_request,process_dynamic_request,4,[{file,"src/boss/boss_web_controller_handle_request.erl"},{line,242}]},
{boss_web_controller_handle_request,process_request,4,[{file,"src/boss/boss_web_controller_handle_request.erl"},{line,228}]},
{boss_web_controller_handle_request,set_timer,7,[{file,"src/boss/boss_web_controller_handle_request.erl"},{line,148}]}]

16:50:45.636 [info] POST /greeting/create [cbmonitor] 302 0ms

Any ideas why the tutorial example (written several years ago) no longer works in the current version of Chicago Boss?

1

There are 1 answers

7
tkowal On

This isn't actually an answer, but it is too long for comment. I run the example code from chicagoboss_tutorial and it works for me. You might try cloning this repository and checking out, what differs.

I also run dbg during execution.

dbg:tracer().
dbg:tpl(boss_json, '_', []).
dbg:p(all, c).

and it gave me following output:

(<0.195.0>) call boss_json:encode([{timestamp,1417745752505174},{greetings,
[{greeting,"greeting-4","asdf"}]}],["greeting"])
(<0.195.0>) call boss_json:json_data1([{timestamp,1417745752505174},{greetings,[{greeting,"greeting-4","asdf"}]}],["greeting"],[])
(<0.195.0>) call boss_json:json_data1([{greetings,[{greeting,"greeting-4","asdf"}]}],["greeting"],[{timestamp,1417745752505174}])
(<0.195.0>) call boss_json:json_data1([],["greeting"],[{greetings,[{struct,[{id,<<"greeting-4">>},{greeting_text,<<"asdf">>}]}]},

Second argument of boss_json:encode/2 and boss_json:json_data1 is ModelList and for some reason, you have empty model list. As I said before - try to compare your code with the code from repository.