Jump to content

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?

 

Link to comment
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 

Link to comment

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
Link to comment

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.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...