Log project services errors when executing async

This commit is contained in:
Felipe Artur 2018-08-21 16:29:50 -03:00
parent fab1556396
commit 9184753146
3 changed files with 35 additions and 1 deletions

View file

@ -7,6 +7,10 @@ class ProjectServiceWorker
def perform(hook_id, data)
data = data.with_indifferent_access
Service.find(hook_id).execute(data)
service = Service.find(hook_id)
service.execute(data)
rescue => error
service_class = service&.class&.name || "Not Found"
logger.error class: self.class.name, service_class: service_class, message: error.message
end
end

View file

@ -0,0 +1,5 @@
---
title: Log project services errors when executing async
merge_request:
author:
type: other

View file

@ -0,0 +1,25 @@
# frozen_string_literal: true
require 'spec_helper'
describe ProjectServiceWorker, '#perform' do
let(:worker) { described_class.new }
let(:service) { JiraService.new }
before do
allow(Service).to receive(:find).and_return(service)
end
it 'executes service with given data' do
data = { test: 'test' }
expect(service).to receive(:execute).with(data)
worker.perform(1, data)
end
it 'logs error messages' do
allow(service).to receive(:execute).and_raise(StandardError, 'invalid URL')
expect(Sidekiq.logger).to receive(:error).with({ class: described_class.name, service_class: service.class.name, message: "invalid URL" })
worker.perform(1, {})
end
end