]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 8.2.2141: a user command with try/catch may not catch an expression error v8.2.2141
authorBram Moolenaar <Bram@vim.org>
Sun, 13 Dec 2020 19:26:29 +0000 (20:26 +0100)
committerBram Moolenaar <Bram@vim.org>
Sun, 13 Dec 2020 19:26:29 +0000 (20:26 +0100)
Problem:    A user command with try/catch may not catch an expression error.
Solution:   When an expression fails check for following "|". (closes #7469)

src/eval.c
src/testdir/test_trycatch.vim
src/testdir/test_vimscript.vim
src/version.c

index 47aaaad237b506c101df7e9d3b00002fdba98c0f..6ce2ebf68df3c2a23dd36bf66ac150881a6ca24b 100644 (file)
@@ -2158,7 +2158,10 @@ eval0(
            semsg(_(e_invexpr2), arg);
 
        // Some of the expression may not have been consumed.  Do not check for
-       // a next command to avoid more errors.
+       // a next command to avoid more errors, unless "|" is following, which
+       // could only be a command separator.
+       if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
+           eap->nextcmd = check_nextcmd(p);
        return FAIL;
     }
 
index 9f670f7aedba39566f371692a9ee169f64af373f..b5ad14c2a57dfe5ef8747a5ceb355bf518ca33b3 100644 (file)
@@ -2199,5 +2199,30 @@ func Test_BufEnter_exception()
   %bwipe!
 endfunc
 
+" Test for using try/catch in a user command with a failing expression    {{{1
+func Test_user_command_try_catch()
+  let lines =<< trim END
+      function s:throw() abort
+        throw 'error'
+      endfunction
+
+      command! Execute
+      \   try
+      \ |   let s:x = s:throw()
+      \ | catch
+      \ |   let g:caught = 'caught'
+      \ | endtry
+
+      let g:caught = 'no'
+      Execute
+      call assert_equal('caught', g:caught)
+  END
+  call writefile(lines, 'XtestTryCatch')
+  source XtestTryCatch
+
+  call delete('XtestTryCatch')
+  unlet g:caught
+endfunc
+
 " Modeline                                                                 {{{1
 " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
index 8c931992441ebf6b739df2f1227fcc72ab012332..e50e4d5fa312a91249cf33077688d0ce2b7657f8 100644 (file)
@@ -6825,7 +6825,7 @@ func Test_script_lines()
                    \ ])
        call assert_report("Shouldn't be able to define function")
     catch
-       call assert_exception('Vim(function):E126: Missing :endfunction')
+       call assert_exception('Vim(function):E1145: Missing heredoc end marker: .')
     endtry
 
     " :change
@@ -6845,7 +6845,7 @@ func Test_script_lines()
                    \ ])
        call assert_report("Shouldn't be able to define function")
     catch
-       call assert_exception('Vim(function):E126: Missing :endfunction')
+       call assert_exception('Vim(function):E1145: Missing heredoc end marker: .')
     endtry
 
     " :insert
@@ -6865,7 +6865,7 @@ func Test_script_lines()
                    \ ])
        call assert_report("Shouldn't be able to define function")
     catch
-       call assert_exception('Vim(function):E126: Missing :endfunction')
+       call assert_exception('Vim(function):E1145: Missing heredoc end marker: .')
     endtry
 endfunc
 
index cf9fbc3d5200e28984a30c567c0e87f872a7a8c0..3aec81b4b00564707376526d4f870bc9c85f31ee 100644 (file)
@@ -750,6 +750,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    2141,
 /**/
     2140,
 /**/