In LaTeX you can use the macro \stop as a "panic button" for aborting compilation without having raised an error-message.
I suggest using the package l3msg of expl3 for raising (error-)messages and probably notifying about aborting before aborting compilation via \stop.
"probably" because I don't know what criteria you use to classify a compile/run as "failed" - whether it's enough that there was an error message, or whether there must have been a situation that lead to abortion of compilation.
If your LaTeX distribution is up to date (L3 programming layer <2021-05-18> or more recent), you can do something like this:
\ExplSyntaxOn
\prop_gput:Nnn \g_msg_module_type_prop { MYPREAMBLECODE } {}
\prop_gput:Nnn \g_msg_module_name_prop { MYPREAMBLECODE } {Preamble Code}
\msg_new:nnnn {MYPREAMBLECODE}
{Invalid Parameter}
{Invalid~parameter~'#3'~as~#2~argument~of~macro~#1~\msg_line_context: .}
{Don't~pass~'#3'~as~#2~argument~of~macro~#1! }
\msg_new:nnnn {MYPREAMBLECODE}
{Abort compilation}
{Compilation~is~aborted~now.}
{The~situation~really~is~weird!~Please~fix~the~reported~errors!}
\cs_new:Npn \InvalidParameterErrorStop #1 #2 #3 {
% \msg_error:... etc turn their arguments into strings, but let's make sure
% the control sequence token whose name is to be displayed is stringified with a
% leading backslash regardless the current value of the \escapechar parameter:
\exp_args:Nne \use:n {
\msg_error:nnnnn {MYPREAMBLECODE} {Invalid Parameter}
%\msg_expandable_error:nnnnn {MYPREAMBLECODE} {Invalid Parameter}
}{\iow_char:N \\\cs_to_str:N #1}{#2}{#3}
% --- the following lines lead to aborting the LaTeX-run
% after notifying about imminent abortion:
\msg_note:nn {MYPREAMBLECODE} {Abort compilation}
%\msg_expandable_note:nn {MYPREAMBLECODE} {Abort compilation}
\stop
% --------------------------------------------------------
}
\ExplSyntaxOff
\RequirePackage{etoolbox}
\newcommand*{\mycommand}[1]{%
\ifcsdef{somecommandcontaining#1}{%
\ifcsdef{someothercommandcontaining#1}{%
valid condition 1, do something
}{ %%%%% This combination of conditions is invalid %%%%%
\InvalidParameterErrorStop{\mycommand}{first}{#1}%
}%
}{%
valid condition 2, do something else
}%
}%
\newcommand\somecommandcontainingFooBar{This is defined.}
\documentclass{article}
\begin{document}
\mycommand{FooBar}
\end{document}
Output on terminal and in .log-file:
! Preamble Code Error: Invalid parameter 'FooBar' as first argument of macro
(Preamble Code) \mycommand on line 49.
For immediate help type H <return>.
...
l.49 \mycommand{FooBar}
? H
Don't pass 'FooBar' as first argument of macro \mycommand!
?
Preamble Code Info: Compilation is aborted now.
\PackageError{mypackage}{Invalid parameter '#1'}{dont do that}? – David Carlisle Jul 24 '22 at 17:29\GenericErrorcan substitute if the relevant macro isn't part of a package. – scorchgeek Jul 24 '22 at 17:39\PackageError{local-hack-in-preamble-i-might-put-in-a-package-one-day}works too – David Carlisle Jul 24 '22 at 17:44\hyperlinkand\hypertargetand a non-pdfTeX-engine you can easily get an output-file with broken hyperlinks without being notified via message.) – Ulrich Diez Jul 24 '22 at 22:24