Logic Machine Forum
error handling - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: error handling (/showthread.php?tid=2850)



error handling - gjniewenhuijse - 18.09.2020

When decoding a json string you can get different errors.

Is this code the best way to handle errors? or is there a more generic way to do this?

Code:
if states and states ~= '' then
  local success, res = pcall(json.decode, states)
  if success then
    states = res
  end
end



RE: error handling - admin - 18.09.2020

You can use json.pdecode which will return nil, err in case of an error:
Code:
res, err = json.pdecode(data)