Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(errors): Convert missing_argument to not_found errors #462

Merged
merged 4 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions app/serializers/error_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
class ErrorSerializer < ModelSerializer
def serialize
{
status: 422,
error: 'Unprocessable entity',
message: model.error,
input_params: model.input_params
status: model.status,
error: (model.status == 404) ? 'Not found' : 'Unprocessable entity',
message: model.error.to_s,
input_params: model.input_params,
}
end
end
4 changes: 2 additions & 2 deletions app/services/applied_add_ons/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def create_from_api(organization:, args:)
attr_reader :customer, :add_on

def check_preconditions(amount_currency:)
return result.fail!(code: 'missing_argument', message: 'unable_to_find_customer') if customer.blank?
return result.fail!(code: 'missing_argument', message: 'add_on_does_not_exist') if add_on.blank?
return result.not_found_failure!(resource: 'customer') unless customer
return result.not_found_failure!(resource: 'add_on') unless add_on
return result.fail!(code: 'no_active_subscription') unless active_subscription?
return result.fail!(code: 'currencies_does_not_match') unless applicable_currency?(amount_currency)
end
Expand Down
4 changes: 2 additions & 2 deletions app/services/applied_coupons/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def create_from_api(organization:, args:)
attr_reader :customer, :coupon

def check_preconditions(amount_currency:)
return result.fail!(code: 'missing_argument', message: 'unable_to_find_customer') if customer.blank?
return result.fail!(code: 'missing_argument', message: 'coupon_does_not_exist') if coupon.blank?
return result.not_found_failure!(resource: 'customer') unless customer
return result.not_found_failure!(resource: 'coupon') unless coupon
return result.fail!(code: 'no_active_subscription') unless active_subscription?
return result.fail!(code: 'coupon_already_applied') if coupon_already_applied?
return result.fail!(code: 'currencies_does_not_match') unless applicable_currency?(amount_currency)
Expand Down
11 changes: 7 additions & 4 deletions app/services/events/validate_creation_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,12 @@ def valid_code?
def send_webhook_notice
return unless organization.webhook_url?

status = result.error.is_a?(BaseService::NotFoundFailure) ? 404 : 422

object = {
input_params: params,
error: result.error,
error: result.error.to_s,
status: status,
organization_id: organization.id,
}

Expand All @@ -91,7 +94,7 @@ def customer_external_subscription_ids
end

def blank_subscription_error
result.fail!(code: 'missing_argument', message: 'subscription does not exist or is not given')
result.not_found_failure!(resource: 'subscription')
send_webhook_notice
end

Expand All @@ -101,12 +104,12 @@ def invalid_subscription_error
end

def invalid_code_error
result.fail!(code: 'missing_argument', message: 'code does not exist')
result.not_found_failure!(resource: 'billable_metric')
send_webhook_notice
end

def invalid_customer_error
result.fail!(code: 'missing_argument', message: 'customer cannot be found')
result.not_found_failure!(resource: 'customer')
send_webhook_notice
end

Expand Down
4 changes: 2 additions & 2 deletions app/services/subscriptions/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ def create(**args)
private

def process_create
return result.fail!(code: 'missing_argument', message: 'unable to find customer') unless current_customer
return result.fail!(code: 'missing_argument', message: 'plan does not exists') unless current_plan
return result.not_found_failure!(resource: 'customer') unless current_customer
return result.not_found_failure!(resource: 'plan') unless current_plan

if currency_missmatch?(current_customer&.active_subscription&.plan, current_plan)
return result.fail!(code: 'currencies_does_not_match', message: 'currencies does not match')
Expand Down
6 changes: 3 additions & 3 deletions spec/requests/api/v1/applied_add_ons_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@
end
end

context 'with invalid params' do
context 'with invalid name' do
let(:params) do
{ name: 'Foo Bar' }
end

