Skip to content

Provide a soft-fail when the response is not 200 #22

@erikyo

Description

@erikyo

I am using this awesome module to dynamically enqueue fonts but, since it is possible that it will fail to download some font families or weights, I would like if WPTT/webfont-loader could handle the error message and display it.
Currently the server error message is queued as a style (and this creates some layout glitches, at least with my template since it's an html message) and it would probably be better to display that error at the top of body (eg. like as when React makes a compilation error)

This is the code in which the request is made:

$response = wp_remote_get( $this->remote_url, array( 'user-agent' => $user_agent ) );
// Early exit if there was an error.
if ( is_wp_error( $response ) ) {
return '';
}
// Get the CSS from our response.
$contents = wp_remote_retrieve_body( $response );
return $contents;

I think it would be enough to add something like below to solve (just after "is_wp_error").

// The response is invalid, report the error message at the top of body and avoid to enqueue the error response as style.
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
  return "body::before {
	  content: 'WPTT Error: {$response['response']['message']}';
	  display: block;
	  font-family: monospace;
	  padding: 1rem;
	  background-color: black;
	  color:white;
	  font-weight: bold;
  }";
}

An error message for testing -> https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@700;600;500&family=Merriweather:wght@500;700&family=Material+Icons&display=swap

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions