Try/Catch: Would Love to Get More Details in Tokens

(Let me know if I'm categorizing this wrong or anything. Despite using KM since version 3 this is my first time joining the forum.)

I am very happy to see the new try/catch action as I have some very complex macros that will sometimes fail for random reasons. Some of these take 40 min or more to run, so not having to re-run them and watch like a hawk when troubleshooting is great.

My problem/request is for a way to quickly locate a failing action. Actions are often nested deeply within macros, and actions, unless renamed individually, are generally indistinguishable from every other instance of the same action type. To help myself out, most groups within my macros are custom labeled so I can find a failure point quickly using the debug window, if I am watching it.

What would be really helpful is to capture the true "path" of the failing action as a token, in addition to its name and output result.

In trying to do this on my own, I have tried to screen capture and OCR the debugger window, store it to a clipboard and email it to myself, but it fails to capture anything intelligible. I suspect it vanishes too quickly to capture properly.

I am currently using the following tokens. If anyone can think of other relevant tokens please let me know.

%ExecutingMacroGroup%
Works as expected, since the macros are grouped logically.

%ExecutingMacro%
The way this works makes logical sense, but is less helpful to me. I am running many macros in sequence with "execute macro" actions, and currently the executing "sub" macro is not named, rather the "orchestrating" macro is. Effectively this duplicates %ExecutingMacroGroup% in my case.

%ExecutingThisMacro%
This does go one layer deeper into the onion, which is a helpful start.

%ActionResult%
This correctly names the action that failed, and its result, but lacks enough context to directly locate it.

%Application%1%
%WindowName%1%
The combination of these gives me enough clues to figure out which part of the macro to start looking in.

Here's an example of a failure notification email I set up with tokens:

Subject:
Error %ExecutingMacroGroup%
Body:
%ExecutingThisMacro% macro failed with message: %ActionResult%
Current Application %Application%1%, window %WindowName%1%

TL:DR
In a nutshell, if the output of the debugger at the moment of failure could be captured as a token I could email it to myself and quickly locate the path of a nested failing action.

Here's an example of the nested debugger output for those who haven't seen it recently.

“Orchestrating Macro Name”
Try Executing Actions and Execute Catch Actions on Error
Execute Macro “Name of sub-macro”
“Group name”
if All Conditions Met Execute Actions
“Sub-group name”
Pause Until Conditions are Met

Since this is my first post, I will say I really appreciate KM and I have alway found it to be a fantastic tool. Try/Catch is already significant improvement for me, and I hope the feedback is helpful.

I have a question for you and a couple of ideas. I hope you don't mind ideas.

When you say "at the moment of failure" do you mean when an action fails? If so, why not turn off the "Failure Aborts Macro" flag for every statement that you want to get a message from and follow the action with something like this: (use a different random number for each time you do this)

image

Yes I know that's a lot of work, but I'm mentioning this to see if I understand the problem correctly. But doing it this way helps ensure you don't get a lot of false alarms.

Another approach to solving your concern is this... create a macro that monitors every new line of the KM log file, and if the new line matches something that you consider to be a "notifiable condition," then email or text that to yourself. If you aren't careful how you define "notifiable condition," you might end up with too many messages. But in principle this could work well, and I have never thought about it until now. It's not a bad idea. If you don't know how to do this, just ask, and someone can help you.

Hey John,

In Keyboard Maestro 10+:

The Keyboard Maestro Editor now has a Go To Last Failed Action command, although you can't see or use it unless there's been a failed action.

Failed actions are logged in the Keyboard Maestro Engine log, and these now have their UUID attached.

I too think it would be useful to have a %LastFailedAction% token or something similar.

@peternlewis?

-Chris

Noted.

1 Like

Hi thanks for your reply. The issue is not so much that certain actions fail regularly, but random actions fail randomly. To be honest, most of the time it's rock solid. If we were talking about a handful of actions I could certainly see creating a solution like you outlined, but it's hundreds.

I will pay more attention to the log though and see if I can exploit that. But really the debugger output is perfect, it's just ephemeral. If I could capture it I would be set.

Okay. I can empathize with that. I deal with it by using the debugger. There's also a KM action that can turn on the debugger while running a macro. I use that too, just before any suspicious code in a macro that I want to trace.

Hey John,

You might find this useful:

Display Last KM Macro Error

And/or this:

Open the Keyboard Maestro Engine Log in the Console or BBEdit

-Chris

Here is my imperfect solution for now: purge the engine log before running the macro, cat | grep the engine log for errors.

Looks pretty good

That's an inspiring solution to an interesting problem. It makes me think about getting notifications sent to my Apple Watch. And that is making me wonder if Peter could create a small app for the Apple Watch which can be used to display some sort of data. Perhaps it could involve a new action in KM, similar to "Send iMessage" which sends the data to the KM Watch App.

Also, maybe the KM Watch app could have some buttons that could trigger a KM macro on my Mac. I've seen an Apple Watch app that uses remote logins to execute an arbitrary shell command, which could be the osascript command that could trigger a KM macro, but that's pretty tricky for the average user and maybe it could be simplified with an official KM app for the Apple Watch (or even for Android.)

Hey John,

I wouldn't purge the log. You might need it for something else.

Nor would I cat the whole log.

I'd look for the running macro, or I'd actually log a marker using KM's log action.

Awk makes it easy to find the last instance of found text in a file and print from there to the end of the file:

awk '/My Logged Text Flag/{flag = 1}; flag' ~/'Library/Logs/Keyboard Maestro/Engine.log'

-Chris

I have added a %LastAbortedActionID% token for the next version.

2 Likes