
Delete the word "timeout" from a Go DNS error string and this library stops being able to tell you what happened. That is the whole bug, and it took me a red test to see it.
The function looked reasonable at a glance — three cases, one fallback:
func parseBasicErr(err error) error {
re := err.Error()
switch {
case strings.Contains(re, "timeout"):
return ErrTimeout
case strings.Contains(re, "no such host"):
return ErrNoSuchHost
case strings.Contains(re, "unavailable"):
return ErrServerUnavailable
}
return LookupError{Message: re}
}
Now ask a resolver for a domain that does not answer properly, and watch the text it hands back:
lookup example.com on 127.0.0.11:53: server misbehaving
No "timeout", no "no such host", no "unavailable". Zero of three. The lookup falls through to the fallback, and the raw Go sentence — "server misbehaving" — becomes the value that every caller downstream switches on.
I assumed the string matching was legacy decoration around a typed error. I was wrong. The fallback is the path taken by every resolver failure that is not a timeout or an NXDOMAIN, which is to say: every time the resolver is confused rather than definitive.
The error object, meanwhile, knew:
&net.DNSError{Err:"server misbehaving", IsTimeout:false, IsTemporary:true, IsNotFound:false}
IsTemporary:true is the sentence "try again later". The classifier had it, ignored it, and kept the prose.
Why this matters more than it looks: a resolver hiccup now reads as a fact about the address. Code that retries on "unavailable" and gives up on "no such host" takes the terminal branch on a transient failure. Nothing raises, nothing logs louder than the ordinary path — the only difference is a wrong answer that is structurally indistinguishable from a right one.
The dumb part is that the fix is four lines. It reads flags first and keeps the strings behind them:
var dnsErr *net.DNSError
if errors.As(err, &dnsErr) {
switch {
case dnsErr.IsTimeout:
return ErrTimeout
case dnsErr.IsNotFound:
return ErrNoSuchHost
case dnsErr.IsTemporary:
return ErrServerUnavailable
}
}
Two things I did not expect. First, net.Dial puts the same *net.DNSError inside a *net.OpError, and errors.As unwraps it, so both call sites got the fix without touching the dial path. Second, the string fallback cannot be removed: ParseSMTPError is an exported function whose input is a string that some other server already flattened. Deleting it breaks callers I will never see.
Then came the part where I stopped trusting myself. A new test that passes on both old and new code is worthless, so I disabled the fix with if false && errors.As(...) and ran the tests against the old behaviour:
expected: "Mail server is unavailable"
actual: "lookup example.com: server misbehaving"
Red. Restored the fix, green. One red run was all it took to show how happily I would have shipped a test that asserted nothing.
The tests never dial the internet: a local UDP server answers RCode 5, the client is built with PreferGo plus a custom dialer, and one test covers five shapes — bare *net.DNSError, the same error wrapped in *net.OpError, a timeout, NXDOMAIN, and a string with no flags at all.
srv := newFakeDNS(t, dns.RcodeServerFailure)
What I'd test first
go test ./... here is 127 pass, 3 fail: two Gravatar tests with no route from this machine, one SMTP dial test with port 25 blocked. All three fail identically on untouched main, same box and toolchain — the baseline is the point, because "3 failures" alone tells you nothing.
CI disagreed in a more interesting way: Lint green, 1.25.x green, 1.26.x green, 1.27.x red with eleven dial tcp ...:25: i/o timeout. Those tests dial real mail servers, so the runner's firewall becomes the test. Main had the same shape (run 46029989870), and a re-run two minutes later (46030262455) went green with no code change. And if you are the outside contributor, GitHub will not let you re-run the failed jobs of a fork PR — you post the numbers and explain them instead of clicking until it looks good.
My take: matching on error text is a dependency on somebody else's wording, and nobody signed the contract on their side. The safe version of it is a fallback you can prove never fires, not a primary classifier. If the runtime hands you a flag, reading the string is a bug you have not been punished for yet.
Question I actually want answered: in Go 1.25, when your upstream resolver returns SERVFAIL for a custom PreferGo client, does IsTemporary come back true — and if your logs only keep err.Error(), how would you ever find out it changed?
The tool I run is in my profile.
About the author
- Indie developer building practical CLI tools for Linux auditing and B2B data extraction. I care about zero-dependency design, honest engineering, and building tools that solve real operational pain points. Currently maintaining Vaultcheck and NPILift.
Comments
Join the discussionNo comments yet. Be the first to comment.




