Skip to content

Commit c0d8d27

Browse files
dankeboy36cmaglie
authored andcommitted
fix: return AlreadyExists when archive exists
Signed-off-by: dankeboy36 <dankeboy36@gmail.com>
1 parent 350b0ce commit c0d8d27

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
@@ -772,6 +772,25 @@ func (e *NotFoundError) GRPCStatus() *status.Status {
772772
return status.New(codes.NotFound, e.Error())
773773
}
774774

775+
// AlreadyExists is returned when creating a resource failed because one already exists
776+
type AlreadyExists struct {
777+
Message string
778+
Cause error
779+
}
780+
781+
func (e *AlreadyExists) Error() string {
782+
return composeErrorMsg(e.Message, e.Cause)
783+
}
784+
785+
func (e *AlreadyExists) Unwrap() error {
786+
return e.Cause
787+
}
788+
789+
// GRPCStatus converts the error into a *status.Status
790+
func (e *AlreadyExists) GRPCStatus() *status.Status {
791+
return status.New(codes.AlreadyExists, e.Error())
792+
}
793+
775794
// PermissionDeniedError is returned when a resource cannot be accessed or modified
776795
type PermissionDeniedError struct {
777796
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
@@ -807,3 +807,17 @@ func (inst *ArduinoCLIInstance) ProfileLibRemove(
807807
resp, err := inst.cli.daemonClient.ProfileLibRemove(ctx, req)
808808
return resp, err
809809
}
810+
811+
// ArchiveSketch calls the "ArchiveSketch" gRPC method.
812+
func (cli *ArduinoCLI) ArchiveSketch(ctx context.Context, sketchPath, archivePath string, includeBuildDir, overwrite bool) (*commands.ArchiveSketchResponse, error) {
813+
req := &commands.ArchiveSketchRequest{
814+
SketchPath: sketchPath,
815+
ArchivePath: archivePath,
816+
IncludeBuildDir: includeBuildDir,
817+
Overwrite: overwrite,
818+
}
819+
logCallf(">>> ArchiveSketch(%+v)\n", req)
820+
resp, err := cli.daemonClient.ArchiveSketch(ctx, req)
821+
logCallf("err=%v\n", err)
822+
return resp, err
823+
}

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)