Skip to content
This repository was archived by the owner on Apr 13, 2025. It is now read-only.
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ client.project(project_id).stories(fields: ':default,tasks') # Eage
story.comments(fields: ':default,person') # Eagerly get comments and the person that made the comment for a story
```

## Error Handling
`TrackerApi::Errors::ClientError` is raised for 4xx HTTP status codes
`TrackerApi::Errors::ServerError` is raised for 5xx HTTP status codes

## Warning

Direct mutation of an attribute value skips coercion and dirty tracking. Please use direct assignment or the specialized add_* methods to get expected behavior.
Expand All @@ -118,6 +122,7 @@ story.save

- Add missing resources and endpoints
- Add create, update, delete for resources
- Error handling for [error responses](https://www.pivotaltracker.com/help/api#Error_Responses)

## Semantic Versioning
http://semver.org/
Expand Down
2 changes: 2 additions & 0 deletions lib/tracker_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ module TrackerApi

module Errors
class UnexpectedData < StandardError; end
class ClientError < Error; end
class ServerError < Error; end
end

module Endpoints
Expand Down
6 changes: 5 additions & 1 deletion lib/tracker_api/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,11 @@ def request(method, options = {})
end
response
rescue Faraday::Error::ClientError => e
raise TrackerApi::Error.new(e)
case e.response[:status]
when 400..499 then raise TrackerApi::Errors::ClientError.new(e)
when 500..599 then raise TrackerApi::Errors::ServerError.new(e)
else raise "Expected 4xx or 5xx HTTP status code"
end
end

class Pagination
Expand Down
41 changes: 41 additions & 0 deletions test/error_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require_relative 'minitest_helper'

describe TrackerApi::Error do
let(:pt_user) { PT_USER_1 }
let(:client) { TrackerApi::Client.new token: pt_user[:token] }
let(:options) { { url: nil, headers: nil } }

it 'raises ClientError for 4xx HTTP status codes' do
(400..499).each do |status_code|
mock_faraday_error(status_code)
assert_raises TrackerApi::Errors::ClientError do
client.send(:request, :get, options)
end
end
end

it 'raises ServerError for 5xx HTTP status codes' do
(500..599).each do |status_code|
mock_faraday_error(status_code)
assert_raises TrackerApi::Errors::ServerError do
client.send(:request, :get, options)
end
end
end

it 'raises RuntimeError for HTTP status codes < 400 and > 500' do
[399, 600].each do |status_code|
mock_faraday_error(status_code)
assert_raises RuntimeError, "Expected 4xx or 5xx HTTP status code" do
client.send(:request, :get, options)
end
end
end

# Simulate the error Faraday will raise with a specific HTTP status code so
# we can test our rescuing of those errors
def mock_faraday_error(status_code)
::Faraday::Connection.any_instance.stubs(:get).
raises(::Faraday::Error::ClientError.new(nil, { status: status_code}))
end
end