golang.zx2c4.com/wireguard/windows@v0.5.4-0.20230123132234-dcc0eb72a04b/services/errors.go (about)

     1  /* SPDX-License-Identifier: MIT
     2   *
     3   * Copyright (C) 2019-2022 WireGuard LLC. All Rights Reserved.
     4   */
     5  
     6  package services
     7  
     8  import (
     9  	"fmt"
    10  
    11  	"golang.org/x/sys/windows"
    12  )
    13  
    14  type Error uint32
    15  
    16  const (
    17  	ErrorSuccess Error = iota
    18  	ErrorRingloggerOpen
    19  	ErrorLoadConfiguration
    20  	ErrorCreateNetworkAdapter
    21  	ErrorDNSLookup
    22  	ErrorFirewall
    23  	ErrorDeviceSetConfig
    24  	ErrorDeviceBringUp
    25  	ErrorBindSocketsToDefaultRoutes
    26  	ErrorMonitorMTUChanges
    27  	ErrorSetNetConfig
    28  	ErrorDetermineExecutablePath
    29  	ErrorTrackTunnels
    30  	ErrorEnumerateSessions
    31  	ErrorDropPrivileges
    32  	ErrorRunScript
    33  	ErrorWin32
    34  )
    35  
    36  func (e Error) Error() string {
    37  	switch e {
    38  	case ErrorSuccess:
    39  		return "No error"
    40  	case ErrorRingloggerOpen:
    41  		return "Unable to open log file"
    42  	case ErrorDetermineExecutablePath:
    43  		return "Unable to determine path of running executable"
    44  	case ErrorLoadConfiguration:
    45  		return "Unable to load configuration from path"
    46  	case ErrorCreateNetworkAdapter:
    47  		return "Unable to create network adapter"
    48  	case ErrorDNSLookup:
    49  		return "Unable to resolve one or more DNS hostname endpoints"
    50  	case ErrorFirewall:
    51  		return "Unable to enable firewall rules"
    52  	case ErrorDeviceSetConfig:
    53  		return "Unable to set device configuration"
    54  	case ErrorDeviceBringUp:
    55  		return "Unable to bring up adapter"
    56  	case ErrorBindSocketsToDefaultRoutes:
    57  		return "Unable to bind sockets to default route"
    58  	case ErrorMonitorMTUChanges:
    59  		return "Unable to monitor default route MTU for changes"
    60  	case ErrorSetNetConfig:
    61  		return "Unable to configure adapter network settings"
    62  	case ErrorTrackTunnels:
    63  		return "Unable to track existing tunnels"
    64  	case ErrorEnumerateSessions:
    65  		return "Unable to enumerate current sessions"
    66  	case ErrorDropPrivileges:
    67  		return "Unable to drop privileges"
    68  	case ErrorRunScript:
    69  		return "An error occurred while running a configuration script command"
    70  	case ErrorWin32:
    71  		return "An internal Windows error has occurred"
    72  	default:
    73  		return "An unknown error has occurred"
    74  	}
    75  }
    76  
    77  func DetermineErrorCode(err error, serviceError Error) (bool, uint32) {
    78  	if syserr, ok := err.(windows.Errno); ok {
    79  		return false, uint32(syserr)
    80  	} else if serviceError != ErrorSuccess {
    81  		return true, uint32(serviceError)
    82  	} else {
    83  		return false, windows.NO_ERROR
    84  	}
    85  }
    86  
    87  func CombineErrors(err error, serviceError Error) error {
    88  	if serviceError != ErrorSuccess {
    89  		if err != nil {
    90  			return fmt.Errorf("%v: %w", serviceError, err)
    91  		}
    92  		return serviceError
    93  	}
    94  	return err
    95  }