From 7e4207161c9bffe7f96f77f7073574d99711b231 Mon Sep 17 00:00:00 2001 From: Ethan <39577870+ethanndickson@users.noreply.github.com> Date: Thu, 10 Jul 2025 05:15:21 +0000 Subject: [PATCH] fix(cli): handle nil unwrap errors when formatting (#18099) Discovered an unhelpful error when running a CLI command without internet (I didn't know I didn't have internet!): ``` $ coder ls Encountered an error running "coder list", see "coder list --help" for more information error: ``` The source of this was that calling `Unwrap()` on `net.DNSError` can return nil, causing the whole error trace to get replaced by it. Instead, we'll just treat a nil `Unwrap()` return value as if there was nothing to unwrap. The result is: ``` $ coder ls Encountered an error running "coder list", see "coder list --help" for more information error: query workspaces: Get "https://dev.coder.com/api/v2/workspaces?q=owner%3Ame": dial tcp: lookup dev.coder.com: no such host ``` --- cli/root.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/cli/root.go b/cli/root.go index 8fec1a945b0b3..22a1c0f3ac329 100644 --- a/cli/root.go +++ b/cli/root.go @@ -1060,11 +1060,12 @@ func cliHumanFormatError(from string, err error, opts *formatOpts) (string, bool return formatRunCommandError(cmdErr, opts), true } - uw, ok := err.(interface{ Unwrap() error }) - if ok { - msg, special := cliHumanFormatError(from+traceError(err), uw.Unwrap(), opts) - if special { - return msg, special + if uw, ok := err.(interface{ Unwrap() error }); ok { + if unwrapped := uw.Unwrap(); unwrapped != nil { + msg, special := cliHumanFormatError(from+traceError(err), unwrapped, opts) + if special { + return msg, special + } } } // If we got here, that means that the wrapped error chain does not have