Skip to content

Commit 26f99ae

Browse files
committed
Merge pull request #4 from github/reason
Return human readable error messages
2 parents 251941a + 2928da0 commit 26f99ae

File tree

6 files changed

+64
-4
lines changed

6 files changed

+64
-4
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
### Getting checks in bulk
4141

42-
```
42+
```ruby
4343
> check.to_hash
4444
=> {
4545
:cloudflare_ip?=>false,
@@ -57,3 +57,15 @@
5757
> check.to_json
5858
=> "{\"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}"
5959
```
60+
61+
### Getting the reason a domain is invalid
62+
63+
```ruby
64+
> check = GitHubPages::HealthCheck.new "developer.facebook.com"
65+
> check.valid?
66+
=> false
67+
> check.reason
68+
=> #<GitHubPages::HealthCheck::InvalidCNAME>
69+
> check.reason.message
70+
=> "CNAME does not point to GitHub Pages"
71+
```

lib/github-pages-health-check.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
require 'singleton'
66
require_relative 'github-pages-health-check/version'
77
require_relative 'github-pages-health-check/cloudflare'
8+
require_relative 'github-pages-health-check/errors/deprecated_ip'
9+
require_relative 'github-pages-health-check/errors/invalid_a_record'
10+
require_relative 'github-pages-health-check/errors/invalid_cname'
811

912
class GitHubPages
1013
class HealthCheck
@@ -91,14 +94,16 @@ def to_hash
9194
:should_be_a_record? => should_be_a_record?,
9295
:pointed_to_github_user_domain? => pointed_to_github_user_domain?,
9396
:pages_domain? => pages_domain?,
94-
:valid? => valid?
97+
:valid? => valid?,
98+
:reason => reason
9599
}
96100
end
97101

98102
def to_json
99103
to_hash.to_json
100104
end
101105

106+
# Runs all checks, raises an error if invalid
102107
def check!
103108
return unless dns
104109
return if cloudflare_ip?
@@ -109,13 +114,22 @@ def check!
109114
end
110115
alias_method :valid!, :check!
111116

117+
# Runs all checks, returns true if valid, otherwise false
112118
def valid?
113119
check!
114120
true
115121
rescue
116122
false
117123
end
118124

125+
# Return the error, if any
126+
def reason
127+
check!
128+
nil
129+
rescue StandardError => e
130+
e
131+
end
132+
119133
def inspect
120134
"#<GitHubPages::HealthCheck @domain=\"#{domain}\" valid?=#{valid?}>"
121135
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class GitHubPages
2+
class HealthCheck
3+
class DeprecatedIP < StandardError
4+
def message
5+
"A record points to deprecated IP address"
6+
end
7+
end
8+
end
9+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class GitHubPages
2+
class HealthCheck
3+
class InvalidARecord < StandardError
4+
def message
5+
"Should not be an A record"
6+
end
7+
end
8+
end
9+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class GitHubPages
2+
class HealthCheck
3+
class InvalidCNAME < StandardError
4+
def message
5+
"CNAME does not point to GitHub Pages"
6+
end
7+
end
8+
end
9+
end

spec/github_pages_health_check_spec.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,15 @@ def cname_packet(domain)
123123

124124
it "returns valid json" do
125125
data = JSON.parse GitHubPages::HealthCheck.new("benbalter.com").to_json
126-
expect(data.length).to eql(10)
127-
data.each { |key, value| expect([true,false].include?(value)).to eql(true) }
126+
expect(data.length).to eql(11)
127+
data.each { |key, value| expect([true,false,nil].include?(value)).to eql(true) }
128+
end
129+
130+
it "return the error" do
131+
check = GitHubPages::HealthCheck.new "developer.facebook.com"
132+
expect(check.valid?).to eql(false)
133+
expect(check.reason.class).to eql(GitHubPages::HealthCheck::InvalidCNAME)
134+
expect(check.reason.message).to eql("CNAME does not point to GitHub Pages")
128135
end
129136

130137
end

0 commit comments

Comments
 (0)