Skip to content

Conversation

@justlevine
Copy link
Contributor

@justlevine justlevine commented Aug 16, 2025

What

This PR updates various WP_Ability methods to return an explicit WP_Error object instead of logging a _doing_it_wrong() and returning null

Why

Refactor for #14

How

  • Now that we can return WP_Error, null is a valid response from execute(), and the test has been updated accordingly. If we want, we could map null to an ability_unknown_execution_error or something.
  • The only WP_Error that's not bubbled out to the WP_REST_Response is the WP_Ability::get_permissions() error, so as not to leak a potentially sensitive error to a user without the proper permissions.

@justlevine
Copy link
Contributor Author

Looks like my IDE phpcbfd our tests file. Since all the other code PRs right now are mine and I don't mind eating any conflicts, im'a leave it as is unless told otherwise.

@gziolo gziolo added the [Type] Enhancement New feature or request label Aug 18, 2025
Copy link
Member

@gziolo gziolo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left some initial feedback. Thank you for taking the time to refactor the code.

return true;
}

$valid_input = rest_validate_value_from_schema( $input, $input_schema );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like it could return the value here because rest_validate_value_from_schema returns true or WP_Error.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking a bit more about it after seeing the updated unit tests, we should remap these WP_Errors so the names are more tied to the abilty, for example:

rest_invalid_type -> ability_input_invalid_type

Could we replace the prefix?

@gziolo
Copy link
Member

gziolo commented Aug 18, 2025

@emdashcodes, this could use some sanity check from you.

return $result;
}

$output_validation = $this->validate_output( $ability, $result );
Copy link
Member

@gziolo gziolo Aug 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both $this->validate_input() and $this->validate_output() calls should no longer be needed as they are handled internally inside $ability->execute() with similar WP_Errors. We would need to differentiate mostly the error names to the proper status code in the response: 400 vs 500.

}

return true;
return is_wp_error( $valid_output ) ? $valid_output : true;
Copy link
Member

@gziolo gziolo Aug 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar feedback as for input handling since we use rest_validate_value_from_schema which happens to be tied to much to REST API at this level.

@gziolo
Copy link
Member

gziolo commented Aug 18, 2025

Looks like my IDE phpcbfd our tests file. Since all the other code PRs right now are mine and I don't mind eating any conflicts, im'a leave it as is unless told otherwise.

I noticed similar issues when running composer format on wordpress-develop. I opened new PR to address that subset of changes:

@codecov
Copy link

codecov bot commented Aug 19, 2025

Codecov Report

❌ Patch coverage is 88.00000% with 3 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (trunk@e61dab5). Learn more about missing BASE report.

Files with missing lines Patch % Lines
includes/abilities-api/class-wp-ability.php 87.50% 3 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##             trunk      #23   +/-   ##
========================================
  Coverage         ?   92.48%           
  Complexity       ?       99           
========================================
  Files            ?        7           
  Lines            ?      519           
  Branches         ?        0           
========================================
  Hits             ?      480           
  Misses           ?       39           
  Partials         ?        0           
Flag Coverage Δ
unit 92.48% <88.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@gziolo
Copy link
Member

gziolo commented Aug 19, 2025

Resolving merge conflicts on mobile is hard but I think I made it 😆

I plan to land changes and look into my feedback separately. This PR largely addresses the issue but we still can add some final touches to reuse the builtin schema validation inside execute method inside the ability ti prevent double processing.

@gziolo gziolo merged commit fb95300 into WordPress:trunk Aug 19, 2025
17 checks passed
@justlevine justlevine deleted the dev/wp_ability-return-error branch August 20, 2025 15:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Type] Enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants