I am pretty new to Erlang, and I am trying to get a basic try / catch statement to work.
I am using webmachine to process some requests and all I really want to do is parse some JSON data and return it. In the event that the JSON data is invalid, I just want to return an error msg. Here is the code I have so far.

Note that the JSON data is invalid.

to_text(ReqData, Context) ->    
    Body =  "{\"firstName\": \"John\"\"lastName\": \"Smith\"}",
    try decode(Body) of
  _ -> {"Success! Json decoded!",ReqData,Context}
 catch
  _ -> {"Error! Json is invalid",ReqData,Context}
 end.         
    

decode(Body) ->
  {struct, MJ} = mochijson:decode(Body).

The code compiles, but when I run it, and send a request for the text, I get the following error back.

error,{error,{case_clause,{{const,"lastName"},
                            ": \"Smith\"}",
                            {decoder,utf8,null,1,31,comma}}},
              [{mochijson,decode_object,3},
               {mochijson,json_decode,2},
               {webmachine_demo_resource,test,1},
               {webmachine_demo_resource,to_text,2},
               {webmachine_demo_resource,to_html,2},
               {webmachine_resource,resource_call,3},
               {webmachine_resource,do,3},
               {webmachine_decision_core,resource_call,1}]}}

What exactly am I doing wrong?
The documentation says the "catch" statement handles all errors, or do I have to do something to catch a specific error that is thrown by mochijson:decode.
Please any leads or advice would be helpful.

Edit
Report