Pārlūkot izejas kodu

Change error reporting method

As suggested by @vheon:
https://github.com/junegunn/vim-plug/pull/40#issuecomment-50278543
Junegunn Choi 11 gadi atpakaļ
vecāks
revīzija
d690f8d576
2 mainītis faili ar 33 papildinājumiem un 43 dzēšanām
  1. 24 30
      plug.vim
  2. 9 13
      test/workflow.vader

+ 24 - 30
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
 

+ 9 - 13
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