-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve error messages for unsupported features
Error messages for supported features were intended to be user-friendly, but they are still too abstract to easily understand. They refer to the concept of "features", which is an internal Juju construct and as such, should not be mentioned to users. This commit attempts to improve the error messaging to make it more straightforward.
- Loading branch information
1 parent
44225ac
commit 9b7c7cc
Showing
10 changed files
with
156 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2024 Canonical Ltd. | ||
// Licensed under the AGPLv3, see LICENCE file for details. | ||
|
||
package assumes | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/juju/version/v2" | ||
gc "gopkg.in/check.v1" | ||
) | ||
|
||
type errorSuite struct{} | ||
|
||
var _ = gc.Suite(&errorSuite{}) | ||
|
||
var errorTests = []struct { | ||
description string | ||
featureSet FeatureSet | ||
assumes string | ||
expectedErr string | ||
}{{ | ||
description: "Unsupported Juju version", | ||
featureSet: FeatureSet{features: map[string]Feature{ | ||
"juju": JujuFeature(version.MustParse("2.9.42")), | ||
}}, | ||
assumes: "assumes:\n- juju >= 3.1", | ||
expectedErr: "(?s).*charm requires Juju version >= 3.1.0.*", | ||
}, { | ||
description: "Deploying k8s charm on machine cloud", | ||
featureSet: FeatureSet{}, | ||
assumes: "assumes:\n- k8s-api", | ||
expectedErr: "(?s).*charm must be deployed on a Kubernetes cloud.*", | ||
}, { | ||
description: "k8s version too low", | ||
featureSet: FeatureSet{features: map[string]Feature{ | ||
"k8s-api": K8sAPIFeature(version.MustParse("1.25.0")), | ||
}}, | ||
assumes: "assumes:\n- k8s-api >= 1.30", | ||
expectedErr: "(?s).*charm requires Kubernetes version >= 1.30.*", | ||
}} | ||
|
||
func (*errorSuite) TestErrorMessages(c *gc.C) { | ||
for _, test := range errorTests { | ||
fmt.Println(test.description) | ||
assumesTree := mustParseAssumesExpr(c, test.assumes) | ||
err := test.featureSet.Satisfies(assumesTree) | ||
c.Check(err, gc.ErrorMatches, test.expectedErr) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright 2024 Canonical Ltd. | ||
// Licensed under the AGPLv3, see LICENCE file for details. | ||
|
||
package assumes | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/juju/version/v2" | ||
) | ||
|
||
var ( | ||
// A set of user-friendly descriptions for potentially supported | ||
// features that are known to the controller. This allows us to | ||
// generate better error messages when an "assumes" expression requests a | ||
// feature that is not included in the feature set supported by the | ||
// current model. | ||
UserFriendlyFeatureDescriptions = map[string]string{ | ||
"juju": "the version of Juju used by the model", | ||
"k8s-api": "the Kubernetes API lets charms query and manipulate the state of API objects in a Kubernetes cluster", | ||
} | ||
) | ||
|
||
// featureMissingErrs is a list of user-friendly error messages to return when | ||
// a given feature is expected by a charm, but not present in the model. | ||
var featureMissingErrs = map[string]string{ | ||
"juju": "charm requires Juju", // this should never happen | ||
"k8s-api": "charm must be deployed on a Kubernetes cloud", | ||
} | ||
|
||
// featureMissingErr returns a user-friendly error message to return when a | ||
// given feature is expected by a charm, but not present in the model. | ||
func featureMissingErr(featureName string) string { | ||
if errStr, ok := featureMissingErrs[featureName]; ok { | ||
return errStr | ||
} | ||
// We don't have a specific error message defined, so return a default. | ||
return fmt.Sprintf("charm requires feature %q but model does not support it", featureName) | ||
} | ||
|
||
// featureVersionMismatchErrs is a list of functions which generate | ||
// user-friendly error messages when a given feature is present in the model, | ||
// but the version is lower than required by the charm. | ||
var featureVersionMismatchErrs = map[string]func(constraint, requiredVersion, actualVersion string) string{ | ||
"juju": func(c, rv, av string) string { | ||
return fmt.Sprintf("charm requires Juju version %s %s, model has version %s", c, rv, av) | ||
}, | ||
"k8s-api": func(c, rv, av string) string { | ||
return fmt.Sprintf("charm requires Kubernetes version %s %s, model is running on version %s", c, rv, av) | ||
}, | ||
} | ||
|
||
// featureVersionMismatchErr returns a user-friendly error message to return when | ||
// a given feature is present in the model, but the version is lower than | ||
// required by the charm. | ||
func featureVersionMismatchErr(featureName, constraint, requiredVersion, actualVersion string) string { | ||
if f, ok := featureVersionMismatchErrs[featureName]; ok { | ||
return f(constraint, requiredVersion, actualVersion) | ||
} | ||
// We don't have a specific error message defined, so return a default. | ||
return fmt.Sprintf("charm requires feature %q (version %s %s) but model currently supports version %s", | ||
featureName, constraint, requiredVersion, actualVersion) | ||
} | ||
|
||
// JujuFeature returns a new Feature representing the Juju API for the given | ||
// version. | ||
func JujuFeature(ver version.Number) Feature { | ||
return Feature{ | ||
Name: "juju", | ||
Description: UserFriendlyFeatureDescriptions["juju"], | ||
Version: &ver, | ||
} | ||
} | ||
|
||
// K8sAPIFeature returns a new Feature representing the Kubernetes API for the | ||
// given version. | ||
func K8sAPIFeature(ver version.Number) Feature { | ||
return Feature{ | ||
Name: "k8s-api", | ||
Description: UserFriendlyFeatureDescriptions["k8s-api"], | ||
Version: &ver, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters