Skip to content

Commit 01df93b

Browse files
authored
Report conflicted OTA requests (#151)
* Print conflicted OTA in report * Refactored print error
1 parent 272e8c1 commit 01df93b

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

Diff for: command/ota/upload.go

+19-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package ota
1919

2020
import (
2121
"context"
22+
"errors"
2223
"fmt"
2324
"os"
2425
"path/filepath"
@@ -101,17 +102,33 @@ func Upload(ctx context.Context, params *UploadParams, cred *config.Credentials)
101102
expiration = otaDeferredExpirationMins
102103
}
103104

105+
var conflictedOta *otaapi.Ota
104106
err = iotClient.DeviceOTA(ctx, params.DeviceID, file, expiration)
105107
if err != nil {
106-
return err
108+
if errors.Is(err, iot.ErrOtaAlreadyInProgress) {
109+
conflictedOta = &otaapi.Ota{
110+
DeviceID: params.DeviceID,
111+
Status: "Skipped",
112+
ErrorReason: "OTA already in progress",
113+
}
114+
} else {
115+
return err
116+
}
107117
}
108118
// Try to get ota-id from API
109119
otaID, err := otapi.GetOtaLastStatusByDeviceID(params.DeviceID)
110120
if err != nil {
111121
return err
112122
}
113123
if otaID != nil && len(otaID.Ota) > 0 {
114-
feedback.PrintResult(otaID.Ota[0])
124+
if conflictedOta != nil {
125+
toPrint := otaapi.OtaStatusList{
126+
Ota: []otaapi.Ota{*conflictedOta, otaID.Ota[0]},
127+
}
128+
feedback.PrintResult(toPrint)
129+
} else {
130+
feedback.PrintResult(otaID.Ota[0])
131+
}
115132
}
116133

117134
return nil

Diff for: internal/iot/client.go

+8-3
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("ota 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)