February 27, 2012

umen242 umen242
Ant Farmer
330 posts

is there generic method to get the network error string out of the enum NetworkError or do i need to build one ?

 

I have basic function that prints network errors based on enum NetworkError.
that looks like this :

  1. void HttpClient::HandleNetworkError(QNetworkReply::NetworkError& networkError)
  2. {
  3.   switch(networkError)
  4.   {
  5.   case(QNetworkReply::ConnectionRefusedError):
  6.    LOG_MSG("NO NETWORK CONNECTION ConnectionRefusedError!! ");
  7.    break;
  8.   case(QNetworkReply::HostNotFoundError):
  9.    //handle the html output is no internet connection is found
  10.    LOG_MSG("NO NETWORK CONNECTION HostNotFoundError!! ");
  11.    break;
  12.   case(QNetworkReply::SslHandshakeFailedError):
  13.    //handle the html output is no internet connection is found
  14.    LOG_MSG("CONNECTION SslHandshakeFailedError!! ");
  15.    break;
  16.   case(QNetworkReply::UnknownContentError):
  17.    LOG_MSG("CONNECTION UnknownContentError!! ");
  18.    break;
  19.   default :
  20.    LOG_MSG("CONNECTION not defined default error UnknownContentError!! ");
  21.   }
  22.  
  23. }

now i need to support more errors , in fact all the error that list in enum NetworkError, so does it means i need to
added them all to this switch case ? or there is some kind of generic Qt functions that do this translation ?

5 replies

February 27, 2012

koahnig koahnig
Mad Scientist
2193 posts

under the signal error [developer.qt.nokia.com]
you can find

The code parameter contains the code of the error that was detected. Call errorString() to obtain a textual representation of the error condition.

Did you try errorString() ?

February 27, 2012

umen242 umen242
Ant Farmer
330 posts

well im using sync http request like this :

  1.  QNetworkReply *reply = networkManager->get(request);
  2.   connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
  3.      loop.exec();  
  4.   //return response
  5.   ByteArrayResponse=reply->readAll();
  6.   QNetworkReply::NetworkError networkError = reply->error();
  7.   HandleNetworkError(networkError);

so i dont think i can use it ? or i can ?

February 27, 2012

koahnig koahnig
Mad Scientist
2193 posts

I would give it a try. Since it is part of the docs for QNetworkReply it should work. Otherwise it is a bug in the docs and should be reported.

February 27, 2012

umen242 umen242
Ant Farmer
330 posts

but how … ?

February 27, 2012

koahnig koahnig
Mad Scientist
2193 posts

When you list all members of QNertworkReply [developer.qt.nokia.com] there is an entry errorString()
So this should work

  1. QString str = reply->errorString();

errorString is a member function of QIODevice and QNetworkReply inherits QIODevice.

 
  ‹‹ QNetworkManager fails to download in some cases [SOLVED]      Question about QScriptEngine and JSON.stringify ››

You must log in to post a reply. Not a member yet? Register here!