it 'returns an unprocessable_entity' do
it 'returns an not_found error' do
post_with_token(organization, '/api/v1/applied_add_ons', { applied_add_on: params })

expect(response).to have_http_status(:unprocessable_entity)
expect(response).to have_http_status(:not_found)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/api/v1/applied_coupons_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
it 'returns an unprocessable_entity' do
post_with_token(organization, '/api/v1/applied_coupons', { applied_coupon: params })

expect(response).to have_http_status(:unprocessable_entity)
expect(response).to have_http_status(:not_found)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/requests/api/v1/subscriptions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@
expect(json[:subscription][:downgrade_plan_date]).to be_nil
end

context 'with invalid params' do
context 'with invalid plan code' do
let(:params) do
{ plan_code: plan.code }
end

it 'returns an unprocessable_entity error' do
it 'returns a not_found error' do
post_with_token(organization, '/api/v1/subscriptions', { subscription: params })

expect(response).to have_http_status(:unprocessable_entity)
expect(response).to have_http_status(:not_found)
end
end
end
Expand Down
17 changes: 9 additions & 8 deletions spec/serializers/error_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,23 @@
input_params: {
customer_id: 'customer',
transaction_id: transaction_id,
code: 'code'
code: 'code',
},
error: 'Code does not exist',
organization_id: 'testtest'
error: 'Event not found',
status: 404,
organization_id: 'testtest',
}
end
let(:json_response_hash) do
{
'event_error' => {
'status' => 422,
'error' => 'Unprocessable entity',
'message' => 'Code does not exist',
'status' => 404,
'error' => 'Not found',
'message' => 'Event not found',
'input_params' => {
'customer_id' => 'customer',
'transaction_id' => transaction_id,
'code' => 'code'
'code' => 'code',
}
}
}
Expand All @@ -37,4 +38,4 @@
it 'serializes object' do
expect(result).to eq json_response_hash
end
end
end
44 changes: 30 additions & 14 deletions spec/services/applied_add_ons/create_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,25 @@
let(:customer) { nil }
let(:customer_id) { 'foo' }

it { expect(create_result).not_to be_success }
it { expect(create_result.error_code).to eq('missing_argument') }
it { expect(create_result.error).to eq('unable_to_find_customer') }
it 'returns a not found error' do
aggregate_failures do
expect(create_result).not_to be_success
expect(create_result.error).to be_a(BaseService::NotFoundFailure)
expect(create_result.error.message).to eq('customer_not_found')
end
end
end

context 'when add-on is not found' do
let(:add_on_id) { 'foo' }

it { expect(create_result).not_to be_success }
it { expect(create_result.error_code).to eq('missing_argument') }
it { expect(create_result.error).to eq('add_on_does_not_exist') }
it 'returns a not found error' do
aggregate_failures do
expect(create_result).not_to be_success
expect(create_result.error).to be_a(BaseService::NotFoundFailure)
expect(create_result.error.message).to eq('add_on_not_found')
end
end
end

context 'when customer does not have a subscription' do
Expand Down Expand Up @@ -158,8 +166,8 @@
properties: {
customer_id: applied_add_on.customer.id,
addon_code: applied_add_on.add_on.code,
addon_name: applied_add_on.add_on.name
}
addon_name: applied_add_on.add_on.name,
},
)
end

Expand All @@ -182,17 +190,25 @@
let(:customer) { nil }
let(:external_customer_id) { 'foo' }

it { expect(create_result).not_to be_success }
it { expect(create_result.error_code).to eq('missing_argument') }
it { expect(create_result.error).to eq('unable_to_find_customer') }
it 'returns a not found error' do
aggregate_failures do
expect(create_result).not_to be_success
expect(create_result.error).to be_a(BaseService::NotFoundFailure)
expect(create_result.error.message).to eq('customer_not_found')
end
end
end

context 'when add-on is not found' do
let(:add_on_code) { 'foo' }

