Skip to content

Commit ab99f73

Browse files
committed
fix: return AlreadyExists when archive exists
Signed-off-by: dankeboy36 <[email protected]>
1 parent f102aad commit ab99f73

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

commands/cmderrors/cmderrors.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,25 @@ func (e *NotFoundError) GRPCStatus() *status.Status {
755755
return status.New(codes.NotFound, e.Error())
756756
}
757757

758+
// AlreadyExists is returned when creating a resource failed because one already exists
759+
type AlreadyExists struct {
760+
Message string
761+
Cause error
762+
}
763+
764+
func (e *AlreadyExists) Error() string {
765+
return composeErrorMsg(e.Message, e.Cause)
766+
}
767+
768+
func (e *AlreadyExists) Unwrap() error {
769+
return e.Cause
770+
}
771+
772+
// GRPCStatus converts the error into a *status.Status
773+
func (e *AlreadyExists) GRPCStatus() *status.Status {
774+
return status.New(codes.AlreadyExists, e.Error())
775+
}
776+
758777
// PermissionDeniedError is returned when a resource cannot be accessed or modified
759778
type PermissionDeniedError struct {
760779
Message string

commands/service_sketch_archive.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (s *arduinoCoreServerImpl) ArchiveSketch(ctx context.Context, req *rpc.Arch
6666

6767
if !req.GetOverwrite() {
6868
if archivePath.Exist() {
69-
return nil, &cmderrors.InvalidArgumentError{Message: i18n.Tr("Archive already exists")}
69+
return nil, &cmderrors.AlreadyExists{Message: i18n.Tr("Archive already exists")}
7070
}
7171
}
7272

internal/integrationtest/arduino-cli.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,3 +741,17 @@ func (inst *ArduinoCLIInstance) NewSketch(ctx context.Context, sketchName, sketc
741741
logCallf(">>> NewSketch(%+v)\n", req)
742742
return inst.cli.daemonClient.NewSketch(ctx, req)
743743
}
744+
745+
// ArchiveSketch calls the "ArchiveSketch" gRPC method.
746+
func (cli *ArduinoCLI) ArchiveSketch(ctx context.Context, sketchPath, archivePath string, includeBuildDir, overwrite bool) (*commands.ArchiveSketchResponse, error) {
747+
req := &commands.ArchiveSketchRequest{
748+
SketchPath: sketchPath,
749+
ArchivePath: archivePath,
750+
IncludeBuildDir: includeBuildDir,
751+
Overwrite: overwrite,
752+
}
753+
logCallf(">>> ArchiveSketch(%+v)\n", req)
754+
resp, err := cli.daemonClient.ArchiveSketch(ctx, req)
755+
logCallf("err=%v\n", err)
756+
return resp, err
757+
}

internal/integrationtest/daemon/daemon_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,28 @@ func TestDaemonCreateSketch(t *testing.T) {
663663
require.NoError(t, err)
664664
}
665665

666+
func TestDaemonArchiveSketchAlreadyExists(t *testing.T) {
667+
env, cli := integrationtest.CreateEnvForDaemon(t)
668+
defer env.CleanUp()
669+
670+
sketchDir := cli.CopySketch("sketch_simple")
671+
archivePath := cli.WorkingDir().Join("ArchiveSketchAlreadyExists.zip")
672+
if archivePath.Exist() {
673+
require.NoError(t, archivePath.Remove())
674+
}
675+
t.Cleanup(func() { _ = archivePath.Remove() })
676+
677+
_, err := cli.ArchiveSketch(context.Background(), sketchDir.String(), archivePath.String(), false, false)
678+
require.NoError(t, err)
679+
680+
_, err = cli.ArchiveSketch(context.Background(), sketchDir.String(), archivePath.String(), false, false)
681+
require.Error(t, err)
682+
st, ok := status.FromError(err)
683+
require.True(t, ok)
684+
require.Equal(t, codes.AlreadyExists, st.Code())
685+
require.Contains(t, st.Message(), "Archive already exists")
686+
}
687+
666688
func analyzeUpdateIndexClient(t *testing.T, cl commands.ArduinoCoreService_UpdateIndexClient) (map[string]*commands.DownloadProgressEnd, error) {
667689
analyzer := NewDownloadProgressAnalyzer(t)
668690
for {

0 commit comments

Comments
 (0)