-
Notifications
You must be signed in to change notification settings - Fork 296
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
Conversation
There was a problem hiding this 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/REST_API/Exception/Invalid_Dimensions_Exception.php
Outdated
Show resolved
Hide resolved
includes/Core/REST_API/Exception/Invalid_Dimensions_Exception.php
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One more thing 😄
Thank you for the kind reviews @aaemnnosttv! I've addressed them. |
There was a problem hiding this 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 ) ) { |
There was a problem hiding this comment.
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.
if ( ! isset( $message ) || empty( $message ) ) { | |
if ( empty( $message ) ) { |
$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 | ||
) | ||
); |
There was a problem hiding this comment.
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.
public function to_wp_error() { | ||
return new WP_Error( | ||
static::WP_ERROR_CODE, | ||
$this->getMessage(), | ||
array( | ||
'status' => 400, // Bad request. | ||
) | ||
); | ||
} |
There was a problem hiding this comment.
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.
includes/Modules/Analytics.php
Outdated
array( 'status' => 400 ) | ||
); | ||
} | ||
$this->validate_report_dimensions( $dimensions ); |
There was a problem hiding this comment.
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 ) |
There was a problem hiding this comment.
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.
includes/Modules/Analytics.php
Outdated
), | ||
implode( ', ', $invalid_metrics ) | ||
); | ||
throw new Invalid_Metrics_Exception( '', 0, null, $invalid_metrics ); |
There was a problem hiding this comment.
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.
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! |
Summary
Addresses issue:
Relevant technical choices
This is a follow-up PR addressing this code review.
PR Author Checklist
Do not alter or remove anything below. The following sections will be managed by moderators only.
Code Reviewer Checklist
Merge Reviewer Checklist