fix: gRPC ArchiveSketch now returns AlreadyExists code when archi… · arduino/arduino-cli@8c79888 (original) (raw)
4 files changed
lines changed
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -772,6 +772,25 @@ func (e *NotFoundError) GRPCStatus() *status.Status { | ||
| 772 | 772 | return status.New(codes.NotFound, e.Error()) |
| 773 | 773 | } |
| 774 | 774 | |
| 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 | + | |
| 775 | 794 | // PermissionDeniedError is returned when a resource cannot be accessed or modified |
| 776 | 795 | type PermissionDeniedError struct { |
| 777 | 796 | Message string |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -66,7 +66,7 @@ func (s *arduinoCoreServerImpl) ArchiveSketch(ctx context.Context, req *rpc.Arch | ||
| 66 | 66 | |
| 67 | 67 | if !req.GetOverwrite() { |
| 68 | 68 | 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")} | |
| 70 | 70 | } |
| 71 | 71 | } |
| 72 | 72 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -807,3 +807,17 @@ func (inst *ArduinoCLIInstance) ProfileLibRemove( | ||
| 807 | 807 | resp, err := inst.cli.daemonClient.ProfileLibRemove(ctx, req) |
| 808 | 808 | return resp, err |
| 809 | 809 | } |
| 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 | +} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -663,6 +663,28 @@ func TestDaemonCreateSketch(t *testing.T) { | ||
| 663 | 663 | require.NoError(t, err) |
| 664 | 664 | } |
| 665 | 665 | |
| 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 | + | |
| 666 | 688 | func analyzeUpdateIndexClient(t *testing.T, cl commands.ArduinoCoreService_UpdateIndexClient) (map[string]*commands.DownloadProgressEnd, error) { |
| 667 | 689 | analyzer := NewDownloadProgressAnalyzer(t) |
| 668 | 690 | for { |