buttonmen-dev / buttonmen

Buttonmen - an online dice game
Other
16 stars 24 forks source link

we don't get a log message if JSON encoding fails #2983

Closed cgolubi1 closed 2 months ago

cgolubi1 commented 2 months ago

This is something i discovered during testing around #2974 but is worth addressing independently.

The ApiResponder.php code which returns API responses to the user ends with:

            header('Content-Type: application/json');
            echo json_encode($output);

JSON encoding fails silently, so if it fails for whatever reason, the user will get red error text, e.g. Internal error when calling loadForumThread, but nothing shows up in the error log. This means people could be getting that kind of error message all the time, and we have no way of knowing unless they complain.

Proposed fix:

            header('Content-Type: application/json');
            echo json_encode($output);
            $last_error = json_last_error();
            if ($last_error) {
                error_log("API call with spec: " . var_export($args, TRUE) . " yielded a JSON error: " . $last_error);
            }

If we made that change, we'd be notified via error log whenever someone didn't get their API response.

cgolubi1 commented 2 months ago

json_last_error_msg() might be even better, depending; i think it's just the string version of the static integer error, so it's not going to make our logs vulnerable or anything.