Skip to content

Commit

Permalink
Report a more friendly error message on WSAEACCES
Browse files Browse the repository at this point in the history
  • Loading branch information
punmechanic committed Mar 7, 2024
1 parent da02549 commit f1c49a7
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,28 @@ package main

import (
"errors"
"fmt"
"os"
"strings"
"syscall"

"github.com/spf13/cobra"
"golang.org/x/exp/slog"
)

const (
// WSAEACCES is the Windows error code for attempting to access a socket that you don't have permission to access.
//
// This commonly occurs if the socket is in use or was not closed correctly, and can be resolved by restarting the hns service.
WSAEACCES = 10013
)

// IsWindowsPortAccessError determines if the given error is the error WSAEACCES.
func IsWindowsPortAccessError(err error) bool {
var syscallErr *syscall.Errno
return errors.As(err, &syscallErr) && *syscallErr == WSAEACCES
}

func init() {
var opts slog.HandlerOptions
if os.Getenv("DEBUG") == "1" {
Expand All @@ -27,6 +42,12 @@ func main() {
rootCmd.SetArgs(args)

err := rootCmd.Execute()
if IsWindowsPortAccessError(err) {
fmt.Fprintf(os.Stderr, "Encountered an issue when opening the port for KeyConjurer: %s\n", err)
fmt.Fprintln(os.Stderr, "Consider running `net stop hns` and then `net start hns`")
os.Exit(ExitCodeConnectivityError)
}

var codeErr codeError
if errors.As(err, &codeErr) {
cobra.CheckErr(codeErr)
Expand Down

0 comments on commit f1c49a7

Please sign in to comment.