Skip to content

Commit 3c31b1e

Browse files
committed
chore: Improve error handling and logging in MarshalMultipartRequest
1 parent 1a1d3dd commit 3c31b1e

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

apiintegrations/jamfpro/jamfpro_api_request.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ func (j *JamfAPIHandler) MarshalRequest(body interface{}, method string, endpoin
5656
return data, nil
5757
}
5858

59-
// MarshalMultipartRequest creates a multipart request body with the provided form fields and files.
6059
func (j *JamfAPIHandler) MarshalMultipartRequest(fields map[string]string, files map[string]string, log logger.Logger) ([]byte, string, string, error) {
6160
body := &bytes.Buffer{}
6261
writer := multipart.NewWriter(body)
6362

6463
// Add the simple fields to the form data
6564
for field, value := range fields {
65+
log.Debug("Adding field to multipart request", zap.String("Field", field), zap.String("Value", value))
6666
if err := writer.WriteField(field, value); err != nil {
6767
return nil, "", "", err
6868
}
@@ -81,6 +81,7 @@ func (j *JamfAPIHandler) MarshalMultipartRequest(fields map[string]string, files
8181
if err != nil {
8282
return nil, "", "", err
8383
}
84+
log.Debug("Adding file to multipart request", zap.String("FormField", formField), zap.String("FilePath", filePath))
8485
if _, err := io.Copy(part, file); err != nil {
8586
return nil, "", "", err
8687
}
@@ -94,7 +95,7 @@ func (j *JamfAPIHandler) MarshalMultipartRequest(fields map[string]string, files
9495
contentType := writer.FormDataContentType()
9596
bodyBytes := body.Bytes()
9697

97-
// Extract the first and last parts of the body
98+
// Extract the first and last parts of the body for logging
9899
const logSegmentSize = 1024 // 1 KB
99100
bodyLen := len(bodyBytes)
100101
var logBody string
@@ -104,5 +105,10 @@ func (j *JamfAPIHandler) MarshalMultipartRequest(fields map[string]string, files
104105
logBody = string(bodyBytes[:logSegmentSize]) + "..." + string(bodyBytes[bodyLen-logSegmentSize:])
105106
}
106107

108+
// Log the boundary and a partial body for debugging
109+
boundary := writer.Boundary()
110+
log.Debug("Multipart boundary", zap.String("Boundary", boundary))
111+
log.Debug("Multipart request body (partial)", zap.String("Body", logBody))
112+
107113
return bodyBytes, contentType, logBody, nil
108114
}

apiintegrations/msgraph/msgraph_api_request.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@ func (g *GraphAPIHandler) MarshalRequest(body interface{}, method string, endpoi
3030
return data, nil
3131
}
3232

33-
// MarshalMultipartRequest creates a multipart request body with the provided form fields and files.
33+
// MarshalMultipartRequest encodes the request body as a multipart message for the Microsoft Graph API.
3434
func (g *GraphAPIHandler) MarshalMultipartRequest(fields map[string]string, files map[string]string, log logger.Logger) ([]byte, string, string, error) {
3535
body := &bytes.Buffer{}
3636
writer := multipart.NewWriter(body)
3737

3838
// Add the simple fields to the form data
3939
for field, value := range fields {
40+
log.Debug("Adding field to multipart request", zap.String("Field", field), zap.String("Value", value))
4041
if err := writer.WriteField(field, value); err != nil {
4142
return nil, "", "", err
4243
}
@@ -55,6 +56,7 @@ func (g *GraphAPIHandler) MarshalMultipartRequest(fields map[string]string, file
5556
if err != nil {
5657
return nil, "", "", err
5758
}
59+
log.Debug("Adding file to multipart request", zap.String("FormField", formField), zap.String("FilePath", filePath))
5860
if _, err := io.Copy(part, file); err != nil {
5961
return nil, "", "", err
6062
}
@@ -68,7 +70,7 @@ func (g *GraphAPIHandler) MarshalMultipartRequest(fields map[string]string, file
6870
contentType := writer.FormDataContentType()
6971
bodyBytes := body.Bytes()
7072

71-
// Extract the first and last parts of the body
73+
// Extract the first and last parts of the body for logging
7274
const logSegmentSize = 1024 // 1 KB
7375
bodyLen := len(bodyBytes)
7476
var logBody string
@@ -78,5 +80,10 @@ func (g *GraphAPIHandler) MarshalMultipartRequest(fields map[string]string, file
7880
logBody = string(bodyBytes[:logSegmentSize]) + "..." + string(bodyBytes[bodyLen-logSegmentSize:])
7981
}
8082

83+
// Log the boundary and a partial body for debugging
84+
boundary := writer.Boundary()
85+
log.Debug("Multipart boundary", zap.String("Boundary", boundary))
86+
log.Debug("Multipart request body (partial)", zap.String("Body", logBody))
87+
8188
return bodyBytes, contentType, logBody, nil
8289
}

0 commit comments

Comments
 (0)