зеркало из
https://github.com/glebtv/telegram-bot.git
synced 2026-09-06 11:05:51 +03:00
rescue_from feature
Этот коммит содержится в:
@@ -1,5 +1,7 @@
|
|||||||
# Unreleased
|
# Unreleased
|
||||||
|
|
||||||
|
- `rescue_from`.
|
||||||
|
|
||||||
# 0.12.4
|
# 0.12.4
|
||||||
|
|
||||||
- Fix spec helpers for callback queries.
|
- Fix spec helpers for callback queries.
|
||||||
|
|||||||
@@ -130,6 +130,9 @@ can represent actions as separate methods keeping source much more readable and
|
|||||||
New instance of controller is instantiated for each update.
|
New instance of controller is instantiated for each update.
|
||||||
This way every update is processed in isolation from others.
|
This way every update is processed in isolation from others.
|
||||||
|
|
||||||
|
Bot controllers like usual rails controllers provides features like callbacks,
|
||||||
|
`rescue_from` and instrumentation.
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
class Telegram::WebhookController < Telegram::Bot::UpdatesController
|
class Telegram::WebhookController < Telegram::Bot::UpdatesController
|
||||||
# use callbacks like in any other controllers
|
# use callbacks like in any other controllers
|
||||||
|
|||||||
@@ -57,6 +57,7 @@ module Telegram
|
|||||||
instrumentation
|
instrumentation
|
||||||
log_subscriber
|
log_subscriber
|
||||||
reply_helpers
|
reply_helpers
|
||||||
|
rescue
|
||||||
session
|
session
|
||||||
].each { |file| require "telegram/bot/updates_controller/#{file}" }
|
].each { |file| require "telegram/bot/updates_controller/#{file}" }
|
||||||
|
|
||||||
@@ -78,6 +79,7 @@ module Telegram
|
|||||||
end
|
end
|
||||||
|
|
||||||
include AbstractController::Translation
|
include AbstractController::Translation
|
||||||
|
include Rescue
|
||||||
include ReplyHelpers
|
include ReplyHelpers
|
||||||
prepend Instrumentation
|
prepend Instrumentation
|
||||||
extend Session::ConfigMethods
|
extend Session::ConfigMethods
|
||||||
|
|||||||
20
lib/telegram/bot/updates_controller/rescue.rb
Обычный файл
20
lib/telegram/bot/updates_controller/rescue.rb
Обычный файл
@@ -0,0 +1,20 @@
|
|||||||
|
require 'active_support/rescuable'
|
||||||
|
|
||||||
|
module Telegram
|
||||||
|
module Bot
|
||||||
|
class UpdatesController
|
||||||
|
module Rescue
|
||||||
|
extend ActiveSupport::Concern
|
||||||
|
include ActiveSupport::Rescuable
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def process_action(*)
|
||||||
|
super
|
||||||
|
rescue Exception => exception # rubocop:disable RescueException
|
||||||
|
rescue_with_handler(exception) || raise
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
31
spec/telegram/bot/updates_controller/rescue_spec.rb
Обычный файл
31
spec/telegram/bot/updates_controller/rescue_spec.rb
Обычный файл
@@ -0,0 +1,31 @@
|
|||||||
|
RSpec.describe Telegram::Bot::UpdatesController::Rescue do
|
||||||
|
include_context 'telegram/bot/updates_controller'
|
||||||
|
|
||||||
|
describe '#process_action' do
|
||||||
|
subject { -> { dispatch_message("/#{command} x y z") } }
|
||||||
|
|
||||||
|
let(:controller_class) do
|
||||||
|
Class.new(Telegram::Bot::UpdatesController) do
|
||||||
|
rescue_from ArgumentError, with: -> { respond_with :message, text: 'Rescued' }
|
||||||
|
|
||||||
|
def rescuable(*)
|
||||||
|
raise ArgumentError, 'rescuable'
|
||||||
|
end
|
||||||
|
|
||||||
|
def not_rescuable(*)
|
||||||
|
raise 'not_rescuable'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when exception is rescued' do
|
||||||
|
let(:command) { :rescuable }
|
||||||
|
it { should respond_with_message 'Rescued' }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when exception is not rescued' do
|
||||||
|
let(:command) { :not_rescuable }
|
||||||
|
it { should raise_error('not_rescuable') }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Ссылка в новой задаче
Block a user