Skip to content

Commit

Permalink
Chore: make error messages consistent across formats
Browse files Browse the repository at this point in the history
  • Loading branch information
Loyalsoldier committed Aug 31, 2024
1 parent deff06a commit 1e4cb74
Show file tree
Hide file tree
Showing 15 changed files with 39 additions and 39 deletions.
4 changes: 2 additions & 2 deletions plugin/maxmind/asn_csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (g *geoLite2ASNCSV) Input(container lib.Container) (lib.Container, error) {
}

if len(entries) == 0 {
return nil, fmt.Errorf("❌ [type %s | action %s] no entry is generated", typeASNCSV, g.Action)
return nil, fmt.Errorf("❌ [type %s | action %s] no entry is generated", g.Type, g.Action)
}

var ignoreIPType lib.IgnoreIPOption
Expand Down Expand Up @@ -190,7 +190,7 @@ func (g *geoLite2ASNCSV) process(file string, entries map[string]*lib.Entry) err
}

if len(record) < 2 {
return fmt.Errorf("❌ [type %s | action %s] invalid record: %v", typeASNCSV, g.Action, record)
return fmt.Errorf("❌ [type %s | action %s] invalid record: %v", g.Type, g.Action, record)
}

if listArr, found := g.Want[strings.TrimSpace(record[1])]; found {
Expand Down
10 changes: 5 additions & 5 deletions plugin/maxmind/country_csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (g *geoLite2CountryCSV) Input(container lib.Container) (lib.Container, erro
}

if len(entries) == 0 {
return nil, fmt.Errorf("❌ [type %s | action %s] no entry is generated", typeCountryCSV, g.Action)
return nil, fmt.Errorf("❌ [type %s | action %s] no entry is generated", g.Type, g.Action)
}

var ignoreIPType lib.IgnoreIPOption
Expand Down Expand Up @@ -176,7 +176,7 @@ func (g *geoLite2CountryCSV) getCountryCode() (map[string]string, error) {
ccMap := make(map[string]string)
for _, line := range lines[1:] {
if len(line) < 5 {
return nil, fmt.Errorf("❌ [type %s | action %s] invalid record: %v", typeCountryCSV, g.Action, line)
return nil, fmt.Errorf("❌ [type %s | action %s] invalid record: %v", g.Type, g.Action, line)
}

id := strings.TrimSpace(line[0])
Expand All @@ -193,15 +193,15 @@ func (g *geoLite2CountryCSV) getCountryCode() (map[string]string, error) {
}

if len(ccMap) == 0 {
return nil, fmt.Errorf("❌ [type %s | action %s] invalid country code data", typeCountryCSV, g.Action)
return nil, fmt.Errorf("❌ [type %s | action %s] invalid country code data", g.Type, g.Action)
}

return ccMap, nil
}

func (g *geoLite2CountryCSV) process(file string, ccMap map[string]string, entries map[string]*lib.Entry) error {
if len(ccMap) == 0 {
return fmt.Errorf("❌ [type %s | action %s] invalid country code data", typeCountryCSV, g.Action)
return fmt.Errorf("❌ [type %s | action %s] invalid country code data", g.Type, g.Action)
}
if entries == nil {
entries = make(map[string]*lib.Entry, len(ccMap))
Expand Down Expand Up @@ -234,7 +234,7 @@ func (g *geoLite2CountryCSV) process(file string, ccMap map[string]string, entri
}

if len(record) < 4 {
return fmt.Errorf("❌ [type %s | action %s] invalid record: %v", typeCountryCSV, g.Action, record)
return fmt.Errorf("❌ [type %s | action %s] invalid record: %v", g.Type, g.Action, record)
}

ccID := ""
Expand Down
2 changes: 1 addition & 1 deletion plugin/maxmind/mmdb_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (m *maxmindMMDBIn) Input(container lib.Container) (lib.Container, error) {
}

if len(entries) == 0 {
return nil, fmt.Errorf("❌ [type %s | action %s] no entry is generated", typeMaxmindMMDBIn, m.Action)
return nil, fmt.Errorf("❌ [type %s | action %s] no entry is generated", m.Type, m.Action)
}

var ignoreIPType lib.IgnoreIPOption
Expand Down
2 changes: 1 addition & 1 deletion plugin/maxmind/mmdb_out.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (m *mmdbOut) Output(container lib.Container) error {
return err
}
} else {
return fmt.Errorf("type %s | action %s failed to write file", m.Type, m.Action)
return fmt.Errorf("❌ [type %s | action %s] failed to write file", m.Type, m.Action)
}

return nil
Expand Down
12 changes: 6 additions & 6 deletions plugin/mihomo/mrs_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ func newMRSIn(action lib.Action, data json.RawMessage) (lib.InputConverter, erro
}

if tmp.Name == "" && tmp.URI == "" && tmp.InputDir == "" {
return nil, fmt.Errorf("type %s | action %s missing inputdir or name or uri", typeMRSIn, action)
return nil, fmt.Errorf("❌ [type %s | action %s] missing inputDir or name or uri", typeMRSIn, action)
}

if (tmp.Name != "" && tmp.URI == "") || (tmp.Name == "" && tmp.URI != "") {
return nil, fmt.Errorf("type %s | action %s name & uri must be specified together", typeMRSIn, action)
return nil, fmt.Errorf("❌ [type %s | action %s] name & uri must be specified together", typeMRSIn, action)
}

// Filter want list
Expand Down Expand Up @@ -115,7 +115,7 @@ func (m *mrsIn) Input(container lib.Container) (lib.Container, error) {
err = m.walkLocalFile(m.URI, m.Name, entries)
}
default:
return nil, fmt.Errorf("config missing argument inputDir or name or uri")
return nil, fmt.Errorf("❌ [type %s | action %s] config missing argument inputDir or name or uri", m.Type, m.Action)
}

if err != nil {
Expand Down Expand Up @@ -181,7 +181,7 @@ func (m *mrsIn) walkLocalFile(path, name string, entries map[string]*lib.Entry)

// check filename
if !regexp.MustCompile(`^[a-zA-Z0-9_.\-]+$`).MatchString(entryName) {
return fmt.Errorf("filename %s cannot be entry name, please remove special characters in it", entryName)
return fmt.Errorf("❌ [type %s | action %s] filename %s cannot be entry name, please remove special characters in it", m.Type, m.Action, entryName)
}

// remove file extension but not hidden files of which filename starts with "."
Expand All @@ -193,7 +193,7 @@ func (m *mrsIn) walkLocalFile(path, name string, entries map[string]*lib.Entry)

entryName = strings.ToUpper(entryName)
if _, found := entries[entryName]; found {
return fmt.Errorf("found duplicated list %s", entryName)
return fmt.Errorf("❌ [type %s | action %s] found duplicated list %s", m.Type, m.Action, entryName)
}

file, err := os.Open(path)
Expand All @@ -217,7 +217,7 @@ func (m *mrsIn) walkRemoteFile(url, name string, entries map[string]*lib.Entry)
defer resp.Body.Close()

if resp.StatusCode != 200 {
return fmt.Errorf("failed to get remote file %s, http status code %d", url, resp.StatusCode)
return fmt.Errorf("❌ [type %s | action %s] failed to get remote file %s, http status code %d", m.Type, m.Action, url, resp.StatusCode)
}

if err := m.generateEntries(name, resp.Body, entries); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion plugin/mihomo/mrs_out.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (m *mrsOut) generate(entry *lib.Entry) error {
}

if len(ipRanges) == 0 {
return fmt.Errorf("entry %s has no CIDR", entry.GetName())
return fmt.Errorf("❌ [type %s | action %s] entry %s has no CIDR", m.Type, m.Action, entry.GetName())
}

filename := strings.ToLower(entry.GetName()) + ".mrs"
Expand Down
2 changes: 1 addition & 1 deletion plugin/plaintext/common_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func (t *textIn) scanFileForJSONIn(reader io.Reader, entry *lib.Entry) error {
}

if !gjson.ValidBytes(data) {
return fmt.Errorf("invalid JSON data")
return fmt.Errorf("❌ [type %s | action %s] invalid JSON data", t.Type, t.Action)
}

// JSON Path syntax:
Expand Down
12 changes: 6 additions & 6 deletions plugin/plaintext/text_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func newTextIn(iType string, action lib.Action, data json.RawMessage) (lib.Input
}

if iType == typeJSONIn && len(tmp.JSONPath) == 0 {
return nil, fmt.Errorf("❌ [type %s | action %s] missing jsonPath", typeJSONIn, action)
return nil, fmt.Errorf("❌ [type %s | action %s] missing jsonPath", iType, action)
}

if tmp.InputDir == "" {
Expand Down Expand Up @@ -131,7 +131,7 @@ func (t *textIn) Input(container lib.Container) (lib.Container, error) {
err = t.appendIPOrCIDR(t.IPOrCIDR, t.Name, entries)

default:
return nil, fmt.Errorf("config missing argument inputDir or name or uri")
return nil, fmt.Errorf("❌ [type %s | action %s] config missing argument inputDir or name or uri or ipOrCIDR", t.Type, t.Action)
}

if err != nil {
Expand All @@ -147,7 +147,7 @@ func (t *textIn) Input(container lib.Container) (lib.Container, error) {
}

if len(entries) == 0 {
return nil, fmt.Errorf("type %s | action %s no entry is generated", t.Type, t.Action)
return nil, fmt.Errorf("❌ [type %s | action %s] no entry is generated", t.Type, t.Action)
}

for _, entry := range entries {
Expand Down Expand Up @@ -197,7 +197,7 @@ func (t *textIn) walkLocalFile(path, name string, entries map[string]*lib.Entry)

// check filename
if !regexp.MustCompile(`^[a-zA-Z0-9_.\-]+$`).MatchString(entryName) {
return fmt.Errorf("filename %s cannot be entry name, please remove special characters in it", entryName)
return fmt.Errorf("❌ [type %s | action %s] filename %s cannot be entry name, please remove special characters in it", t.Type, t.Action, entryName)
}

// remove file extension but not hidden files of which filename starts with "."
Expand All @@ -213,7 +213,7 @@ func (t *textIn) walkLocalFile(path, name string, entries map[string]*lib.Entry)
return nil
}
if _, found := entries[entryName]; found {
return fmt.Errorf("found duplicated list %s", entryName)
return fmt.Errorf("❌ [type %s | action %s] found duplicated list %s", t.Type, t.Action, entryName)
}

entry := lib.NewEntry(entryName)
Expand All @@ -239,7 +239,7 @@ func (t *textIn) walkRemoteFile(url, name string, entries map[string]*lib.Entry)
defer resp.Body.Close()

if resp.StatusCode != 200 {
return fmt.Errorf("failed to get remote file %s, http status code %d", url, resp.StatusCode)
return fmt.Errorf("❌ [type %s | action %s] failed to get remote file %s, http status code %d", t.Type, t.Action, url, resp.StatusCode)
}

name = strings.ToUpper(name)
Expand Down
14 changes: 7 additions & 7 deletions plugin/singbox/srs_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ func newSRSIn(action lib.Action, data json.RawMessage) (lib.InputConverter, erro
}

if tmp.Name == "" && tmp.URI == "" && tmp.InputDir == "" {
return nil, fmt.Errorf("type %s | action %s missing inputdir or name or uri", typeSRSIn, action)
return nil, fmt.Errorf("❌ [type %s | action %s] missing inputdir or name or uri", typeSRSIn, action)
}

if (tmp.Name != "" && tmp.URI == "") || (tmp.Name == "" && tmp.URI != "") {
return nil, fmt.Errorf("type %s | action %s name & uri must be specified together", typeSRSIn, action)
return nil, fmt.Errorf("❌ [type %s | action %s] name & uri must be specified together", typeSRSIn, action)
}

// Filter want list
Expand Down Expand Up @@ -109,15 +109,15 @@ func (s *srsIn) Input(container lib.Container) (lib.Container, error) {
err = s.walkLocalFile(s.URI, s.Name, entries)
}
default:
return nil, fmt.Errorf("config missing argument inputDir or name or uri")
return nil, fmt.Errorf("❌ [type %s | action %s] config missing argument inputDir or name or uri", s.Type, s.Action)
}

if err != nil {
return nil, err
}

if len(entries) == 0 {
return nil, fmt.Errorf("type %s | action %s no entry is generated", s.Type, s.Action)
return nil, fmt.Errorf("❌ [type %s | action %s] no entry is generated", s.Type, s.Action)
}

var ignoreIPType lib.IgnoreIPOption
Expand Down Expand Up @@ -175,7 +175,7 @@ func (s *srsIn) walkLocalFile(path, name string, entries map[string]*lib.Entry)

// check filename
if !regexp.MustCompile(`^[a-zA-Z0-9_.\-]+$`).MatchString(entryName) {
return fmt.Errorf("filename %s cannot be entry name, please remove special characters in it", entryName)
return fmt.Errorf("❌ [type %s | action %s] filename %s cannot be entry name, please remove special characters in it", s.Type, s.Action, entryName)
}

// remove file extension but not hidden files of which filename starts with "."
Expand All @@ -187,7 +187,7 @@ func (s *srsIn) walkLocalFile(path, name string, entries map[string]*lib.Entry)

entryName = strings.ToUpper(entryName)
if _, found := entries[entryName]; found {
return fmt.Errorf("found duplicated list %s", entryName)
return fmt.Errorf("❌ [type %s | action %s] found duplicated list %s", s.Type, s.Action, entryName)
}

file, err := os.Open(path)
Expand All @@ -211,7 +211,7 @@ func (s *srsIn) walkRemoteFile(url, name string, entries map[string]*lib.Entry)
defer resp.Body.Close()

if resp.StatusCode != 200 {
return fmt.Errorf("failed to get remote file %s, http status code %d", url, resp.StatusCode)
return fmt.Errorf("❌ [type %s | action %s] failed to get remote file %s, http status code %d", s.Type, s.Action, url, resp.StatusCode)
}

if err := s.generateEntries(name, resp.Body, entries); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion plugin/singbox/srs_out.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func (s *srsOut) generateRuleSet(entry *lib.Entry) (*option.PlainRuleSet, error)
return &plainRuleSet, nil
}

return nil, fmt.Errorf("entry %s has no CIDR", entry.GetName())
return nil, fmt.Errorf("❌ [type %s | action %s] entry %s has no CIDR", s.Type, s.Action, entry.GetName())
}

func (s *srsOut) writeFile(filename string, ruleset *option.PlainRuleSet) error {
Expand Down
4 changes: 2 additions & 2 deletions plugin/special/cutter.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func newCutter(action lib.Action, data json.RawMessage) (lib.InputConverter, err
}

if action != lib.ActionRemove {
return nil, fmt.Errorf("type %s only supports `remove` action", typeCutter)
return nil, fmt.Errorf("❌ [type %s] only supports `remove` action", typeCutter)
}

// Filter want list
Expand All @@ -47,7 +47,7 @@ func newCutter(action lib.Action, data json.RawMessage) (lib.InputConverter, err
}

if len(wantList) == 0 {
return nil, fmt.Errorf("type %s wantedList must be specified", typeCutter)
return nil, fmt.Errorf("❌ [type %s] wantedList must be specified", typeCutter)
}

return &cutter{
Expand Down
2 changes: 1 addition & 1 deletion plugin/special/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func newLookup(action lib.Action, data json.RawMessage) (lib.OutputConverter, er

tmp.Search = strings.TrimSpace(tmp.Search)
if tmp.Search == "" {
return nil, fmt.Errorf("type %s | action %s: please specify an IP or a CIDR as search target", typeLookup, action)
return nil, fmt.Errorf("❌ [type %s | action %s] please specify an IP or a CIDR as search target", typeLookup, action)
}

return &lookup{
Expand Down
2 changes: 1 addition & 1 deletion plugin/special/stdin.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func newStdin(action lib.Action, data json.RawMessage) (lib.InputConverter, erro
}

if tmp.Name == "" {
return nil, fmt.Errorf("type %s | action %s missing name", typeStdin, action)
return nil, fmt.Errorf("❌ [type %s | action %s] missing name", typeStdin, action)
}

return &stdin{
Expand Down
6 changes: 3 additions & 3 deletions plugin/v2ray/dat_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func newGeoIPDatIn(action lib.Action, data json.RawMessage) (lib.InputConverter,
}

if tmp.URI == "" {
return nil, fmt.Errorf("[type %s | action %s] uri must be specified in config", typeGeoIPdatIn, action)
return nil, fmt.Errorf("[type %s | action %s] uri must be specified in config", typeGeoIPdatIn, action)
}

// Filter want list
Expand Down Expand Up @@ -100,7 +100,7 @@ func (g *geoIPDatIn) Input(container lib.Container) (lib.Container, error) {
}

if len(entries) == 0 {
return nil, fmt.Errorf("❌ [type %s | action %s] no entry is generated", typeGeoIPdatIn, g.Action)
return nil, fmt.Errorf("❌ [type %s | action %s] no entry is generated", g.Type, g.Action)
}

var ignoreIPType lib.IgnoreIPOption
Expand Down Expand Up @@ -151,7 +151,7 @@ func (g *geoIPDatIn) walkRemoteFile(url string, entries map[string]*lib.Entry) e
defer resp.Body.Close()

if resp.StatusCode != 200 {
return fmt.Errorf("failed to get remote file %s, http status code %d", url, resp.StatusCode)
return fmt.Errorf("❌ [type %s | action %s] failed to get remote file %s, http status code %d", g.Type, g.Action, url, resp.StatusCode)
}

if err := g.generateEntries(resp.Body, entries); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion plugin/v2ray/dat_out.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func (g *geoIPDatOut) generateGeoIP(entry *lib.Entry) (*router.GeoIP, error) {
}, nil
}

return nil, fmt.Errorf("entry %s has no CIDR", entry.GetName())
return nil, fmt.Errorf("❌ [type %s | action %s] entry %s has no CIDR", g.Type, g.Action, entry.GetName())
}

// Sort by country code to make reproducible builds
Expand Down

0 comments on commit 1e4cb74

Please sign in to comment.