Skip to content

Commit

Permalink
Refactor error handling in request functions; improve error messages …
Browse files Browse the repository at this point in the history
…for clarity in GetRequestByResourceId, PostRequest, and PutRequestByResourceId. Introduce PutResponse struct for better response management.
  • Loading branch information
ShocOne committed Dec 1, 2024
1 parent 22bfe8d commit a40cddf
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
4 changes: 2 additions & 2 deletions internal/client/graphcustom/get_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func GetRequestByResourceId(ctx context.Context, adapter abstractions.RequestAda
func makeRequest(ctx context.Context, adapter abstractions.RequestAdapter, requestInfo *abstractions.RequestInformation) ([]byte, error) {
nativeReq, err := adapter.ConvertToNativeRequest(ctx, requestInfo)
if err != nil {
return nil, fmt.Errorf("error converting to native request: %w", err)
return nil, fmt.Errorf("error converting to native HTTP request: %w", err)
}

httpReq := nativeReq.(*http.Request)
Expand All @@ -191,7 +191,7 @@ func makeRequest(ctx context.Context, adapter abstractions.RequestAdapter, reque

resp, err := client.Do(httpReq)
if err != nil {
return nil, fmt.Errorf("error executing request: %w", err)
return nil, fmt.Errorf("error executing GET request: %w", err)
}
defer resp.Body.Close()

Expand Down
4 changes: 2 additions & 2 deletions internal/client/graphcustom/post_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func PostRequest(
// Send the request using the adapter's Send method
result, err := adapter.Send(ctx, requestInfo, factory, errorMappings)
if err != nil {
return nil, fmt.Errorf("error sending request: %v", err)
return nil, err
}

return result, nil
Expand Down Expand Up @@ -106,7 +106,7 @@ func PostRequestNoContent(ctx context.Context, adapter abstractions.RequestAdapt
// Use SendNoContent for requests that don't return a response body
err = adapter.SendNoContent(ctx, requestInfo, nil)
if err != nil {
return fmt.Errorf("error sending post request: %v", err)
return err
}

return nil
Expand Down
7 changes: 6 additions & 1 deletion internal/client/graphcustom/put_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ type PutRequestConfig struct {
RequestBody s.Parsable
}

type PutResponse struct {
StatusCode int
Error error
}

// PutRequestByResourceId performs a custom PUT request using the Microsoft Graph SDK when the operation
// is not available in the generated SDK methods. This function supports both Beta and V1.0 Graph API versions
// and expects a 204 No Content response from the server on success.
Expand Down Expand Up @@ -78,7 +83,7 @@ func PutRequestByResourceId(ctx context.Context, adapter abstractions.RequestAda

err = adapter.SendNoContent(ctx, requestInfo, nil)
if err != nil {
return fmt.Errorf("error sending request: %v", err)
return err
}

return nil
Expand Down

0 comments on commit a40cddf

Please sign in to comment.