Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 496ddcf

Browse files
committedMay 2, 2024·
Print conflicted OTA in report
1 parent 272e8c1 commit 496ddcf

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed
 

‎command/ota/upload.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,16 @@ func Upload(ctx context.Context, params *UploadParams, cred *config.Credentials)
101101
expiration = otaDeferredExpirationMins
102102
}
103103

104+
var conflictedOta *otaapi.Ota
104105
err = iotClient.DeviceOTA(ctx, params.DeviceID, file, expiration)
105106
if err != nil {
107+
if err == iot.ErrOtaAlreadyInProgress {
108+
conflictedOta = &otaapi.Ota{
109+
DeviceID: params.DeviceID,
110+
Status: "Failed",
111+
ErrorReason: "OTA already in progress",
112+
}
113+
}
106114
return err
107115
}
108116
// Try to get ota-id from API
@@ -111,7 +119,14 @@ func Upload(ctx context.Context, params *UploadParams, cred *config.Credentials)
111119
return err
112120
}
113121
if otaID != nil && len(otaID.Ota) > 0 {
114-
feedback.PrintResult(otaID.Ota[0])
122+
if conflictedOta != nil {
123+
toPrint := otaapi.OtaStatusList{
124+
Ota: []otaapi.Ota{*conflictedOta, otaID.Ota[0]},
125+
}
126+
feedback.PrintResult(toPrint)
127+
} else {
128+
feedback.PrintResult(otaID.Ota[0])
129+
}
115130
}
116131

117132
return nil

‎internal/iot/client.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import (
2828
"golang.org/x/oauth2"
2929
)
3030

31+
var ErrOtaAlreadyInProgress = fmt.Errorf("already in progress")
32+
3133
// Client can perform actions on Arduino IoT Cloud.
3234
type Client struct {
3335
api *iotclient.APIClient
@@ -196,9 +198,12 @@ func (cl *Client) DeviceOTA(ctx context.Context, id string, file *os.File, expir
196198
Async: optional.NewBool(true),
197199
}
198200
resp, err := cl.api.DevicesV2OtaApi.DevicesV2OtaUpload(ctx, id, file, opt)
199-
if err != nil && resp.StatusCode != 409 { // 409 (Conflict) is the status code for an already existing OTA for the same SHA/device, so ignoring it.
200-
err = fmt.Errorf("uploading device ota: %w", errorDetail(err))
201-
return err
201+
if err != nil {
202+
// 409 (Conflict) is the status code for an already existing OTA in progress for the same device. Handling it in a different way.
203+
if resp.StatusCode == 409 {
204+
return ErrOtaAlreadyInProgress
205+
}
206+
return fmt.Errorf("uploading device ota: %w", errorDetail(err))
202207
}
203208
return nil
204209
}

0 commit comments

Comments
 (0)
Please sign in to comment.