Skip to content

Commit

Permalink
Remove Http prefix from http errors
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii-balitskyi committed Oct 10, 2024
1 parent a5c2c1d commit 644f08b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
12 changes: 7 additions & 5 deletions lib/seam/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ def self.new(**args)
end

def self.from_api_key(api_key, endpoint: nil, wait_for_action_attempt: false, debug: false)
Http::SingleWorkspace.from_api_key(api_key, endpoint: endpoint, wait_for_action_attempt: wait_for_action_attempt, debug: debug)
Http::SingleWorkspace.from_api_key(api_key, endpoint: endpoint, wait_for_action_attempt: wait_for_action_attempt,
debug: debug)
end

def self.from_personal_access_token(personal_access_token, workspace_id, endpoint: nil, wait_for_action_attempt: false, debug: false)
Http::SingleWorkspace.from_personal_access_token(personal_access_token, workspace_id, endpoint: endpoint, wait_for_action_attempt: wait_for_action_attempt, debug: debug)
Http::SingleWorkspace.from_personal_access_token(personal_access_token, workspace_id, endpoint: endpoint,
wait_for_action_attempt: wait_for_action_attempt, debug: debug)
end

class HttpApiError < StandardError
class ApiError < StandardError
attr_reader :code, :status_code, :request_id, :data

def initialize(error, status_code, request_id)
Expand All @@ -28,13 +30,13 @@ def initialize(error, status_code, request_id)
end
end

class HttpUnauthorizedError < HttpApiError
class UnauthorizedError < ApiError
def initialize(request_id)
super({type: "unauthorized", message: "Unauthorized"}, 401, request_id)
end
end

class InvalidInputError < HttpApiError
class InvalidInputError < ApiError
attr_reader :validation_errors

def initialize(error, status_code, request_id)
Expand Down
4 changes: 2 additions & 2 deletions lib/seam/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def handle_error_response(response, _method, _uri)
status_code = response.status.code
request_id = response.headers["seam-request-id"]

raise Http::HttpUnauthorizedError.new(request_id) if status_code == 401
raise Http::UnauthorizedError.new(request_id) if status_code == 401

error = response.parse["error"] || {}
error_type = error["type"] || "unknown_error"
Expand All @@ -52,7 +52,7 @@ def handle_error_response(response, _method, _uri)
)
end

raise Http::HttpApiError.new(error_details, status_code, request_id)
raise Http::ApiError.new(error_details, status_code, request_id)
end

def build_url(uri)
Expand Down
4 changes: 2 additions & 2 deletions spec/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

it "parses the error" do
expect { client.health }.to raise_error do |error|
expect(error).to be_a(Seam::Http::HttpApiError)
expect(error).to be_a(Seam::Http::ApiError)
expect(error.message).to eq(message)
expect(error.code).to eq(type)
expect(error.request_id).to eq(request_id)
Expand All @@ -48,7 +48,7 @@

it "parses the error" do
expect { client.health }.to raise_error do |error|
expect(error).to be_a(Seam::Http::HttpApiError)
expect(error).to be_a(Seam::Http::ApiError)
expect(error.message).to eq(message)
expect(error.code).to eq(type)
expect(error.request_id).to eq(request_id)
Expand Down
8 changes: 4 additions & 4 deletions spec/seam_client/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
.to_return(status: 401, headers: {"seam-request-id" => request_id})
end

it "raises HttpUnauthorizedError" do
expect { seam.devices.list }.to raise_error(Seam::Http::HttpUnauthorizedError) do |error|
it "raises UnauthorizedError" do
expect { seam.devices.list }.to raise_error(Seam::Http::UnauthorizedError) do |error|
expect(error.message).to eq("Unauthorized")
expect(error.request_id).to eq(request_id)
end
Expand Down Expand Up @@ -67,8 +67,8 @@
"seam-request-id" => request_id})
end

it "raises HttpApiError with the correct details" do
expect { seam.devices.list }.to raise_error(Seam::Http::HttpApiError) do |error|
it "raises ApiError with the correct details" do
expect { seam.devices.list }.to raise_error(Seam::Http::ApiError) do |error|
expect(error.message).to eq(error_message)
expect(error.status_code).to eq(error_status)
expect(error.request_id).to eq(request_id)
Expand Down

0 comments on commit 644f08b

Please sign in to comment.