Skip to content

Commit

Permalink
Merge pull request #14 from payrollhero/url-in-error-exception
Browse files Browse the repository at this point in the history
response body error message in exception
  • Loading branch information
mathieujobin authored Jul 24, 2019
2 parents 52ebb62 + fb1e30c commit 88e731c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ gemfile:
- gemfiles/rails_5.gemfile
- gemfiles/rails_5_2.gemfile


matrix:
exclude:
- rvm: 2.5.3
gemfile: gemfiles/rails_4.gemfile
- rvm: 2.1.8
gemfile: gemfiles/rails_5.gemfile
- rvm: 2.1.8
Expand Down
11 changes: 7 additions & 4 deletions lib/webhook_system/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ class Job < ActiveJob::Base

# Exception class around non 200 responses
class RequestFailed < RuntimeError
def initialize(message, code)
def initialize(message, code, error_message=nil)
super(message)
@code = code
@error_message = error_message
end
end

Expand Down Expand Up @@ -46,12 +47,14 @@ def self.post(subscription, event)
end

log_response(subscription, event, request, response)
ensure_success(response.status, :POST, subscription.url)
ensure_success(response, :POST, subscription.url)
end

def self.ensure_success(status, http_method, url)
def self.ensure_success(response, http_method, url)
status = response.status
unless (200..299).cover? status
raise RequestFailed.new("#{http_method} request to #{url} failed with code: #{status}", status)
text = "#{http_method} request to #{url} failed with code: #{status} and error #{response.body}"
raise RequestFailed.new(text, status, response.body)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/webhook_system/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module WebhookSystem
VERSION = '2.1.4'
VERSION = '2.1.5'
end
12 changes: 7 additions & 5 deletions spec/dispatching_events_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
require 'spec_helper'

describe 'dispatching events', aggregate_failures: true, db: true do
let(:hook_url) { "http://lvh.me/hook1" }
describe 'dispatching' do
let!(:subscription1) do
create(:webhook_subscription, :active, :encrypted, :with_topics, url: 'http://lvh.me/hook1', topics: ['other_event'])
create(:webhook_subscription, :active, :encrypted, :with_topics, url: hook_url, topics: ['other_event'])
end

let!(:subscription2) do
Expand Down Expand Up @@ -34,7 +35,7 @@ def payload_attributes
let(:event) { event_class.build(name: 'John', age: 21) }
let(:subscription1_hook_stub) {
headers = { 'Content-Type' => 'application/json; base64+aes256' }
stub_request(:post, 'http://lvh.me/hook1').with(body: /.*/, headers: headers)
stub_request(:post, hook_url).with(body: /.*/, headers: headers)
}

describe 'successful delivery' do
Expand Down Expand Up @@ -65,7 +66,7 @@ def payload_attributes
body: "I don't like you",
headers: { 'Hello' => 'World' })

error_message = "POST request to http://lvh.me/hook1 failed with code: 400"
error_message = "POST request to #{hook_url} failed with code: 400 and error I don't like you"
expect {
expect {
perform_enqueued_jobs do
Expand All @@ -85,10 +86,11 @@ def payload_attributes
end

describe 'exception occurs during the delivery' do
let(:upstream_error) { %r(RuntimeError\nexception message\n) }
it 'fires the jobs' do
subscription1_hook_stub.to_raise(RuntimeError.new('exception message'))

error_message = "POST request to http://lvh.me/hook1 failed with code: 0"
error_message = /POST request to #{hook_url} failed with code: 0 and error .*RuntimeError.*/
expect {
expect {
perform_enqueued_jobs do
Expand All @@ -100,7 +102,7 @@ def payload_attributes
log = subscription1.event_logs.last

expect(log.status).to eq(0)
expect(log.response['body']).to match(%r(RuntimeError\nexception message\n))
expect(log.response['body']).to match(upstream_error)
expect(log.response['headers']).to eq({})
end
end
Expand Down
2 changes: 1 addition & 1 deletion webhook_system.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Gem::Specification.new do |gem|
gem.add_runtime_dependency 'ph_model'
gem.add_runtime_dependency 'validate_url', '~> 1.0'

gem.add_development_dependency 'bundler', '~> 1.0'
gem.add_development_dependency 'bundler', '> 1.0', '< 2.5'
gem.add_development_dependency 'rake'
gem.add_development_dependency 'rspec', '~> 3.0'
gem.add_development_dependency 'rubygems-tasks', '~> 0.2'
Expand Down

0 comments on commit 88e731c

Please sign in to comment.