Skip to content

Commit

Permalink
Handle Zendesk API errors in /support-tickets
Browse files Browse the repository at this point in the history
Otherwise 500 error is returned. The Client apps can then handle those
appropriately.

Ideally calling Zendesk API should be done in a worker that would handle all
possible Zendesk API errors with appropriate retry logic (e.g. don’t retry on
409 :conflict). The controller should respond with 202 :accepted status rather
than 201 :created.
It is not implemented at this time due to change in delivery priorities (and
this work being reprioritised).
  • Loading branch information
AgaDufrat committed Aug 9, 2024
1 parent 770d9fc commit 62977a0
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
12 changes: 12 additions & 0 deletions app/controllers/support_tickets_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
class SupportTicketsController < ApplicationController
rescue_from ZendeskAPI::Error::RecordInvalid, with: :handle_as_validation_error

rescue_from ZendeskAPI::Error::NetworkError, with: :cast_zendesk_api_error

def create
support_ticket = SupportTicket.new(support_ticket_attributes)

Expand All @@ -13,6 +17,14 @@ def create

private

def handle_as_validation_error(error)
render json: { status: "error", errors: error.message }, status: :unprocessable_entity
end

def cast_zendesk_api_error(error)
render json: { status: "error", errors: error.message }, status: error.response[:status]
end

def support_ticket_attributes
params.slice(
:subject, :description, :priority, :requester, :collaborators, :tags, :custom_fields, :ticket_form_id
Expand Down
8 changes: 8 additions & 0 deletions spec/helpers/zendesk_test_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ def zendesk_has_valid_user_with_email(email)
headers: { "Content-Type" => "application/json" })
end

def stub_zendesk_returns_record_invalid
stub_request(:any, /#{zendesk_endpoint}\/.*/).to_return(status: 422)
end

def stub_zendesk_is_unavailable
stub_request(:any, /#{zendesk_endpoint}\/.*/).to_return(status: 503)
end

def zendesk_endpoint
"https://govuk.zendesk.com/api/v2"
end
Expand Down
26 changes: 26 additions & 0 deletions spec/requests/support_tickets_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,32 @@
expect(response_hash).to include("errors" => include("description" => include("can't be blank")))
end

it "sends validation error response if ZendeskAPI::Error::RecordInvalid occurs" do
stub_zendesk_returns_record_invalid
post "/support-tickets",
params: {
subject: "Feedback for app",
description: "Ticket body",
requester: { email: "a@b.com" },
}

expect(response.code).to eq("422")
expect(response_hash).to include("status" => "error")
end

it "casts the error responses if ZendeskAPI::Error::NetworkError occurs" do
stub_zendesk_is_unavailable

post "/support-tickets",
params: {
subject: "Feedback for app",
description: "Ticket body",
}

expect(response.code).to eq("503")
expect(response_hash).to include("status" => "error")
end

def response_hash
JSON.parse(response.body)
end
Expand Down

0 comments on commit 62977a0

Please sign in to comment.