discordrb icon indicating copy to clipboard operation
discordrb copied to clipboard

'Inline' `Bot#gateway_check` assertion

Open biqqles opened this issue 3 years ago • 1 comments

Summary

Bot#gateway_check is a common method used to assert that a gateway connection is active, called as the first step in many bot methods. By using the three argument form of raise, along with caller, we can hide the gateway_check call from the traceback, making the source of the error clearer.

With this change, after calling bot.servers:

/home/biqqles/Code/discordrb/lib/discordrb/bot.rb:180:in `servers': A gateway connection is necessary to call this method! You'll have to do it inside any event (e.g. `ready`) or after `bot.run :async`. (RuntimeError)
	from /home/biqqles/Code/discordrb/lib/test.rb:5:in `<main>

Before:

/home/biqqles/Code/discordrb/lib/discordrb/bot.rb:902:in `gateway_check': A gateway connection is necessary to call this method! You'll have to do it inside any event (e.g. `ready`) or after `bot.run :async`. (RuntimeError)
	from /home/biqqles/Code/discordrb/lib/discordrb/bot.rb:180:in `servers'
	from /home/biqqles/Code/discordrb/lib/test.rb:5:in `<main>'

Changed

  • Bot#gateway_check is now hidden from the traceback it causes

biqqles avatar Jul 21 '22 21:07 biqqles

@Daniel-Worrall I'd like your input here. I think it's a fine change to make but one could argue for more explicit backtraces

swarley avatar Oct 09 '22 01:10 swarley