Make error description available as "%val{error}" during catch blocks

Fixes #2761
This commit is contained in:
Maxime Coste 2019-03-05 20:46:23 +11:00
parent f9d421130f
commit 4843149b6a
6 changed files with 20 additions and 5 deletions

View File

@ -278,7 +278,9 @@ but not really useful in that context.
done on error, the catch part can be omitted. If an error is raised done on error, the catch part can be omitted. If an error is raised
in the *on_error_commands*, that error is propagated, except if in the *on_error_commands*, that error is propagated, except if
another *catch* and *on_error_commands* parameter follows, in which another *catch* and *on_error_commands* parameter follows, in which
case those commands get executed, and so-on. case those commands get executed, and so-on. During error commands,
the description of the last raiser error is available as `$kak_error`
in the shell, or `%val{error}` in commands.
*nop*:: *nop*::
does nothing, but arguments will be evaluated (e.g. shell expansion) does nothing, but arguments will be evaluated (e.g. shell expansion)

View File

@ -2080,17 +2080,26 @@ const CommandDesc try_catch_cmd = {
} }
CommandManager& command_manager = CommandManager::instance(); CommandManager& command_manager = CommandManager::instance();
Optional<ShellContext> shell_context_with_error;
for (size_t i = 0; i < parser.positional_count(); i += 2) for (size_t i = 0; i < parser.positional_count(); i += 2)
{ {
if (i == 0 or i < parser.positional_count() - 1) if (i == 0 or i < parser.positional_count() - 1)
{ {
try { try
command_manager.execute(parser[i], context, shell_context); {
command_manager.execute(parser[i], context,
shell_context_with_error.value_or(shell_context));
return; return;
} catch (runtime_error&) {} }
catch (const runtime_error& error)
{
shell_context_with_error.emplace(shell_context);
shell_context_with_error->env_vars[StringView{"error"}] = error.what().str();
}
} }
else else
command_manager.execute(parser[i], context, shell_context); command_manager.execute(parser[i], context,
shell_context_with_error.value_or(shell_context));
} }
} }
}; };

View File

@ -0,0 +1 @@
:error<ret>

View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@
no such command: 'non-existing-command'

View File

@ -0,0 +1 @@
define-command error %{ try %{ non-existing-command } catch %{ exec i "%val{error}" <esc> } }