Skip to content

Commit

Permalink
see #607: return better error messages, make build_request_url private
Browse files Browse the repository at this point in the history
  • Loading branch information
stoyan0v committed Sep 7, 2017
1 parent dc853e7 commit 9f0b6b7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
14 changes: 10 additions & 4 deletions src/includes/class-wordlift-autocomplete-adapter.php
Expand Up @@ -68,8 +68,6 @@ public function wl_autocomplete() {

// Make request.
$response = $this->autocomplete_service->make_request( $query );
// Decode response body.
$suggestions = json_decode( wp_remote_retrieve_body( $response ), true );

// Clear any buffer.
ob_clean();
Expand All @@ -78,12 +76,20 @@ public function wl_autocomplete() {
if ( ! is_wp_error( $response ) && 200 === (int) $response['response']['code'] ) {
// Echo the response.
wp_send_json_success( array(
'suggestions' => $suggestions,
json_decode( wp_remote_retrieve_body( $response ), true )
) );
} else {
// Default error message.
$error_message = __( 'Something went wrong.' );

// Get the real error message if there is WP_Error.
if ( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
}

// There is an error, so send error message.
wp_send_json_error( array(
'message' => __( 'Something went wrong.' ),
'message' => $error_message,
) );
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/includes/class-wordlift-autocomplete-service.php
Expand Up @@ -78,7 +78,7 @@ public function make_request( $query ) {
*
* @return string Built url.
*/
public function build_request_url( $query ) {
private function build_request_url( $query ) {
$args = array(
'key' => $this->configuration_service->get_key(),
'language' => $this->configuration_service->get_language_code(),
Expand Down

0 comments on commit 9f0b6b7

Please sign in to comment.