From d690f8d576de512554605703d9b57e1684d222e4 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Mon, 28 Jul 2014 19:50:09 +0900 Subject: [PATCH] Change error reporting method As suggested by @vheon: https://github.com/junegunn/vim-plug/pull/40#issuecomment-50278543 --- plug.vim | 54 ++++++++++++++++++++------------------------- test/workflow.vader | 22 ++++++++---------- 2 files changed, 33 insertions(+), 43 deletions(-) diff --git a/plug.vim b/plug.vim index db70c6c..8fcb1b7 100644 --- a/plug.vim +++ b/plug.vim @@ -86,21 +86,18 @@ function! plug#begin(...) elseif !empty(&rtp) let home = s:path(split(&rtp, ',')[0]) . '/plugged' else - echoerr "Unable to determine plug home. Try calling plug#begin() with a path argument." - return 0 + return s:err('Unable to determine plug home. Try calling plug#begin() with a path argument.') endif if !isdirectory(home) try call mkdir(home, 'p') catch - echoerr 'Invalid plug directory: '. home - return 0 + return s:err('Invalid plug directory: '. home) endtry endif if !executable('git') - echoerr "`git' executable not found. vim-plug requires git." - return 0 + return s:err('`git` executable not found. vim-plug requires git.') endif let g:plug_home = home @@ -125,8 +122,7 @@ endfunction function! plug#end() if !exists('g:plugs') - echoerr 'Call plug#begin() first' - return + return s:err('Call plug#begin() first') endif let keys = keys(g:plugs) let plugfiles = s:find_plugfiles() @@ -226,6 +222,13 @@ else endfunction endif +function! s:err(msg) + echohl ErrorMsg + echom a:msg + echohl None + return 0 +endfunction + function! s:esc(path) return substitute(a:path, ' ', '\\ ', 'g') endfunction @@ -293,11 +296,9 @@ endfunction function! s:add(force, repo, ...) if a:0 > 1 - echoerr "Invalid number of arguments (1..2)" - return + return s:err('Invalid number of arguments (1..2)') endif - let exception = '' try let repo = s:trim_trailing_slashes(a:repo) let name = s:extract_name(repo) @@ -315,27 +316,22 @@ function! s:add(force, repo, ...) let g:plugs[name] = spec let g:plugs_order += [name] catch - let exception = v:exception + return s:err(v:exception) endtry - if !empty(exception) - echoerr exception - endif endfunction function! s:parse_options(arg) let opts = { 'branch': 'master', 'frozen': 0, 'local': 0 } - if !empty(a:arg) - let type = type(a:arg) - if type == s:TYPE.string - let opts.branch = a:arg - elseif type == s:TYPE.dict - call extend(opts, a:arg) - if has_key(opts, 'tag') - let opts.branch = remove(opts, 'tag') - endif - else - throw "Invalid argument type (expected: string or dictionary)" + let type = type(a:arg) + if type == s:TYPE.string + let opts.branch = a:arg + elseif type == s:TYPE.dict + call extend(opts, a:arg) + if has_key(opts, 'tag') + let opts.branch = remove(opts, 'tag') endif + else + throw 'Invalid argument type (expected: string or dictionary)' endif return opts endfunction @@ -1068,8 +1064,7 @@ function! s:upgrade() echo "Downloaded ". s:plug_source return 1 else - echoerr "Error upgrading vim-plug" - return 0 + return s:err('Error upgrading vim-plug') endif elseif has('ruby') echo "Downloading ". s:plug_source @@ -1089,8 +1084,7 @@ EOF echo "Downloaded ". s:plug_source return 1 else - echoerr "curl executable or ruby support not found" - return 0 + return s:err('curl executable or ruby support not found') endif endfunction diff --git a/test/workflow.vader b/test/workflow.vader index 16fca9a..313fbba 100644 --- a/test/workflow.vader +++ b/test/workflow.vader @@ -39,12 +39,10 @@ Execute (Initialize test environment): let $MYVIMRC = vimrc Execute (plug#end() before plug#begin() should fail): - try - call plug#end() - Assert 0, 'should not reach here' - catch - Assert stridx(v:exception, 'Call plug#begin() first') >= 0 - endtry + redir => out + AssertEqual 0, plug#end() + redir END + Assert stridx(out, 'Call plug#begin() first') >= 0 Execute (plug#begin() without path argument): call plug#begin() @@ -54,12 +52,10 @@ Execute (plug#begin() without path argument): Execute (plug#begin() without path argument with empty &rtp): let save_rtp = &rtp set rtp= - try - call plug#begin() - Assert 0, 'should not reach here' - catch - Assert stridx(v:exception, 'Unable to determine plug home') >= 0, 'Got: '.v:exception - endtry + redir => out + AssertEqual 0, plug#begin() + redir END + Assert stridx(out, 'Unable to determine plug home') >= 0 let &rtp = save_rtp Execute (plug#begin(path)): @@ -671,7 +667,7 @@ Execute (Cleanup): unlet g:plugs unlet g:plug_home unlet g:vimrc_reloaded - unlet temp_plugged vader plug basertp save_rtp repo lnum fzf + unlet temp_plugged vader plug basertp save_rtp repo lnum fzf out delf PlugStatusSorted delf AssertExpect delf PlugUpdated