Skip to content

Commit a89ce4c

Browse files
authored
Merge pull request #3938 from jandubois/error-as
Use errors.As() instead of type assertions
2 parents e49e987 + d871572 commit a89ce4c

File tree

3 files changed

+10
-12
lines changed

3 files changed

+10
-12
lines changed

cmd/limactl/main.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func main() {
4747
rootCmd := newApp()
4848
if err := executeWithPluginSupport(rootCmd, os.Args[1:]); err != nil {
4949
server.StopAllExternalDrivers()
50-
handleExitCoder(err)
50+
handleExitError(err)
5151
logrus.Fatal(err)
5252
}
5353

@@ -200,17 +200,13 @@ func newApp() *cobra.Command {
200200
return rootCmd
201201
}
202202

203-
type ExitCoder interface {
204-
error
205-
ExitCode() int
206-
}
207-
208-
func handleExitCoder(err error) {
203+
func handleExitError(err error) {
209204
if err == nil {
210205
return
211206
}
212207

213-
if exitErr, ok := err.(ExitCoder); ok {
208+
var exitErr *exec.ExitError
209+
if errors.As(err, &exitErr) {
214210
os.Exit(exitErr.ExitCode()) //nolint:revive // it's intentional to call os.Exit in this function
215211
return
216212
}
@@ -253,7 +249,7 @@ func runExternalPlugin(ctx context.Context, name string, args []string) {
253249
cmd.Env = os.Environ()
254250

255251
err = cmd.Run()
256-
handleExitCoder(err)
252+
handleExitError(err)
257253
if err == nil {
258254
os.Exit(0) //nolint:revive // it's intentional to call os.Exit in this function
259255
}

pkg/downloader/downloader.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,8 +564,9 @@ func decompressLocal(ctx context.Context, decompressCmd, dst, src, ext, descript
564564
bar.Start()
565565
err = cmd.Run()
566566
if err != nil {
567-
if ee, ok := err.(*exec.ExitError); ok {
568-
ee.Stderr = buf.Bytes()
567+
var exitErr *exec.ExitError
568+
if errors.As(err, &exitErr) {
569+
exitErr.Stderr = buf.Bytes()
569570
}
570571
}
571572
bar.Finish()

pkg/networks/usernet/udpfileconn.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ type UDPFileConn struct {
1616
func (conn *UDPFileConn) Read(b []byte) (n int, err error) {
1717
// Check if the connection has been closed
1818
if err := conn.SetReadDeadline(time.Time{}); err != nil {
19-
if opErr, ok := err.(*net.OpError); ok && opErr.Err.Error() == "use of closed network connection" {
19+
var opErr *net.OpError
20+
if errors.As(err, &opErr) && opErr.Err.Error() == "use of closed network connection" {
2021
return 0, errors.New("UDPFileConn connection closed")
2122
}
2223
}

0 commit comments

Comments
 (0)