Skip to content
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -475,8 +475,8 @@ urls.each do |url|
end
end

# => "[404] not found error with url: https://httpbin.org/status/404 (Halite::ClientError)"
# => "[500] internal server error error with url: https://httpbin.org/status/500?foo=bar (Halite::ServerError)"
# => "[404] not found error with url: https://httpbin.org/status/404 (Halite::Exception::ClientError)"
# => "[500] internal server error error with url: https://httpbin.org/status/500?foo=bar (Halite::Exception::ServerError)"
# => ""
```

Expand Down
54 changes: 54 additions & 0 deletions spec/halite/error_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require "../spec_helper"

describe Halite::Exception do
describe "#APIError" do
it "should initial without arguments" do
error = Halite::APIError.new
error.message.should be_nil
error.status_code.should be_nil
error.status_message.not_nil!.should eq "unknown error"
end

it "should initial with message only" do
message = "foobar"

error = Halite::APIError.new(message)
error.message.not_nil!.should eq message
error.status_code.should be_nil
error.status_message.not_nil!.should eq "foobar error"
end

it "should initial with message and status_code" do
message = "foobar"
status_code = 400

error = Halite::APIError.new(message, status_code)
error.message.not_nil!.should eq message
error.status_code.not_nil!.should eq status_code
error.status_message.not_nil!.should eq "bad request error"
end

it "should initial with full arguments" do
message = "foobar"
status_code = 400
uri = URI.parse("https://www.example.com/get/foobar")

error = Halite::APIError.new(message, status_code, uri)
error.message.not_nil!.should eq message
error.status_code.not_nil!.should eq status_code
error.uri.not_nil!.should eq uri
error.status_message.not_nil!.should eq "bad request error with url: #{uri}"
end

it "should initial without message" do
status_code = 400
uri = URI.parse("https://www.example.com/get/foobar")

error = Halite::APIError.new(nil, status_code, uri)
error.message.not_nil!.should eq "#{status_code} bad request error with url: #{uri}"
error.status_code.not_nil!.should eq status_code
error.uri.not_nil!.should eq uri
error.status_message.not_nil!.should eq "bad request error with url: #{uri}"
end
end
end
21 changes: 15 additions & 6 deletions src/halite/error.cr
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,28 @@ module Halite
class APIError < ResponseError
getter uri
getter status_code
getter status_message
getter status_message : String? = nil

def initialize(@message : String? = nil, @status_code : Int32? = nil, @uri : URI? = nil)
@status_message = build_status_message
if status_code = @status_code
status_message = [HTTP::Status.new(status_code).description.to_s.downcase]
status_message << "error"
status_message << "with url: #{@uri.not_nil!}" if @uri

@message ||= "#{status_code} #{status_message.join(" ")} "
@message ||= "#{status_code} #{@status_message}"
end

super(@message)
end

private def build_status_message : String
String::Builder.build do |io|
if status_code = @status_code
io << "#{HTTP::Status.new(status_code).description.to_s.downcase} error"
else
io << "#{@message || "unknown"} error"
end

io << " with url: #{@uri}" if uri = @uri
end.to_s
end
end

# 4XX client error
Expand Down