Skip to content

Commit

Permalink
Improve error messages for CSS @import failures
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed May 23, 2019
1 parent ee9fbb5 commit fd23dc0
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions includes/sanitizers/class-amp-style-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -1227,12 +1227,20 @@ private function fetch_external_stylesheet( $url ) {
$cache_key = md5( $url );
$contents = get_transient( $cache_key );
if ( false === $contents ) {
$r = wp_remote_get( $url );
if ( 200 !== wp_remote_retrieve_response_code( $r ) ) {
$contents = new WP_Error(
wp_remote_retrieve_response_code( $r ),
wp_remote_retrieve_response_message( $r )
);
$r = wp_remote_get( $url );
$code = wp_remote_retrieve_response_code( $r );
if ( $code < 200 || $code >= 300 ) {
$message = wp_remote_retrieve_response_message( $r );
if ( ! $code ) {
$code = 'http_error';
} else {
$code = "http_{$code}";
}
if ( ! $message ) {
/* translators: %s: the fetched URL */
$message = sprintf( __( 'Failed to fetch: %s', 'amp' ), $url );
}
$contents = new WP_Error( $code, $message );
} elseif ( ! preg_match( '#^text/css#', wp_remote_retrieve_header( $r, 'content-type' ) ) ) {
$contents = new WP_Error(
'no_css_content_type',
Expand Down Expand Up @@ -1395,6 +1403,7 @@ private function parse_import_stylesheet( Import $item, CSSList $css_list, $opti
'code' => $contents->get_error_code(),
'message' => $contents->get_error_message(),
'type' => AMP_Validation_Error_Taxonomy::CSS_ERROR_TYPE,
'url' => $import_stylesheet_url,
);
$sanitized = $this->should_sanitize_validation_error( $error );
if ( $sanitized ) {
Expand All @@ -1410,6 +1419,7 @@ private function parse_import_stylesheet( Import $item, CSSList $css_list, $opti
'code' => $css_file_path->get_error_code(),
'message' => $css_file_path->get_error_message(),
'type' => AMP_Validation_Error_Taxonomy::CSS_ERROR_TYPE,
'url' => $import_stylesheet_url,
);
$sanitized = $this->should_sanitize_validation_error( $error );
if ( $sanitized ) {
Expand Down

0 comments on commit fd23dc0

Please sign in to comment.