Correctly handle failing to connect to the given session in client mode

This commit is contained in:
Maxime Coste 2014-03-21 13:42:37 +00:00
parent 2cdf578834
commit 459cb212e5
3 changed files with 23 additions and 3 deletions

View File

@ -228,6 +228,11 @@ int run_client(const String& session, const String& init_command)
fputs("disconnected from server\n", stderr); fputs("disconnected from server\n", stderr);
return -1; return -1;
} }
catch (connection_failed& e)
{
fputs(e.what(), stderr);
return -1;
}
return 0; return 0;
} }
@ -254,7 +259,15 @@ int kakoune(const ParametersParser& parser)
} }
command += String{buf, buf + count}; command += String{buf, buf + count};
} }
send_command(parser.option_value("p"), command); try
{
send_command(parser.option_value("p"), command);
}
catch (connection_failed& e)
{
fputs(e.what(), stderr);
return -1;
}
return 0; return 0;
} }

View File

@ -448,7 +448,7 @@ std::unique_ptr<RemoteClient> connect_to(const String& session, std::unique_ptr<
addr.sun_family = AF_UNIX; addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, filename.c_str(), sizeof(addr.sun_path) - 1); strncpy(addr.sun_path, filename.c_str(), sizeof(addr.sun_path) - 1);
if (connect(sock, (sockaddr*)&addr, sizeof(addr.sun_path)) == -1) if (connect(sock, (sockaddr*)&addr, sizeof(addr.sun_path)) == -1)
throw runtime_error("connect to " + filename + " failed"); throw connection_failed(filename);
return std::unique_ptr<RemoteClient>{new RemoteClient{sock, std::move(ui), init_command}}; return std::unique_ptr<RemoteClient>{new RemoteClient{sock, std::move(ui), init_command}};
} }
@ -463,7 +463,7 @@ void send_command(const String& session, const String& command)
addr.sun_family = AF_UNIX; addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, filename.c_str(), sizeof(addr.sun_path) - 1); strncpy(addr.sun_path, filename.c_str(), sizeof(addr.sun_path) - 1);
if (connect(sock, (sockaddr*)&addr, sizeof(addr.sun_path)) == -1) if (connect(sock, (sockaddr*)&addr, sizeof(addr.sun_path)) == -1)
throw runtime_error("connect to " + filename + " failed"); throw connection_failed(filename);
{ {
Message msg(sock); Message msg(sock);

View File

@ -10,6 +10,13 @@ namespace Kakoune
struct peer_disconnected {}; struct peer_disconnected {};
struct connection_failed : runtime_error
{
connection_failed(const String& filename)
: runtime_error{"connect to " + filename + " failed"}
{}
};
// A remote client handle communication between a client running on the server // A remote client handle communication between a client running on the server
// and a user interface running on the local process. // and a user interface running on the local process.
class RemoteClient class RemoteClient