Skip to content

Commit

Permalink
Feat(anrok): create integration error details service (#2316)
Browse files Browse the repository at this point in the history
## Context

As part of implementation logic to store errors received from Anrok
through Nango we need a new model that would be used to store the errors
we got from integrations we have. This PR creates service to create this
error

## Description

Added `IntegrationErrorDetails::CreateService`

---------
  • Loading branch information
annvelents authored Jul 24, 2024
1 parent b1204aa commit 83210b3
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
23 changes: 23 additions & 0 deletions app/services/error_details/base_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module ErrorDetails
class BaseService < BaseService
def initialize(params:, owner:, organization:)
@params = params
@owner = owner
@organization = organization

super
end

def call
result.not_found_failure!(resource: 'owner') unless owner
result.not_found_failure!(resource: 'organization') unless organization
result
end

private

attr_reader :params, :owner, :organization
end
end
30 changes: 30 additions & 0 deletions app/services/error_details/create_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

module ErrorDetails
class CreateService < BaseService
def call
result = super
return result unless result.success?

create_error_details!
end

private

def create_error_details!
new_error = ErrorDetail.create!(
owner:,
organization:,
error_code: params[:error_code],
details: params[:details]
)

result.error_details = new_error
result
rescue ArgumentError => e
result.validation_failure!(errors: e.message)
rescue ActiveRecord::RecordInvalid => e
result.record_validation_failure!(record: e.record)
end
end
end
82 changes: 82 additions & 0 deletions spec/services/error_details/create_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe ErrorDetails::CreateService, type: :service do
let(:membership) { create(:membership) }
let(:organization) { membership.organization }
let(:customer) { create(:customer, organization:) }
let(:owner) { create(:invoice, organization:, customer:) }

describe '#call' do
subject(:service_call) { described_class.call(params:, owner:, organization:) }

let(:params) do
{
error_code: 'not_provided',
details: {'error_code' => 'taxDateTooFarInFuture'}
}
end

context 'when created succesfully' do
context 'when all - owner and organization are provided' do
it 'creates an error_detail' do
expect { service_call }.to change(ErrorDetail, :count).by(1)
end

it 'returns created error_detail' do
result = service_call

aggregate_failures do
expect(result).to be_success
expect(result.error_details.owner_id).to eq(owner.id)
expect(result.error_details.owner_type).to eq(owner.class.to_s)
expect(result.error_details.organization_id).to eq(organization.id)
expect(result.error_details.details).to eq(params[:details])
end
end
end
end

context 'when not created succesfully' do
context 'when no owner is provided' do
subject(:service_call) { described_class.call(params:, organization:, owner: nil) }

it 'does not create an error_detail' do
expect { service_call }.to change(ErrorDetail, :count).by(0)
end

it 'returns error for error_detail' do
result = service_call
aggregate_failures do
expect(result.error).to be_a(BaseService::NotFoundFailure)
expect(result.error.message).to include('owner_not_found')
end
end
end

context 'when error code is not registered in enum' do
subject(:service_call) { described_class.call(params:, owner:, organization:) }

let(:params) do
{
error_code: 'this_error_code_will_never_achieve_its_goal',
details: {'error_received' => 'taxDateTooFarInFuture'}
}
end

it 'does not create an error_detail' do
expect { service_call }.to change(ErrorDetail, :count).by(0)
end

it 'returns error for error_detail' do
result = service_call
aggregate_failures do
expect(result.error).to be_a(BaseService::ValidationFailure)
expect(result.error.message).to include("'this_error_code_will_never_achieve_its_goal' is not a valid error_code")
end
end
end
end
end
end

0 comments on commit 83210b3

Please sign in to comment.