Change error reporting method

As suggested by @vheon:
https://github.com/junegunn/vim-plug/pull/40#issuecomment-50278543
This commit is contained in:
Junegunn Choi 2014-07-28 19:50:09 +09:00
parent 8738341ad0
commit d690f8d576
2 changed files with 33 additions and 43 deletions

View File

@ -86,21 +86,18 @@ function! plug#begin(...)
elseif !empty(&rtp) elseif !empty(&rtp)
let home = s:path(split(&rtp, ',')[0]) . '/plugged' let home = s:path(split(&rtp, ',')[0]) . '/plugged'
else else
echoerr "Unable to determine plug home. Try calling plug#begin() with a path argument." return s:err('Unable to determine plug home. Try calling plug#begin() with a path argument.')
return 0
endif endif
if !isdirectory(home) if !isdirectory(home)
try try
call mkdir(home, 'p') call mkdir(home, 'p')
catch catch
echoerr 'Invalid plug directory: '. home return s:err('Invalid plug directory: '. home)
return 0
endtry endtry
endif endif
if !executable('git') if !executable('git')
echoerr "`git' executable not found. vim-plug requires git." return s:err('`git` executable not found. vim-plug requires git.')
return 0
endif endif
let g:plug_home = home let g:plug_home = home
@ -125,8 +122,7 @@ endfunction
function! plug#end() function! plug#end()
if !exists('g:plugs') if !exists('g:plugs')
echoerr 'Call plug#begin() first' return s:err('Call plug#begin() first')
return
endif endif
let keys = keys(g:plugs) let keys = keys(g:plugs)
let plugfiles = s:find_plugfiles() let plugfiles = s:find_plugfiles()
@ -226,6 +222,13 @@ else
endfunction endfunction
endif endif
function! s:err(msg)
echohl ErrorMsg
echom a:msg
echohl None
return 0
endfunction
function! s:esc(path) function! s:esc(path)
return substitute(a:path, ' ', '\\ ', 'g') return substitute(a:path, ' ', '\\ ', 'g')
endfunction endfunction
@ -293,11 +296,9 @@ endfunction
function! s:add(force, repo, ...) function! s:add(force, repo, ...)
if a:0 > 1 if a:0 > 1
echoerr "Invalid number of arguments (1..2)" return s:err('Invalid number of arguments (1..2)')
return
endif endif
let exception = ''
try try
let repo = s:trim_trailing_slashes(a:repo) let repo = s:trim_trailing_slashes(a:repo)
let name = s:extract_name(repo) let name = s:extract_name(repo)
@ -315,16 +316,12 @@ function! s:add(force, repo, ...)
let g:plugs[name] = spec let g:plugs[name] = spec
let g:plugs_order += [name] let g:plugs_order += [name]
catch catch
let exception = v:exception return s:err(v:exception)
endtry endtry
if !empty(exception)
echoerr exception
endif
endfunction endfunction
function! s:parse_options(arg) function! s:parse_options(arg)
let opts = { 'branch': 'master', 'frozen': 0, 'local': 0 } let opts = { 'branch': 'master', 'frozen': 0, 'local': 0 }
if !empty(a:arg)
let type = type(a:arg) let type = type(a:arg)
if type == s:TYPE.string if type == s:TYPE.string
let opts.branch = a:arg let opts.branch = a:arg
@ -334,8 +331,7 @@ function! s:parse_options(arg)
let opts.branch = remove(opts, 'tag') let opts.branch = remove(opts, 'tag')
endif endif
else else
throw "Invalid argument type (expected: string or dictionary)" throw 'Invalid argument type (expected: string or dictionary)'
endif
endif endif
return opts return opts
endfunction endfunction
@ -1068,8 +1064,7 @@ function! s:upgrade()
echo "Downloaded ". s:plug_source echo "Downloaded ". s:plug_source
return 1 return 1
else else
echoerr "Error upgrading vim-plug" return s:err('Error upgrading vim-plug')
return 0
endif endif
elseif has('ruby') elseif has('ruby')
echo "Downloading ". s:plug_source echo "Downloading ". s:plug_source
@ -1089,8 +1084,7 @@ EOF
echo "Downloaded ". s:plug_source echo "Downloaded ". s:plug_source
return 1 return 1
else else
echoerr "curl executable or ruby support not found" return s:err('curl executable or ruby support not found')
return 0
endif endif
endfunction endfunction

View File

@ -39,12 +39,10 @@ Execute (Initialize test environment):
let $MYVIMRC = vimrc let $MYVIMRC = vimrc
Execute (plug#end() before plug#begin() should fail): Execute (plug#end() before plug#begin() should fail):
try redir => out
call plug#end() AssertEqual 0, plug#end()
Assert 0, 'should not reach here' redir END
catch Assert stridx(out, 'Call plug#begin() first') >= 0
Assert stridx(v:exception, 'Call plug#begin() first') >= 0
endtry
Execute (plug#begin() without path argument): Execute (plug#begin() without path argument):
call plug#begin() call plug#begin()
@ -54,12 +52,10 @@ Execute (plug#begin() without path argument):
Execute (plug#begin() without path argument with empty &rtp): Execute (plug#begin() without path argument with empty &rtp):
let save_rtp = &rtp let save_rtp = &rtp
set rtp= set rtp=
try redir => out
call plug#begin() AssertEqual 0, plug#begin()
Assert 0, 'should not reach here' redir END
catch Assert stridx(out, 'Unable to determine plug home') >= 0
Assert stridx(v:exception, 'Unable to determine plug home') >= 0, 'Got: '.v:exception
endtry
let &rtp = save_rtp let &rtp = save_rtp
Execute (plug#begin(path)): Execute (plug#begin(path)):
@ -671,7 +667,7 @@ Execute (Cleanup):
unlet g:plugs unlet g:plugs
unlet g:plug_home unlet g:plug_home
unlet g:vimrc_reloaded 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 PlugStatusSorted
delf AssertExpect delf AssertExpect
delf PlugUpdated delf PlugUpdated