private function cleanErrorResponse($error) {
    if (strpos($error, '<html') !== false) {
      $error = preg_replace('/<(style|title|script)[^>]*>[^<]*<\/(style|title|script)>/i', '', $error);
      return trim(preg_replace('/\s+/', ' ', strip_tags($error)));
    }
    else
    {
      $json = json_decode($error);
      return isset($json->error->message) ? strval($json->error->message) : $error;
    }
  }