Skip to content
Open
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
2 changes: 2 additions & 0 deletions lib/buttercms-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ def self.api_request(path, options = {})
case response
when Net::HTTPNotFound
raise ::ButterCMS::NotFound, JSON.parse(response.body)["detail"]
when Net::HTTPUnauthorized
raise ::ButterCMS::Unauthorized, JSON.parse(response.body)['detail']
end

response.body
Expand Down
3 changes: 3 additions & 0 deletions lib/buttercms/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ class Error < StandardError
# NotFound is raised when a resource cannot be found
class NotFound < Error
end

class Unauthorized < Error
end

class BadRequest < Error
end
Expand Down
2 changes: 1 addition & 1 deletion lib/buttercms/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ButterCMS
VERSION = '2.2'
VERSION = '2.3'
end
13 changes: 13 additions & 0 deletions spec/lib/butter-ruby_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,18 @@

expect(request).to have_been_made
end

it "raises Unauthorized on 401" do
allow(ButterCMS).to receive(:api_token).and_return("test")

request = stub_request(:get, %r{/posts/slug/})
.with(query: { auth_token: "test" })
.to_return(status: 401, body: '{"detail":"Invalid token."}')

expect { ButterCMS.request("/posts/slug/") }
.to raise_error(ButterCMS::Unauthorized)

expect(request).to have_been_made
end
end
end