1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-11-24 11:37:32 +03:00

Add error handling when NvimServer could not be started correctly

This commit is contained in:
Tae Won Ha 2019-03-25 11:47:48 +01:00
parent 77847f70e9
commit b84cfb1f3c
No known key found for this signature in database
GPG Key ID: E40743465B5B8B44
8 changed files with 82 additions and 0 deletions

View File

@ -30,6 +30,7 @@ int main(int argc, const char *argv[]) {
"Started NvimServer '%s' and connected it with GUI '%s'.",
local_port_name, remote_port_name
);
CFRunLoopRun();
os_log_debug(logger, "NvimServer exiting.");

View File

@ -84,6 +84,12 @@ static void do_autocmd_guienter(void **argv);
// We declare nvim_main because it's not declared in any header files of neovim
extern int nvim_main(int argc, const char **argv);
#ifdef DEBUG
void debug_function() {
}
#endif
void server_set_nvim_args(int argc, const char **const argv) {
nvim_argc = argc + 1;
@ -108,6 +114,14 @@ void server_init_local_port(const char *name) {
);
CFRelease(name_cf);
if (local_port == NULL) {
send_msg_packing(NvimServerMsgIdFatalError, ^(msgpack_packer *packer) {
msgpack_pack_int64(packer, NvimServerFatalErrorCodeLocalPort);
});
exit(NvimServerFatalErrorCodeLocalPort);
}
cond_var_t cond_var;
cond_var_init(&cond_var, 5, false);
@ -136,6 +150,14 @@ void server_init_remote_port(const char *name) {
);
remote_port = CFMessagePortCreateRemote(kCFAllocatorDefault, name_cf);
CFRelease(name_cf);
if (remote_port == NULL) {
send_msg_packing(NvimServerMsgIdFatalError, ^(msgpack_packer *packer) {
msgpack_pack_int64(packer, NvimServerFatalErrorCodeRemotePort);
});
exit(NvimServerFatalErrorCodeRemotePort);
}
}
void server_destroy_remote_port() {

View File

@ -24,4 +24,8 @@ void send_msg_packing(NvimServerMsgId msgid, pack_block body);
void msgpack_pack_bool(msgpack_packer *packer, bool value);
void msgpack_pack_cstr(msgpack_packer *packer, const char *cstr);
#ifdef DEBUG
void debug_function(void);
#endif
#endif // NVIMSERVER_SERVER_H

View File

@ -46,9 +46,16 @@ typedef CF_ENUM(NSInteger, NvimServerMsgId) {
NvimServerMsgIdAutoCommandEvent,
NvimServerMsgIdRpcEventSubscribed,
NvimServerMsgIdFatalError,
NvimServerMsgIdDebug1,
};
typedef CF_ENUM(NSInteger, NvimServerFatalErrorCode) {
NvimServerFatalErrorCodeLocalPort = 1,
NvimServerFatalErrorCodeRemotePort,
};
typedef CF_ENUM(NSInteger, NvimBridgeMsgId) {
NvimBridgeMsgIdAgentReady = 0,
NvimBridgeMsgIdReadyForRpcEvents,

View File

@ -431,6 +431,41 @@ extension NvimView {
self.eventsSubject.onNext(.rpcEventSubscribed)
}
final func bridgeHasFatalError(_ value: MessagePackValue?) {
gui.async {
let alert = NSAlert()
alert.addButton(withTitle: "OK")
alert.messageText = "Error launching background neovim process"
alert.alertStyle = .critical
if let rawCode = value?.intValue,
let code = NvimServerFatalErrorCode(rawValue: rawCode) {
switch code {
case .localPort:
alert.informativeText = "GUI could not connect to the background " +
"neovim process. The window will close."
case .remotePort:
alert.informativeText = "The remote message port could not " +
"connect to GUI. The window will close."
}
} else {
alert.informativeText = "There was an unknown error launching the " +
"background neovim Process. " +
"The window will close."
}
alert.runModal()
self.queue.async {
self.eventsSubject.onNext(.neoVimStopped)
self.eventsSubject.onCompleted()
}
}
}
final func setAttr(with value: MessagePackValue) {
guard let array = value.arrayValue else {
self.bridgeLogger.error("Could not convert \(value)")

View File

@ -311,6 +311,8 @@ public class NvimView: NSView,
case .rpcEventSubscribed:
self?.rpcEventSubscribed()
case let .fatalError(value):
self?.bridgeHasFatalError(value)
case .debug1:
self?.debug1(nil)

View File

@ -46,9 +46,16 @@ typedef CF_ENUM(NSInteger, NvimServerMsgId) {
NvimServerMsgIdAutoCommandEvent,
NvimServerMsgIdRpcEventSubscribed,
NvimServerMsgIdFatalError,
NvimServerMsgIdDebug1,
};
typedef CF_ENUM(NSInteger, NvimServerFatalErrorCode) {
NvimServerFatalErrorCodeLocalPort = 1,
NvimServerFatalErrorCodeRemotePort,
};
typedef CF_ENUM(NSInteger, NvimBridgeMsgId) {
NvimBridgeMsgIdAgentReady = 0,
NvimBridgeMsgIdReadyForRpcEvents,

View File

@ -37,6 +37,7 @@ class UiBridge {
case autoCommandEvent(MessagePackValue)
case highlightAttrs(MessagePackValue)
case rpcEventSubscribed
case fatalError(MessagePackValue?)
case debug1
case unknown
}
@ -255,6 +256,9 @@ class UiBridge {
case .rpcEventSubscribed:
self.streamSubject.onNext(.rpcEventSubscribed)
case .fatalError:
self.streamSubject.onNext(.fatalError(MessagePackUtils.value(from: data)))
}
}