Jump to content
iandol

How to surface silent script errors to the user?

Recommended Posts

I have a problem with ruby net:http library conditionally failing depending on whether the macOS system proxy is enabled or not. Currently my script just silently fails and Alfred shows the default searches. To know there was an error, one needs to go into the workflow editor debug mode. Does anyone have any tips how to handle script errors so they trigger a notification or some other method. My idea is to set an environment variable at the end of the script, and if that isn't set then Alfred's args and vars can do something conditional -- does that make sense?

 

Share this post


Link to post
39 minutes ago, iandol said:

I have a problem with ruby net:http library conditionally failing depending on whether the macOS system proxy is enabled or not. Currently my script just silently fails and Alfred shows the default searches. To know there was an error, one needs to go into the workflow editor debug mode. Does anyone have any tips how to handle script errors so they trigger a notification or some other method. My idea is to set an environment variable at the end of the script, and if that isn't set then Alfred's args and vars can do something conditional -- does that make sense?

 


 

yes it’s makes sense to hold the error in a Variable then Display it with a notification 

Share this post


Link to post

Actually, when Ruby errors, then the workflow halts and the other Alfred options blocks do not run. I have the line that errors wrapped in an exception block:

 

begin
	response = Net::HTTP.get(uri)
rescue Net::ReadTimeout => e
	response = ''
end

But I see this in the debug:

 

[07:39:35.463] ERROR: Bookends Tools[Script Filter] Code 1: /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/uri/rfc3986_parser.rb:67:in `split': bad URI(is not URI?): "127.0.0.1:16005" (URI::InvalidURIError)
	from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/uri/rfc3986_parser.rb:73:in `parse'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/uri/common.rb:234:in `parse'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/uri/generic.rb:1542:in `find_proxy'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/net/http.rb:1109:in `proxy_uri'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/net/http.rb:1096:in `proxy?'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/net/http.rb:936:in `connect'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/net/http.rb:930:in `do_start'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/net/http.rb:919:in `start'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/net/http.rb:605:in `start'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/net/http.rb:481:in `get_response'
...

And then nothing gets printed from my debug block:

401974173_Screenshot2020-05-21at07_40.11_SMALL.png.65317f4ec811baf2abbf5092a5f2b212.png

 

 

ALSO, I realise that I cannot change the environment variables from within Ruby, as it is a child process so it only works on copies of the ENV sent to it from Alfred.  I suppose I must modify the stdout to something that can be parsed to determine an error...

 

Edited by iandol

Share this post


Link to post

Make a script filter JSON output with a single option that tells the user the option failed, and output that on the rescue. That way, instead of the Script Filter failing and the user getting the default searches, they’ll get the message in Alfred itself.


In your latest example, you’re rescuing the wrong thing. You’re catching Net::ReadTimeout but the error was URI::InvalidURIError.

Share this post


Link to post

Hi Vitor, yes, I just realised I had a misspecification problem in my rescue block.

 

Spitting out JSON for the error message also makes a lot more sense. Thank you!!!

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...