You are already mimicking the behavior of MMA built-in functions. The following code prints a $Assumptions::cas message not a TensorDimensions::cas.
$Assumptions = A \[Element] Arrays[{2, 2, 2, 2}];
Assuming[A \[Element] Arrays[{2, 2}], Print["QD was here…"];TensorDimensions[A]]
(*error message before QD gets there*)
Note that the error is printed before the message. That suggests that Assuming processes the assumptions before moving on to the evaluation of the code in the second parameter. Checking the attributes further supports that suggestion:
Attributes[Assuming]
{HoldRest, Protected}
If one must have one's own message, one could try evaluating $Assumptions within a Quiet block.
Options[qdSimplify] = {Assumptions :> $Assumptions};
qdSimplify[e_, o : OptionsPattern[]] := qdSimplify[e, True, o];
qdSimplify[e_, a_, OptionsPattern[]] :=
With[{qdAssumptions =
Quiet[Check[
Assuming[a && OptionValue[Assumptions], $Assumptions],
Print["Your ad here"]; True], {$Assumptions::cas}]},
Print["Bleached assumptions: ", qdAssumptions];
Assuming[qdAssumptions, Print[qdAssumptions];
Print[$Assumptions];]];
$Assumptions = True;
qdSimplify[Sin[x], x > 0 && x < 0];
Of course, one would have to determine what to do in case of contradiction (perhaps devising some preference semantics) … but that is the subject for another question.
Update
I am not sure that I understand your request.
If it about suppressing the message $Assumptions::fas, you could just add it to the list that contains $Assumptions::cas.
If it is about printing your own message, you can replace Print["Your ad here"] with your own Message[blah].
If you want to reproduce the fact that Simplify with contradictory assumptions evaluates to True, you can do it by changing the True after Print with a flag that you check in the body.
Maybe you want all of the above.
Options[qdSimplify] = {Assumptions :> $Assumptions};
(*As pointed out by b3m2a1, the next two lines are not necessary*)
(*
qdSimplify::fas="Warning: one or more assumptions evaluated to False";
qdSimplify::cas="Warning: contradictory assumption(s) encountered: `1`."*)
qdSimplifyContr::usage = "This is a flag indicating contradictory assumptions.";
qdSimplifyFalse::usage = "This is a flag indicating false assumptions.";
qdSimplify[e_, o : OptionsPattern[]] := qdSimplify[e, True, o];
qdSimplify[e_, a_, OptionsPattern[]] := With[{qdAssumptions = Quiet[
Check[
Check[
Assuming[a && OptionValue[Assumptions], $Assumptions],
Message[qdSimplify::cas, a && OptionValue[Assumptions]]; qdSimplifyContr,
{$Assumptions::cas}],
Message[qdSimplify::fas]; qdSimplifyFalse,
{$Assumptions::fas}],
{$Assumptions::cas, $Assumptions::fas}]},
Print["Bleached assumptions: ", qdAssumptions];
Which[
(*A*)! FreeQ[qdAssumptions, qdSimplifyContr], True,
(*B*)! FreeQ[qdAssumptions, qdSimplifyFalse], "Handled false assumptions",
(*C*)True,
Assuming[qdAssumptions, Print[qdAssumptions]; Print[$Assumptions];
"Normal code"]]];
A. Simulates the fact that Simplify with contradictory assumptions evaluates to True.
B. This is where you would handle the situation with false assumptions.
C. This is where you would put the body of the function.
WithMessageHandlerto catch messages and reemit them with the appropriate head or use a different handler built at theInternal`HandlerBlocklevel. – b3m2a1 Aug 07 '18 at 22:39WithMessageHandler... you can look here I guess, although that's a bit ugly and autobuilt. – b3m2a1 Aug 07 '18 at 23:04