Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update validation for analytics shared requests #5727

Merged
merged 9 commits into from
Aug 24, 2022

Conversation

nfmohit
Copy link
Collaborator

@nfmohit nfmohit commented Aug 23, 2022

Summary

Addresses issue:

Relevant technical choices

This is a follow-up PR addressing this code review.

PR Author Checklist

  • My code is tested and passes existing unit tests.
  • My code has an appropriate set of unit tests which all pass.
  • My code is backward-compatible with WordPress 4.7 and PHP 5.6.
  • My code follows the WordPress coding standards.
  • My code has proper inline documentation.
  • I have added a QA Brief on the issue linked above.
  • I have signed the Contributor License Agreement (see https://cla.developers.google.com/).

Do not alter or remove anything below. The following sections will be managed by moderators only.

Code Reviewer Checklist

  • Run the code.
  • Ensure the acceptance criteria are satisfied.
  • Reassess the implementation with the IB.
  • Ensure no unrelated changes are included.
  • Ensure CI checks pass.
  • Check Storybook where applicable.
  • Ensure there is a QA Brief.

Merge Reviewer Checklist

  • Ensure the PR has the correct target branch.
  • Double-check that the PR is okay to be merged.
  • Ensure the corresponding issue has a ZenHub release assigned.
  • Add a changelog message to the issue.

@nfmohit nfmohit changed the base branch from develop to main August 23, 2022 20:45
@nfmohit nfmohit mentioned this pull request Aug 23, 2022
18 tasks
Copy link
Collaborator

@aaemnnosttv aaemnnosttv left a comment

Choose a reason for hiding this comment

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

Thanks @nfmohit - this looks great, just a few minor points to address 👍

includes/Core/Modules/Module.php Outdated Show resolved Hide resolved
Copy link
Collaborator

@aaemnnosttv aaemnnosttv left a comment

Choose a reason for hiding this comment

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

One more thing 😄

includes/Modules/Analytics.php Outdated Show resolved Hide resolved
includes/Modules/Analytics.php Outdated Show resolved Hide resolved
@nfmohit
Copy link
Collaborator Author

nfmohit commented Aug 23, 2022

Thank you for the kind reviews @aaemnnosttv! I've addressed them.

@nfmohit nfmohit requested a review from aaemnnosttv August 23, 2022 22:16
Copy link
Collaborator

@aaemnnosttv aaemnnosttv left a comment

Choose a reason for hiding this comment

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

Thanks @nfmohit ! I think we were a bit ambitious with the addition of the exceptions here and after giving it another look, I think it was a bit simpler before and while I still think they are good to use, we can aim for a bit of a middle ground here.

Apologies for the iterations – I've gone ahead and made the final adjustments (mostly undoing some of the additions here) to wrap this up for the release, but see my comments below.

* @param array $dimensions Optional. Dimensions that are invalid.
*/
public function __construct( $message = '', $code = 0, $previous = null, $dimensions = array() ) {
if ( ! isset( $message ) || empty( $message ) ) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Using isset here is redundant as empty uses it internally.

Suggested change
if ( ! isset( $message ) || empty( $message ) ) {
if ( empty( $message ) ) {

Comment on lines 40 to 53
$message = sprintf(
/* translators: %s is replaced with a comma separated list of the invalid dimensions. */
_n(
'Unsupported dimension requested: %s',
'Unsupported dimensions requested: %s',
count( $dimensions ),
'google-site-kit'
),
join(
/* translators: used between list items, there is a space after the comma. */
__( ', ', 'google-site-kit' ),
$dimensions
)
);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Looking at this again, none of our other exceptions handle the message internally. I think it makes sense to leave this with the consumer for now.

Comment on lines 66 to 74
public function to_wp_error() {
return new WP_Error(
static::WP_ERROR_CODE,
$this->getMessage(),
array(
'status' => 400, // Bad request.
)
);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I realize this was based on my initial thoughts but after seeing it in use here, I don't think it's right for this use case.

I think the WP Error is best returned from the module class, which can then be module-specific. It also seems a bit improper to dictate the status code here – especially as a 400 which indicates that the request is not valid as given which is not really accurate in this case as the validation is a sharing specific detail. The request wouldn't be any different and not throw if the module wasn't shared.

array( 'status' => 400 )
);
}
$this->validate_report_dimensions( $dimensions );
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this was mostly correct before. The only change to really make is adding a try/catch around the validation and returning the appropriate WP_Error.

return new WP_Error(
'invalid_dimensions',
$invalid_dimensions_error_message,
array( 'status' => 400 )
Copy link
Collaborator

Choose a reason for hiding this comment

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

As mentioned before, 400 isn't really the proper status here. We can keep this as the default 500 as this is more of an "internal server" error than something wrong with the request itself, even though it is directly related to the requested data. The only reason it's an error is due to the internal server state related to sharing.

),
implode( ', ', $invalid_metrics )
);
throw new Invalid_Metrics_Exception( '', 0, null, $invalid_metrics );
Copy link
Collaborator

Choose a reason for hiding this comment

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

As mentioned before, let's give the message to the exception rather than have it generate its own. This makes the instantiation much simpler as well as we don't need to provide it with the metrics (although maybe this would make sense to do in the future), it's not needed yet.

@nfmohit
Copy link
Collaborator Author

nfmohit commented Aug 25, 2022

Thanks @nfmohit ! I think we were a bit ambitious with the addition of the exceptions here and after giving it another look, I think it was a bit simpler before and while I still think they are good to use, we can aim for a bit of a middle ground here.

Apologies for the iterations – I've gone ahead and made the final adjustments (mostly undoing some of the additions here) to wrap this up for the release, but see my comments below.

I've looked at all the mentioned suggestions, the implemented changes, and I agree with all of them. Thank you very much for showing me the right track here and implementing the appropriate changes, @aaemnnosttv!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants