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

✨ Improving error handling #6

Merged
merged 2 commits into from
Mar 6, 2024
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
12 changes: 11 additions & 1 deletion lib/calendlyr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@
module Calendlyr
autoload :Client, "calendlyr/client"
autoload :Collection, "calendlyr/collection"
autoload :Error, "calendlyr/error"
autoload :Resource, "calendlyr/resource"
autoload :Object, "calendlyr/object"

# Errors
autoload :ResponseErrorHandler, "calendlyr/error"
autoload :Error, "calendlyr/error"
autoload :PermissionDenied, "calendlyr/error"
autoload :BadRequest, "calendlyr/error"
autoload :PaymentRequired, "calendlyr/error"
autoload :Unauthenticated, "calendlyr/error"
autoload :NotFound, "calendlyr/error"
autoload :ExternalCalendarEror, "calendlyr/error"
autoload :InternalServerError, "calendlyr/error"

# High-level categories of Calendly API calls
autoload :UserResource, "calendlyr/resources/users"
autoload :EventTypeResource, "calendlyr/resources/event_types"
Expand Down
43 changes: 42 additions & 1 deletion lib/calendlyr/error.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,45 @@
module Calendlyr
class Error < StandardError
class Error < StandardError; end

class PermissionDenied < StandardError; end

class BadRequest < StandardError; end

class PaymentRequired < StandardError; end

class Unauthenticated < StandardError; end

class NotFound < StandardError; end

class ExternalCalendarEror < StandardError; end

class InternalServerError < StandardError; end

class ResponseErrorHandler
ERROR_TYPES = {
"400" => BadRequest,
"401" => Unauthenticated,
"403" => PermissionDenied,
"404" => NotFound,
"424" => ExternalCalendarEror,
"500" => InternalServerError
}

def initialize(code, body)
@code = code
@body = body
end

def error
error_type.new("[Error #{@code}] #{@body["title"]}. #{@body["message"]}")
end

private

def error_type
return PaymentRequired if @code == "403" && @body["message"].include?("upgrade")

ERROR_TYPES[@code]
end
end
end
4 changes: 2 additions & 2 deletions lib/calendlyr/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Calendlyr
class Resource
attr_reader :client

ERROR_CODES = %w[400 401 403 404 409 500]
ERROR_CODES = %w[400 401 403 404 424 500]

def initialize(client)
@client = client
Expand Down Expand Up @@ -52,7 +52,7 @@ def handle_response(response)

body = JSON.parse(response.read_body)
if ERROR_CODES.include? response.code
raise Error, "[Error #{response.code}] #{body["title"]}. #{body["message"]}"
raise ResponseErrorHandler.new(response.code, body).error
else
body
end
Expand Down
15 changes: 12 additions & 3 deletions test/calendlyr/resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@

class ResourceTest < Minitest::Test
def test_handle_response_error
error_code = Calendlyr::Resource::ERROR_CODES.sample
stub(path: "users/me", response: {body: fixture_file("resources/#{error_code}"), status: error_code.to_i})
Calendlyr::ResponseErrorHandler::ERROR_TYPES.each do |error_code, error_class|
stub(path: "users/me", response: {body: fixture_file("resources/#{error_code}"), status: error_code.to_i})

assert_raises Calendlyr::Error do
assert_raises "Calendlyr::#{error_class}" do
client.me
end
end
end

def test_handle_response_error_payment
stub(path: "users/me", response: {body: fixture_file("resources/403_payment_required"), status: 403})

assert_raises Calendlyr::PaymentRequired do
client.me
end
end
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/resources/401.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"title": "Invalid Argument",
"message": "The supplied parameters are invalid.",
"title": "Unauthenticated",
"message": "The access token is invalid",
"details": [
{
"parameter": "string",
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/resources/403.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"title": "Permission Denied",
"message": "This user is not in your organization",
"message": "You do not have permission to access this resource.",
"details": [
{
"parameter": "string",
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/resources/403_payment_required.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"title": "Permission Denied",
"message": "Please upgrade your Calendly account to Enterprise. You do not have permission to access this resource."
}
4 changes: 0 additions & 4 deletions test/fixtures/resources/409.json

This file was deleted.

4 changes: 4 additions & 0 deletions test/fixtures/resources/424.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"title": "External Calendar Error",
"message": "There is a problem accessing your calendar."
}