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

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

Open
erikyo opened this issue Feb 7, 2023 · 0 comments
Open

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

erikyo opened this issue Feb 7, 2023 · 0 comments

Comments

@erikyo
Copy link

erikyo commented Feb 7, 2023

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

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

No branches or pull requests

1 participant