Skip to content

Account sign-up fatals when the Optimole API returns an empty or non-JSON response #1200

Closed

Description

@kushh23

Description

When an admin creates an Optimole account from the plugin dashboard (POST /optml/v1/register_service), Optml_Api::request() returns false if the API answers with an empty body, a non-JSON body (for example a proxy's HTML 502 page), JSON without a code key, or a non-200 code without an error key.

register_service() sends false into the same branch as WP_Error and calls $user->get_error_code() on it. This throws Error: Call to a member function get_error_code() on false. WordPress then returns a 500 "There has been a critical error on this website." and the dashboard shows its generic REST error instead of "Error creating account.".

Expected: when the API returns false, the endpoint should return the normal code: error response ("Error creating account."), as it did up to 3.11.3 and as connect() and select_application() already do.

Step-by-step reproduction instructions

  1. On a test site with Optimole 4.2.17 active and not connected, run this command. It fakes an empty 502 reply from the account API through pre_http_request, so no real request is sent. Replace 1 with an administrator's user ID.
    wp eval '
    add_filter( "pre_http_request", function ( $pre, $args, $url ) {
        return false !== strpos( $url, "complete_register_remote" )
            ? [ "headers" => [], "body" => "", "response" => [ "code" => 502, "message" => "Bad Gateway" ], "cookies" => [], "filename" => null ]
            : $pre;
    }, 10, 3 );
    wp_set_current_user( 1 );
    $r = new WP_REST_Request( "POST", "/optml/v1/register_service" );
    $r->set_param( "email", "qa@example.test" );
    var_dump( rest_do_request( $r )->get_data() );
    '
    
  2. Result: PHP Fatal error: Uncaught Error: Call to a member function get_error_code() on false in .../inc/rest.php:468.
  3. Expected: an array with code => error and message => "Error creating account.".
  4. Same outcome without WP-CLI: I loaded the real inc/api.php and inc/rest.php from v4.2.17 with WordPress functions stubbed and called register_service() with canned API bodies (PHP 8.4.19):
    == empty body ==
    UNCAUGHT Error: Call to a member function get_error_code() on false at rest.php:468
    == HTML 502 page from a proxy ==
    UNCAUGHT Error: Call to a member function get_error_code() on false at rest.php:468
    == JSON without code ==
    UNCAUGHT Error: Call to a member function get_error_code() on false at rest.php:468
    == JSON code 500, no error key ==
    UNCAUGHT Error: Call to a member function get_error_code() on false at rest.php:468
    == control: JSON code 400 with error ==
    returned WP_REST_Response {"data":null,"message":"Error creating account. Some API error","code":"error"}
    

Screenshots, screen recording, code snippet or Help Scout ticket

false takes the WP_Error branch and is used as an object:
https://github.com/Codeinwp/optimole-wp/blob/v4.2.17/inc/rest.php#L467-L468

if ( $user === false || is_wp_error( $user ) ) {
    if ( $user->get_error_code() === 'domain_not_accessible' ) {

The same $user->get_error_message() call is repeated at https://github.com/Codeinwp/optimole-wp/blob/v4.2.17/inc/rest.php#L482.

The places where request() returns false:
https://github.com/Codeinwp/optimole-wp/blob/v4.2.17/inc/api.php#L180-L191 and https://github.com/Codeinwp/optimole-wp/blob/v4.2.17/inc/api.php#L217-L228

if ( empty( $response ) ) { return false; }
$response = json_decode( $response, true );
...
if ( ! isset( $response['code'] ) ) { return false; }

The sibling handlers already guard the error-object calls with is_wp_error(): https://github.com/Codeinwp/optimole-wp/blob/v4.2.17/inc/rest.php#L377-L390

Core does not catch the error (call_user_func( $handler['callback'], $request ) in WP_REST_Server::respond_to_request(), wp-includes/rest-api/class-wp-rest-server.php:1293 in WordPress 7.1.3). The fatal handler answers with HTTP 500 (wp-includes/class-wp-fatal-error-handler.php:212).

A likely fix is to only call get_error_code() / get_error_message() when is_wp_error( $user ), and return the plain "Error creating account." response for false.

Environment info

  • Optimole 4.2.17 (the code is the same on master at 63f3f7c)
  • Any WordPress version the plugin supports (5.5 and later; core lines cited from 7.1.3)
  • PHP 7.4 and later. Calling a method on false throws Error on every PHP 7 and 8 version. Reproduced on PHP 8.4.19.

Is the issue you are reporting a regression

Yes. Up to 3.11.3, register_service() returned "Error creating account." for false without calling any method on it (https://github.com/Codeinwp/optimole-wp/blob/v3.11.3/inc/rest.php#L357-L366). The $user->get_error_code() call first ships in 3.12.0.

Found while writing the WordPress compatibility checks for Optimole.

Activity

  1. added
    bugThis label could be used to identify issues that are caused by a defect in the product.
    on Oct 28, 2026
  2. added theissue type on Oct 28, 2026
  3. girishpanchal30 commented on Oct 29, 2026

    @girishpanchal30
    Contributor

    I have fixed it with PR #1210

  4. milicavs commented on Oct 29, 2026

    @milicavs

    🧪 QA steps and test links

    When Optimole's account service answers a sign-up with an empty or broken reply (for example a proxy's 502 page), creating an account from the Optimole dashboard crashes with a PHP fatal error. The dashboard then shows its generic "core REST API functionality is not available" banner instead of a normal "Error creating account." message.

    Link Build Bot check
    Reproduce the bug Optimole 4.2.17 (released) ✅ Reproduced: red REST API banner, and Call to a member function get_error_code() on false in the log
    Verify the fix PR #1210 build 8e82cd2 ✅ Fixed: "Error creating account." under the email field, no fatal error

    In the links:

    • Steps 1 to 2 are already done, and the links open on the Optimole dashboard (not connected).
    • A must-use plugin, "QA: fake an empty Optimole sign-up reply", does what the Code Snippet in step 1 does, so no real account is created.
    • Tools → QA Logs shows debug.log.

    Reproduce on Optimole 4.2.17 (with Optimole not connected):

    1. Snippets → Add New, title "Fake empty Optimole sign-up reply", code below, run everywhere, Save and Activate:
      add_filter( 'pre_http_request', function ( $pre, $args, $url ) { return false !== strpos( $url, 'complete_register_remote' ) ? [ 'headers' => [], 'body' => '', 'response' => [ 'code' => 502, 'message' => 'Bad Gateway' ], 'cookies' => [], 'filename' => null ] : $pre; }, 10, 3 );
    2. Go to Optimole in the admin menu.
    3. Leave any email in "Your email address" and click Create & connect your account.
    4. Check Tools → QA Logs (or debug.log).
    • Bug: a red banner says the core REST API functionality is not available, and the log has PHP Fatal error: Uncaught Error: Call to a member function get_error_code() on false.
    • Fixed: "Error creating account." shows under the email field, and the log has no fatal error.
    Edge cases
    • Snippet returns 'body' => '<html><body><h1>502 Bad Gateway</h1></body></html>' (HTML page): "Error creating account.", no fatal.
    • Snippet returns code 400 with body {"code":400,"error":"Some API error"}: "Error creating account. Some API error".
    • Snippet returns code 400 with body {"code":400,"error":"This email address is already registered."}: the "email is already registered" message, as before.
    • Deactivate the snippet and sign up with a real test email: the account connects as before.

    Fix: #1210 (open)

  5. pirate-bot commented on Oct 29, 2026

    @pirate-bot
    Collaborator

    QA agent — 🟢 Passing checks · Sign-up now handles broken API replies without crashing.

    With Optimole disconnected and the account service mocked, all four reported bad replies—and eight extra malformed replies—returned the normal account error without crashing. The dashboard displayed the expected error, and normal API errors and website-access guidance stayed intact. The same sign-up action crashed on the base build but passed with this PR. The added tests passed all 8 cases and 22 assertions.

    Worth a human look

    1. Try a real sign-up with a dedicated test email and confirm the account connects; the automated QA hint's successful-registration path was not exercised against Optimole's live service.
    Checks (3 passed)
    • ✅ Creating an account from the unconnected Optimole dashboard with an unusable API reply returns HTTP 200 with code error and message Error creating account., shows that normal message under the email field, and does not produce a PHP fatal or the generic REST-error banner; cover empty body, HTML, JSON without code, and non-200 code without error.
    • ✅ Creating an account when the API supplies a normal error preserves HTTP 200, code error, null data, and the Error creating account. prefix followed by the API error message, with the full message visible on the dashboard and no generic REST-error banner.
    • ✅ Creating an account when the API reports domain_not_accessible preserves the dedicated HTTP 200 response with code domain_not_accessible, null data and website-access guidance, and the dashboard displays that guidance rather than the generic account or REST-error message.

    Full report, evidence and session trail

  6. pirate-bot commented on Nov 2, 2026

    @pirate-bot
    Collaborator

    🎉 This issue has been resolved in version 4.2.18 🎉

    The release is available on GitHub release

    Your semantic-release bot 📦🚀

  7. added
    releasedIndicate that an issue has been resolved and released in a particular version of the product.
    on Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugThis label could be used to identify issues that are caused by a defect in the product.releasedIndicate that an issue has been resolved and released in a particular version of the product.

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions