Skip to content

Commit

Permalink
Let the callback also handle the error itself
Browse files Browse the repository at this point in the history
- Also fix an unwrap in Rust.

Close #500
  • Loading branch information
liuchengxu committed Aug 18, 2020
1 parent 34610d6 commit 191553a
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 16 deletions.
8 changes: 1 addition & 7 deletions autoload/clap/client.vim
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,8 @@ function! clap#client#handle(msg) abort
return
endif

if has_key(decoded, 'error')
" TODO: show the error message in preview window when it's on_move
call clap#helper#echo_error('[client_handle] '.string(decoded.error))
return
endif

if has_key(s:handlers, decoded.id)
call s:handlers[decoded.id](decoded.result)
call s:handlers[decoded.id](get(decoded, 'result', v:null), get(decoded, 'error', v:null))
call remove(s:handlers, decoded.id)
return
endif
Expand Down
5 changes: 4 additions & 1 deletion autoload/clap/impl/on_move.vim
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ if clap#maple#is_available()

let s:async_preview_implemented = ['files', 'git_files', 'grep', 'grep2', 'proj_tags', 'tags', 'blines', 'history']

function! s:handle_on_move_result(result) abort
function! s:handle_on_move_result(result, error) abort
if a:error isnot v:null
return
endif
if has_key(a:result, 'lines')
try
call g:clap.preview.show(a:result.lines)
Expand Down
5 changes: 4 additions & 1 deletion autoload/clap/provider/commits.vim
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ function! s:commits.on_move() abort
call clap#provider#commits#on_move_common('git show '.rev)
endfunction

function! clap#provider#commits#on_move_callback(result) abort
function! clap#provider#commits#on_move_callback(result, error) abort
if a:error isnot v:null
return
endif
let lines = a:result.lines
call clap#preview#show_lines(lines, 'diff', -1)
call clap#preview#highlight_header()
Expand Down
24 changes: 18 additions & 6 deletions autoload/clap/provider/filer.vim
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,17 @@ function! clap#provider#filer#hi_empty_dir() abort
hi default link ClapEmptyDirectory WarningMsg
endfunction

function! s:handle_result(result) abort
function! s:handle_error(error) abort
let s:filer_error_cache[a:error.dir] = a:error.message
call g:clap.display.set_lines([a:error.message])
call clap#indicator#set('[??]')
endfunction

function! s:handle_result(result, error) abort
if a:error isnot v:null
call s:handle_error(a:error)
return
endif
if a:result.total == 0
let s:filer_empty_cache[a:result.dir] = s:DIRECTORY_IS_EMPTY
call g:clap.display.set_lines([s:DIRECTORY_IS_EMPTY])
Expand All @@ -30,10 +40,7 @@ endfunction

function! clap#provider#filer#daemon_handle(decoded) abort
if has_key(a:decoded, 'error')
let error = a:decoded.error
let s:filer_error_cache[error.dir] = error.message
call g:clap.display.set_lines([error.message])
call clap#indicator#set('[??]')
call s:handle_error(a:decoded.error)
return
endif

Expand Down Expand Up @@ -226,7 +233,12 @@ function! s:sync_on_move_impl() abort
endif
endfunction

function! s:filer_handle_on_move_result(result) abort
function! s:filer_handle_on_move_result(result, error) abort
if a:error isnot v:null
call s:handle_error(a:error)
return
endif

if empty(a:result.lines)
call g:clap.preview.show(['Empty entries'])
else
Expand Down
6 changes: 5 additions & 1 deletion crates/stdio_server/src/filer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ impl HandleMessage for FilerMessageHandler {
context,
inner: OnMove::Filer(path),
};
on_move_handler.handle().unwrap();
if let Err(err) = on_move_handler.handle() {
let error = json!({"message": format!("{}", err), "dir": msg.get_cwd()});
let res = json!({ "id": msg.id, "provider_id": "filer", "error": error });
write_response(res);
}
}
// TODO: handle on_typed
RpcMessage::OnTyped(msg) => handle_message(msg),
Expand Down

0 comments on commit 191553a

Please # to comment.