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(error): Turn more service errors into not found or not allowed #445

Merged
merged 1 commit into from
Sep 13, 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
4 changes: 2 additions & 2 deletions app/graphql/concerns/execution_error_responder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ def not_found_error(resource:)

def not_allowed_error(code:)
execution_error(
message: 'Method Not Allowed',
error: 'Method Not Allowed',
status: 405,
code: code,
)
end

def result_error(service_result)
case service_result.error.class
case service_result.error
when BaseService::NotFoundFailure
return not_found_error(resource: service_result.error.resource)
when BaseService::MethodNotAllowedFailure
Expand Down
2 changes: 1 addition & 1 deletion app/services/invites/revoke_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Invites
class RevokeService < BaseService
def call(**args)
invite = args[:current_organization].invites.pending.find_by(id: args[:id], status: :pending)
return result.fail!(code: 'invite_not_found') unless invite
return result.not_found_failure!(resource: 'invite') unless invite

invite.mark_as_revoked!

Expand Down
6 changes: 3 additions & 3 deletions app/services/invoices/payments/stripe_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def create

def update_status(provider_payment_id:, status:)
payment = Payment.find_by(provider_payment_id: provider_payment_id)
return result.fail!(code: 'stripe_payment_not_found') unless payment
return result.not_found_failure!(resource: 'stripe_payment') unless payment

result.payment = payment
result.invoice = payment.invoice
Expand Down Expand Up @@ -185,8 +185,8 @@ def track_payment_status_changed(invoice)
properties: {
organization_id: invoice.organization.id,
invoice_id: invoice.id,
payment_status: invoice.status
}
payment_status: invoice.status,
},
)
end
end
Expand Down
10 changes: 2 additions & 8 deletions app/services/memberships/revoke_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,8 @@ module Memberships
class RevokeService < BaseService
def call(id)
membership = Membership.find_by(id: id)
return result.fail!(code: 'membership_not_found') unless membership

if result.user.id == membership.user.id
return result.fail!(
code: 'unprocessable_entity',
message: 'Cannot revoke own membership',
)
end
return result.not_found_failure!(resource: 'membership') unless membership
return result.not_allowed_failure!(code: 'cannot_revoke_own_membership') if result.user.id == membership.user.id

membership.mark_as_revoked!

Expand Down
7 changes: 5 additions & 2 deletions spec/graphql/mutations/invites/revoke_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
GQL
end

describe 'Invite revoke mutation' do
describe 'Invite revoke mutation' do
context 'with an existing invite' do
let(:invite) { create(:invite, organization: organization) }

Expand Down Expand Up @@ -53,7 +53,10 @@
},
)

expect(result['errors'].first['message']).to eq('invite_not_found')
aggregate_failures do
expect(result['errors'].first['message']).to eq('Resource not found')
expect(result['errors'].first['extensions']['code']).to eq('invite_not_found')
end
end
end
end
Expand Down
7 changes: 5 additions & 2 deletions spec/graphql/mutations/memberships/revoke_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@
},
)

expect(result['errors'].first['message']).to eq('Cannot revoke own membership')
expect(result['errors'].first['extensions']['status']).to eq(422)
aggregate_failures do
expect(result['errors'].first['message']).to eq('Method Not Allowed')
expect(result['errors'].first['extensions']['code']).to eq('cannot_revoke_own_membership')
expect(result['errors'].first['extensions']['status']).to eq(405)
end
end
end
6 changes: 3 additions & 3 deletions spec/services/invites/revoke_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
result = revoke_service.call(**revoke_args)

expect(result).not_to be_success
expect(result.error).to eq('invite_not_found')
expect(result.error.error_code).to eq('invite_not_found')
end
end

Expand All @@ -39,7 +39,7 @@
result = revoke_service.call(**revoke_args)

expect(result).not_to be_success
expect(result.error).to eq('invite_not_found')
expect(result.error.error_code).to eq('invite_not_found')
end
end

Expand All @@ -56,7 +56,7 @@
result = revoke_service.call(**revoke_args)

expect(result).not_to be_success
expect(result.error).to eq('invite_not_found')
expect(result.error.error_code).to eq('invite_not_found')
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/services/memberships/revoke_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
result = revoke_service.call(membership.id)

expect(result).not_to be_success
expect(result.error).to eq('Cannot revoke own membership')
expect(result.error.code).to eq('cannot_revoke_own_membership')
end
end

Expand All @@ -22,7 +22,7 @@
result = revoke_service.call(nil)

expect(result).not_to be_success
expect(result.error).to eq('membership_not_found')
expect(result.error.error_code).to eq('membership_not_found')
end
end

Expand Down