Skip to content

Commit

Permalink
chore: return error when override fails
Browse files Browse the repository at this point in the history
  • Loading branch information
Benyamin-Tehrani committed Oct 7, 2024
1 parent 3f2e93e commit 634c806
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ func ParseRawConfig(rawCfg *RawConfig) (*Config, error) {
// apply overrides
err := ApplyOverride(rawCfg, rawCfg.Override)
if err != nil {
log.Errorln("Error when applying overrides: %v", err)
return nil, err
}

general, err := parseGeneral(rawCfg)
Expand Down
10 changes: 7 additions & 3 deletions config/override.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package config

import (
"errors"
"fmt"
"github.com/metacubex/mihomo/log"
"gopkg.in/yaml.v3"
"os"
Expand Down Expand Up @@ -55,7 +57,7 @@ func ApplyOverride(rawCfg *RawConfig, overrides []RawOverride) error {
overrideContent, err := yaml.Marshal(override.Content)
if err != nil {
log.Errorln("Error when applying override #%v: %v", id, err)
continue
return err
}

// unmarshal override content into rawConfig, with custom list merge strategy
Expand All @@ -69,11 +71,13 @@ func ApplyOverride(rawCfg *RawConfig, overrides []RawOverride) error {
case Override, Default:
err = yaml.Unmarshal(overrideContent, rawCfg)
default:
log.Errorln("Bad list strategy in override #%v: %v", id, override.ListStrategy)
err = errors.New(fmt.Sprintf("Bad list strategy in override #%v: %v", id, override.ListStrategy))
log.Errorln(err.Error())
return err
}
if err != nil {
log.Errorln("Error when applying override #%v: %v", id, err)
continue
return err
}
}
return nil
Expand Down
23 changes: 23 additions & 0 deletions config/override_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"os/user"
"runtime"
"strings"
"testing"
)

Expand Down Expand Up @@ -245,4 +246,26 @@ override:
assert.Contains(t, cfg.Providers, "provider3")
assert.Equal(t, "https://www.google.com", cfg.Providers["provider3"].HealthCheckURL())
})

t.Run("bad_override", func(t *testing.T) {
config_file := `
mixed-port: 7890
ipv6: true
log-level: debug
allow-lan: false
unified-delay: false
tcp-concurrent: true
external-controller: 127.0.0.1:9090
default-nameserver:
- "223.5.5.5"
override:
- list-strategy: 12wlfiu3o
content:
external-controller: 0.0.0.0:9090
allow-lan: true`
rawCfg, err := UnmarshalRawConfig([]byte(config_file))
assert.NoError(t, err)
_, err = ParseRawConfig(rawCfg)
assert.True(t, strings.HasPrefix(err.Error(), "Bad list strategy in override #0:"))
})
}

0 comments on commit 634c806

Please sign in to comment.