Add function to return JSON encoded errors.