Skip to content

Return human readable error messages #4

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

Merged
merged 2 commits into from
Aug 28, 2014
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
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

### Getting checks in bulk

```
```ruby
> check.to_hash
=> {
:cloudflare_ip?=>false,
Expand All @@ -57,3 +57,15 @@
> check.to_json
=> "{\"cloudflare_ip?\":false,\"old_ip_address?\":false,\"a_record?\":true,\"cname_record?\":false,\"valid_domain?\":true,\"apex_domain?\":true,\"should_be_a_record?\":true,\"pointed_to_github_user_domain?\":false,\"pages_domain?\":false,\"valid?\":true}"
```

### Getting the reason a domain is invalid

```ruby
> check = GitHubPages::HealthCheck.new "developer.facebook.com"
> check.valid?
=> false
> check.reason
=> #<GitHubPages::HealthCheck::InvalidCNAME>
> check.reason.message
=> "CNAME does not point to GitHub Pages"
```
16 changes: 15 additions & 1 deletion lib/github-pages-health-check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
require 'singleton'
require_relative 'github-pages-health-check/version'
require_relative 'github-pages-health-check/cloudflare'
require_relative 'github-pages-health-check/errors/deprecated_ip'
require_relative 'github-pages-health-check/errors/invalid_a_record'
require_relative 'github-pages-health-check/errors/invalid_cname'

class GitHubPages
class HealthCheck
Expand Down Expand Up @@ -91,14 +94,16 @@ def to_hash
:should_be_a_record? => should_be_a_record?,
:pointed_to_github_user_domain? => pointed_to_github_user_domain?,
:pages_domain? => pages_domain?,
:valid? => valid?
:valid? => valid?,
:reason => reason
}
end

def to_json
to_hash.to_json
end

# Runs all checks, raises an error if invalid
def check!
return unless dns
return if cloudflare_ip?
Expand All @@ -109,13 +114,22 @@ def check!
end
alias_method :valid!, :check!

# Runs all checks, returns true if valid, otherwise false
def valid?
check!
true
rescue
false
end

# Return the error, if any
def reason
check!
nil
rescue StandardError => e
e
end

def inspect
"#<GitHubPages::HealthCheck @domain=\"#{domain}\" valid?=#{valid?}>"
end
Expand Down
9 changes: 9 additions & 0 deletions lib/github-pages-health-check/errors/deprecated_ip.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class GitHubPages
class HealthCheck
class DeprecatedIP < StandardError
def message
"A record points to deprecated IP address"
end
end
end
end
9 changes: 9 additions & 0 deletions lib/github-pages-health-check/errors/invalid_a_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class GitHubPages
class HealthCheck
class InvalidARecord < StandardError
def message
"Should not be an A record"
end
end
end
end
9 changes: 9 additions & 0 deletions lib/github-pages-health-check/errors/invalid_cname.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class GitHubPages
class HealthCheck
class InvalidCNAME < StandardError
def message
"CNAME does not point to GitHub Pages"
end
end
end
end
11 changes: 9 additions & 2 deletions spec/github_pages_health_check_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,15 @@ def cname_packet(domain)

it "returns valid json" do
data = JSON.parse GitHubPages::HealthCheck.new("benbalter.com").to_json
expect(data.length).to eql(10)
data.each { |key, value| expect([true,false].include?(value)).to eql(true) }
expect(data.length).to eql(11)
data.each { |key, value| expect([true,false,nil].include?(value)).to eql(true) }
end

it "return the error" do
check = GitHubPages::HealthCheck.new "developer.facebook.com"
expect(check.valid?).to eql(false)
expect(check.reason.class).to eql(GitHubPages::HealthCheck::InvalidCNAME)
expect(check.reason.message).to eql("CNAME does not point to GitHub Pages")
end

end