Make error description available as "%val{error}" during catch blocks
Fixes #2761
This commit is contained in:
parent
f9d421130f
commit
4843149b6a
|
@ -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)
|
||||||
|
|
|
@ -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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
1
test/compose/catch-error-desc/cmd
Normal file
1
test/compose/catch-error-desc/cmd
Normal file
|
@ -0,0 +1 @@
|
||||||
|
:error<ret>
|
1
test/compose/catch-error-desc/in
Normal file
1
test/compose/catch-error-desc/in
Normal file
|
@ -0,0 +1 @@
|
||||||
|
|
1
test/compose/catch-error-desc/out
Normal file
1
test/compose/catch-error-desc/out
Normal file
|
@ -0,0 +1 @@
|
||||||
|
no such command: 'non-existing-command'
|
1
test/compose/catch-error-desc/rc
Normal file
1
test/compose/catch-error-desc/rc
Normal file
|
@ -0,0 +1 @@
|
||||||
|
define-command error %{ try %{ non-existing-command } catch %{ exec i "%val{error}" <esc> } }
|
Loading…
Reference in New Issue
Block a user