Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions internal/hns/hnsfuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,27 @@ func hnsCallRawResponse(method, path, request string) (*hnsResponse, error) {
return hnsresponse, nil
}

type HNSError struct {
ErrorString string
ErrorCode uint32
}

func (e *HNSError) Error() string {
return fmt.Sprintf("hns failed with error : %s", e.ErrorString)
}
Comment on lines +31 to +38

var hnsCallRawResponseMock = hnsCallRawResponse

func hnsCall(method, path, request string, returnResponse interface{}) error {
hnsresponse, err := hnsCallRawResponse(method, path, request)
hnsresponse, err := hnsCallRawResponseMock(method, path, request)
if err != nil {
return fmt.Errorf("failed during hnsCallRawResponse: %w", err)
}
if !hnsresponse.Success {
return fmt.Errorf("hns failed with error : %s", hnsresponse.Error)
return &HNSError{
ErrorString: hnsresponse.Error,
ErrorCode: hnsresponse.ErrorCode,
}
Comment on lines +48 to +51
}

if len(hnsresponse.Output) == 0 {
Expand Down
42 changes: 42 additions & 0 deletions internal/hns/hnsfuncs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//go:build windows

package hns

import (
"errors"
"testing"
)

func TestHNSErrorPreservation(t *testing.T) {
// Mock the raw response
originalMock := hnsCallRawResponseMock
defer func() { hnsCallRawResponseMock = originalMock }()

hnsCallRawResponseMock = func(method, path, request string) (*hnsResponse, error) {
return &hnsResponse{
Success: false,
Error: "vSwitch in transient state",
ErrorCode: 0x803b0013, // HCS_E_VMSWITCH_IN_TRANSIENT_STATE
}, nil
}

Comment on lines +5 to +22
// Execute hnsCall
err := hnsCall("POST", "/endpoints", "{}", nil)
if err == nil {
t.Fatalf("expected error, got nil")
}

// Assert errors.As
var hnsErr *HNSError
if !errors.As(err, &hnsErr) {
t.Fatalf("expected error to be of type *HNSError, got %T: %v", err, err)
}

if hnsErr.ErrorCode != 0x803b0013 {
t.Errorf("expected ErrorCode 0x803b0013, got %#x", hnsErr.ErrorCode)
}

if hnsErr.ErrorString != "vSwitch in transient state" {
t.Errorf("expected ErrorString 'vSwitch in transient state', got %s", hnsErr.ErrorString)
}
}
7 changes: 4 additions & 3 deletions internal/hns/hnsnetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ type HNSNetwork struct {
}

type hnsResponse struct {
Success bool
Error string
Output json.RawMessage
Success bool
Error string
ErrorCode uint32 `json:"ErrorCode,omitempty"`
Output json.RawMessage
}
Comment on lines +46 to 50

// HNSNetworkRequest makes a call into HNS to update/query a single network
Expand Down
Loading