From f701abd05a84124feec3b0e5b1c3b7a3717e5762 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Wed, 11 Jun 2025 20:03:23 +0200 Subject: [PATCH 1/2] arduino: improve error code when platform not available for OS --- commands/cmderrors/cmderrors.go | 21 +++++++++++++++++++ commands/service_platform_install.go | 4 ++++ .../arduino/cores/packagemanager/download.go | 4 +++- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/commands/cmderrors/cmderrors.go b/commands/cmderrors/cmderrors.go index bcc33bc3a8f..ce8251aa3bf 100644 --- a/commands/cmderrors/cmderrors.go +++ b/commands/cmderrors/cmderrors.go @@ -16,6 +16,7 @@ package cmderrors import ( + "errors" "fmt" "strings" @@ -415,6 +416,26 @@ func (e *PlatformNotFoundError) Unwrap() error { return e.Cause } +// PlatformNotAvaliableForOSError is returned when a platform contains a tool not aviable +// for the user OS + ARCH +type PlatformNotAvaliableForOSError struct { + Platform string + Cause error +} + +func (e *PlatformNotAvaliableForOSError) Error() string { + return composeErrorMsg(i18n.Tr("Platform '%s'", e.Platform), errors.New(i18n.Tr("platform is not available for your OS"))) +} + +// GRPCStatus converts the error into a *status.Status +func (e *PlatformNotAvaliableForOSError) GRPCStatus() *status.Status { + return status.New(codes.FailedPrecondition, e.Error()) +} + +func (e *PlatformNotAvaliableForOSError) Unwrap() error { + return e.Cause +} + // PlatformLoadingError is returned when a platform has fatal errors that prevents loading type PlatformLoadingError struct { Cause error diff --git a/commands/service_platform_install.go b/commands/service_platform_install.go index 9e268339bc8..51aff7aea61 100644 --- a/commands/service_platform_install.go +++ b/commands/service_platform_install.go @@ -17,6 +17,7 @@ package commands import ( "context" + "errors" "fmt" "github.com/arduino/arduino-cli/commands/cmderrors" @@ -79,6 +80,9 @@ func (s *arduinoCoreServerImpl) PlatformInstall(req *rpc.PlatformInstallRequest, } platformRelease, tools, err := pme.FindPlatformReleaseDependencies(ref) if err != nil { + if errors.Is(err, packagemanager.ErrPlatformNotAvailableForOS) { + return &cmderrors.PlatformNotAvaliableForOSError{Platform: ref.String()} + } return &cmderrors.PlatformNotFoundError{Platform: ref.String(), Cause: err} } diff --git a/internal/arduino/cores/packagemanager/download.go b/internal/arduino/cores/packagemanager/download.go index 05cc0f70047..d80cd5e9185 100644 --- a/internal/arduino/cores/packagemanager/download.go +++ b/internal/arduino/cores/packagemanager/download.go @@ -26,6 +26,8 @@ import ( semver "go.bug.st/relaxed-semver" ) +var ErrPlatformNotAvailableForOS = errors.New("platform is not available for your OS") + // PlatformReference represents a tuple to identify a Platform type PlatformReference struct { Package string // The package where this Platform belongs to. @@ -89,7 +91,7 @@ func (pme *Explorer) FindPlatformReleaseDependencies(item *PlatformReference) (* } else { release = platform.GetLatestCompatibleRelease() if release == nil { - return nil, nil, errors.New(i18n.Tr("platform is not available for your OS")) + return nil, nil, ErrPlatformNotAvailableForOS } } From 40ea05e0417ecc701db2901baa89e1d967650f1d Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Thu, 12 Jun 2025 09:48:06 +0200 Subject: [PATCH 2/2] fix typo --- commands/cmderrors/cmderrors.go | 10 +++++----- commands/service_platform_install.go | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/commands/cmderrors/cmderrors.go b/commands/cmderrors/cmderrors.go index ce8251aa3bf..38eef33bcc1 100644 --- a/commands/cmderrors/cmderrors.go +++ b/commands/cmderrors/cmderrors.go @@ -416,23 +416,23 @@ func (e *PlatformNotFoundError) Unwrap() error { return e.Cause } -// PlatformNotAvaliableForOSError is returned when a platform contains a tool not aviable +// PlatformNotAvailableForOSError is returned when a platform contains a tool not available // for the user OS + ARCH -type PlatformNotAvaliableForOSError struct { +type PlatformNotAvailableForOSError struct { Platform string Cause error } -func (e *PlatformNotAvaliableForOSError) Error() string { +func (e *PlatformNotAvailableForOSError) Error() string { return composeErrorMsg(i18n.Tr("Platform '%s'", e.Platform), errors.New(i18n.Tr("platform is not available for your OS"))) } // GRPCStatus converts the error into a *status.Status -func (e *PlatformNotAvaliableForOSError) GRPCStatus() *status.Status { +func (e *PlatformNotAvailableForOSError) GRPCStatus() *status.Status { return status.New(codes.FailedPrecondition, e.Error()) } -func (e *PlatformNotAvaliableForOSError) Unwrap() error { +func (e *PlatformNotAvailableForOSError) Unwrap() error { return e.Cause } diff --git a/commands/service_platform_install.go b/commands/service_platform_install.go index 51aff7aea61..bd4ea00ddb7 100644 --- a/commands/service_platform_install.go +++ b/commands/service_platform_install.go @@ -81,7 +81,7 @@ func (s *arduinoCoreServerImpl) PlatformInstall(req *rpc.PlatformInstallRequest, platformRelease, tools, err := pme.FindPlatformReleaseDependencies(ref) if err != nil { if errors.Is(err, packagemanager.ErrPlatformNotAvailableForOS) { - return &cmderrors.PlatformNotAvaliableForOSError{Platform: ref.String()} + return &cmderrors.PlatformNotAvailableForOSError{Platform: ref.String()} } return &cmderrors.PlatformNotFoundError{Platform: ref.String(), Cause: err} }