Skip to content

Commit 1479845

Browse files
committed
safes err handling
1 parent 4920137 commit 1479845

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

main.go

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"errors"
45
"flag"
56
"log"
67
"os"
@@ -91,7 +92,13 @@ func templCSSSort(flags Flags) {
9192
originalContent := string(content)
9293
assert(originalContent != "", "File is empty")
9394

94-
newContent := processContent(originalContent)
95+
newContent, err := processContent(originalContent)
96+
if err != nil {
97+
if err.Error() == "no_classes_found" {
98+
continue
99+
}
100+
log.Fatal(err)
101+
}
95102

96103
// write the modified content back to the file
97104
err = os.WriteFile(file, []byte(newContent), 0644)
@@ -103,29 +110,39 @@ func templCSSSort(flags Flags) {
103110
log.Println("Done in", time.Since(start))
104111
}
105112

106-
func processContent(content string) string {
113+
func processContent(content string) (string, error) {
107114
// find all classes in content
108115
re := regexp.MustCompile(`class="([^"]+)"`)
109116
matches := re.FindAllStringSubmatch(content, -1)
110-
assert(len(matches) > 0, "No classes found")
117+
if len(matches) == 0 {
118+
return "", errors.New("no_classes_found")
119+
}
111120

112121
for _, match := range matches {
113122
classList := match[1]
114-
assert(classList != "", "Class list is empty")
123+
if classList == "" {
124+
continue
125+
}
115126

116127
// trim in place
117128
classList = strings.TrimSpace(classList)
118-
assert(classList != "", "Class list is empty")
129+
if classList == "" {
130+
continue
131+
}
119132

120133
// any whitespace bigger then 1 char, reduce to 1 char
121134
for strings.Contains(classList, " ") {
122135
classList = strings.ReplaceAll(classList, " ", " ")
123136
}
124-
assert(classList != "", "Class list is empty")
137+
if classList == "" {
138+
continue
139+
}
125140

126141
// split
127142
classes := strings.Split(classList, " ")
128-
assert(len(classes) > 0, "No classes found")
143+
if len(classes) == 0 {
144+
continue
145+
}
129146

130147
// sort
131148
sort.Strings(classes)
@@ -135,14 +152,18 @@ func processContent(content string) string {
135152

136153
// create new class list string
137154
newClassList := strings.Join(classes, " ")
138-
assert(newClassList != "", "New class list is empty")
155+
if newClassList == "" {
156+
continue
157+
}
139158

140159
// replace class list in file
141160
content = strings.Replace(content, match[0], "class=\""+newClassList+"\"", -1)
142-
assert(content != "", "New content is empty")
161+
if content == "" {
162+
continue
163+
}
143164
}
144165

145-
return content
166+
return content, nil
146167
}
147168

148169
func removeDuplicates(slice []string) []string {

0 commit comments

Comments
 (0)