catch json encoding errors and throw an exception