it { expect(create_result).not_to be_success }
it { expect(create_result.error_code).to eq('missing_argument') }
it { expect(create_result.error).to eq('add_on_does_not_exist') }
it 'returns a not found error' do
aggregate_failures do
expect(create_result).not_to be_success
expect(create_result.error).to be_a(BaseService::NotFoundFailure)
expect(create_result.error.message).to eq('add_on_not_found')
end
end
end

context 'when customer does not have a subscription' do
Expand Down
64 changes: 44 additions & 20 deletions spec/services/applied_coupons/create_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,37 @@
let(:customer) { nil }
let(:customer_id) { 'foo' }

it { expect(create_result).not_to be_success }
it { expect(create_result.error_code).to eq('missing_argument') }
it { expect(create_result.error).to eq('unable_to_find_customer') }
it 'returns a not found error' do
aggregate_failures do
expect(create_result).not_to be_success
expect(create_result.error).to be_a(BaseService::NotFoundFailure)
expect(create_result.error.message).to eq('customer_not_found')
end
end
end

context 'when coupon is not found' do
let(:coupon_id) { 'foo' }

it { expect(create_result).not_to be_success }
it { expect(create_result.error_code).to eq('missing_argument') }
it { expect(create_result.error).to eq('coupon_does_not_exist') }
it 'returns a not found error' do
aggregate_failures do
expect(create_result).not_to be_success
expect(create_result.error).to be_a(BaseService::NotFoundFailure)
expect(create_result.error.message).to eq('coupon_not_found')
end
end
end

context 'when coupon is inactive' do
before { coupon.terminated! }

it { expect(create_result).not_to be_success }
it { expect(create_result.error_code).to eq('missing_argument') }
it { expect(create_result.error).to eq('coupon_does_not_exist') }
it 'returns a not found error' do
aggregate_failures do
expect(create_result).not_to be_success
expect(create_result.error).to be_a(BaseService::NotFoundFailure)
expect(create_result.error.message).to eq('coupon_not_found')
end
end
end

context 'when customer does not have a subscription' do
Expand Down Expand Up @@ -176,8 +188,8 @@
customer_id: applied_coupon.customer.id,
coupon_code: applied_coupon.coupon.code,
coupon_name: applied_coupon.coupon.name,
organization_id: applied_coupon.coupon.organization_id
}
organization_id: applied_coupon.coupon.organization_id,
},
)
end

Expand All @@ -200,25 +212,37 @@
let(:customer) { nil }
let(:external_customer_id) { 'foo' }

it { expect(create_result).not_to be_success }
it { expect(create_result.error_code).to eq('missing_argument') }
it { expect(create_result.error).to eq('unable_to_find_customer') }
it 'returns a not found error' do
aggregate_failures do
expect(create_result).not_to be_success
expect(create_result.error).to be_a(BaseService::NotFoundFailure)
expect(create_result.error.message).to eq('customer_not_found')
end
end
end

context 'when coupon is not found' do
let(:coupon_code) { 'foo' }

it { expect(create_result).not_to be_success }
it { expect(create_result.error_code).to eq('missing_argument') }
it { expect(create_result.error).to eq('coupon_does_not_exist') }
it 'returns a not found error' do
aggregate_failures do
expect(create_result).not_to be_success
expect(create_result.error).to be_a(BaseService::NotFoundFailure)
expect(create_result.error.message).to eq('coupon_not_found')
end
end
end

context 'when coupon is inactive' do
before { coupon.terminated! }

it { expect(create_result).not_to be_success }
it { expect(create_result.error_code).to eq('missing_argument') }
it { expect(create_result.error).to eq('coupon_does_not_exist') }
it 'returns a not found error' do
aggregate_failures do
expect(create_result).not_to be_success
expect(create_result.error).to be_a(BaseService::NotFoundFailure)
expect(create_result.error.message).to eq('coupon_not_found')
end
end
end

context 'when customer does not have a subscription' do
Expand Down
Loading