]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
Improve error messages for PostCSS etc.
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 17 Jul 2023 17:15:48 +0000 (19:15 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 17 Jul 2023 18:42:32 +0000 (20:42 +0200)
Fixes #9730

common/herrors/errors.go
common/herrors/errors_test.go
config/commonConfig.go
resources/resource_transformers/babel/babel.go
resources/resource_transformers/postcss/integration_test.go
resources/resource_transformers/postcss/postcss.go
resources/transform.go

index 4d86423629c5ef299b9ad5a9ad53f174503b533f..598c50b32fd24f22dfac98ee0edfc92cdaafa825 100644 (file)
@@ -59,11 +59,34 @@ func GetGID() uint64 {
        return n
 }
 
+// IsFeatureNotAvailableError returns true if the given error is or contains a FeatureNotAvailableError.
+func IsFeatureNotAvailableError(err error) bool {
+       return errors.Is(err, &FeatureNotAvailableError{})
+}
+
 // ErrFeatureNotAvailable denotes that a feature is unavailable.
 //
 // We will, at least to begin with, make some Hugo features (SCSS with libsass) optional,
 // and this error is used to signal those situations.
-var ErrFeatureNotAvailable = errors.New("this feature is not available in your current Hugo version, see https://goo.gl/YMrWcn for more information")
+var ErrFeatureNotAvailable = &FeatureNotAvailableError{Cause: errors.New("this feature is not available in your current Hugo version, see https://goo.gl/YMrWcn for more information")}
+
+// FeatureNotAvailableError is an error type used to signal that a feature is not available.
+type FeatureNotAvailableError struct {
+       Cause error
+}
+
+func (e *FeatureNotAvailableError) Unwrap() error {
+       return e.Cause
+}
+
+func (e *FeatureNotAvailableError) Error() string {
+       return e.Cause.Error()
+}
+
+func (e *FeatureNotAvailableError) Is(target error) bool {
+       _, ok := target.(*FeatureNotAvailableError)
+       return ok
+}
 
 // Must panics if err != nil.
 func Must(err error) {
index 1e07300284e29f62b06c49516663c999752ee140..223782e23ad04e99e94498d65e9016d9c79a26fa 100644 (file)
@@ -14,6 +14,7 @@
 package herrors
 
 import (
+       "errors"
        "fmt"
        "testing"
 
@@ -34,3 +35,12 @@ func TestIsNotExist(t *testing.T) {
        // os.IsNotExist returns false for wrapped errors.
        c.Assert(IsNotExist(fmt.Errorf("foo: %w", afero.ErrFileNotFound)), qt.Equals, true)
 }
+
+func TestIsFeatureNotAvailableError(t *testing.T) {
+       c := qt.New(t)
+
+       c.Assert(IsFeatureNotAvailableError(ErrFeatureNotAvailable), qt.Equals, true)
+       c.Assert(IsFeatureNotAvailableError(&FeatureNotAvailableError{}), qt.Equals, true)
+       c.Assert(IsFeatureNotAvailableError(errors.New("asdf")), qt.Equals, false)
+
+}
index 2c6497b34887c38fcc8a9560caacfc3d2739f9fc..5cf526708c94d986ec9c2372343ca4bec7c2532c 100644 (file)
@@ -148,7 +148,7 @@ func (b BuildConfig) UseResourceCache(err error) bool {
        }
 
        if b.UseResourceCacheWhen == "fallback" {
-               return err == herrors.ErrFeatureNotAvailable
+               return herrors.IsFeatureNotAvailableError(err)
        }
 
        return true
index 5f8fcb00f7ef10449803844b8e301d1c27b7f9be..2999d73cb0581fd157508576c246d2a4b9b85d65 100644 (file)
@@ -181,7 +181,7 @@ func (t *babelTransformation) Transform(ctx *resources.ResourceTransformationCtx
        if err != nil {
                if hexec.IsNotFound(err) {
                        // This may be on a CI server etc. Will fall back to pre-built assets.
-                       return herrors.ErrFeatureNotAvailable
+                       return &herrors.FeatureNotAvailableError{Cause: err}
                }
                return err
        }
@@ -200,7 +200,7 @@ func (t *babelTransformation) Transform(ctx *resources.ResourceTransformationCtx
        err = cmd.Run()
        if err != nil {
                if hexec.IsNotFound(err) {
-                       return herrors.ErrFeatureNotAvailable
+                       return &herrors.FeatureNotAvailableError{Cause: err}
                }
                return fmt.Errorf(errBuf.String()+": %w", err)
        }
index c920fe17d461761cc62a0460114839461e549e6b..74aaa2661c72cf52b747c3bcc7b6bc8c0a676d03 100644 (file)
@@ -168,6 +168,25 @@ func TestTransformPostCSSError(t *testing.T) {
 
 }
 
+func TestTransformPostCSSNotInstalledError(t *testing.T) {
+       if !htesting.IsCI() {
+               t.Skip("Skip long running test when running locally")
+       }
+
+       c := qt.New(t)
+
+       s, err := hugolib.NewIntegrationTestBuilder(
+               hugolib.IntegrationTestConfig{
+                       T:           c,
+                       NeedsOsFS:   true,
+                       TxtarString: postCSSIntegrationTestFiles,
+               }).BuildE()
+
+       s.AssertIsFileError(err)
+       c.Assert(err.Error(), qt.Contains, `binary with name "npx" not found`)
+
+}
+
 // #9895
 func TestTransformPostCSSImportError(t *testing.T) {
        if !htesting.IsCI() {
index 0836072468ec9d769c3a8f92aa39fd8b17e61245..a65fa3783a00567e3396849d01f3f0b8c7e65dfe 100644 (file)
@@ -205,7 +205,7 @@ func (t *postcssTransformation) Transform(ctx *resources.ResourceTransformationC
        if err != nil {
                if hexec.IsNotFound(err) {
                        // This may be on a CI server etc. Will fall back to pre-built assets.
-                       return herrors.ErrFeatureNotAvailable
+                       return &herrors.FeatureNotAvailableError{Cause: err}
                }
                return err
        }
@@ -240,7 +240,9 @@ func (t *postcssTransformation) Transform(ctx *resources.ResourceTransformationC
        err = cmd.Run()
        if err != nil {
                if hexec.IsNotFound(err) {
-                       return herrors.ErrFeatureNotAvailable
+                       return &herrors.FeatureNotAvailableError{
+                               Cause: err,
+                       }
                }
                return imp.toFileError(errBuf.String())
        }
index 9e5b576257f3dd600320aed5bc6694e13e66493f..eb7d8fb0b3c7fb696a7e04a03f365cf01c7b62a0 100644 (file)
@@ -449,7 +449,7 @@ func (r *resourceAdapter) transform(publish, setContent bool) error {
                newErr := func(err error) error {
                        msg := fmt.Sprintf("%s: failed to transform %q (%s)", strings.ToUpper(tr.Key().Name), tctx.InPath, tctx.InMediaType.Type)
 
-                       if err == herrors.ErrFeatureNotAvailable {
+                       if herrors.IsFeatureNotAvailableError(err) {
                                var errMsg string
                                if tr.Key().Name == "postcss" {
                                        // This transformation is not available in this