From 7f00c5c8bdd6709f5b604aa0ef5e145af3ccd4f8 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 27 May 2023 16:48:48 +0200 Subject: [PATCH] osxkeychain: use a switch for handling errors Makes the error-handling slightly cleaner. Signed-off-by: Sebastiaan van Stijn --- osxkeychain/osxkeychain_darwin.go | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/osxkeychain/osxkeychain_darwin.go b/osxkeychain/osxkeychain_darwin.go index 33796cf..8d8925d 100644 --- a/osxkeychain/osxkeychain_darwin.go +++ b/osxkeychain/osxkeychain_darwin.go @@ -93,15 +93,14 @@ func (h Osxkeychain) Get(serverURL string) (string, string, error) { errMsg := C.keychain_get(s, &usernameLen, &username, &secretLen, &secret) if errMsg != nil { defer C.free(unsafe.Pointer(errMsg)) - goMsg := C.GoString(errMsg) - if goMsg == errCredentialsNotFound { + switch goMsg := C.GoString(errMsg); goMsg { + case errCredentialsNotFound: return "", "", credentials.NewErrCredentialsNotFound() - } - if goMsg == errInteractionNotAllowed { + case errInteractionNotAllowed: return "", "", ErrInteractionNotAllowed + default: + return "", "", errors.New(goMsg) } - - return "", "", errors.New(goMsg) } user := C.GoStringN(username, C.int(usernameLen)) @@ -124,15 +123,14 @@ func (h Osxkeychain) List() (map[string]string, error) { defer C.freeListData(&acctsC, listLenC) if errMsg != nil { defer C.free(unsafe.Pointer(errMsg)) - goMsg := C.GoString(errMsg) - if goMsg == errCredentialsNotFound { + switch goMsg := C.GoString(errMsg); goMsg { + case errCredentialsNotFound: return make(map[string]string), nil - } - if goMsg == errInteractionNotAllowed { + case errInteractionNotAllowed: return nil, ErrInteractionNotAllowed + default: + return nil, errors.New(goMsg) } - - return nil, errors.New(goMsg) } var listLen int