style: use interface functions instead of inline logic

Signed-off-by: Jesse Simpson <jesse.simpson36@gmail.com>
pull/13586/head
Jesse Simpson 9 months ago
parent 87f9e2dc45
commit a56daca82b
No known key found for this signature in database
GPG Key ID: 237495C89AB0AAFC

@ -340,6 +340,35 @@ func (t TraceableError) String() string {
return t.location + "\n " + t.executedFunction + "\n " + t.message + "\n" return t.location + "\n " + t.executedFunction + "\n " + t.message + "\n"
} }
func (t TraceableError) ExtractExecutedFunction() (TraceableError, error) {
executionLocationRegex, regexFindErr := regexp.Compile(`executing "[^\"]*" at <[^\<\>]*>:?\s*`)
if regexFindErr != nil {
return t, regexFindErr
}
byteArrayMsg := []byte(t.message)
executionLocations := executionLocationRegex.FindAll(byteArrayMsg, -1)
t.executedFunction = string(executionLocations[0])
t.message = strings.ReplaceAll(t.message, t.executedFunction, "")
return t, nil
}
func (t TraceableError) FilterLocation() TraceableError {
if strings.Contains(t.message, t.location) {
t.message = strings.ReplaceAll(t.message, t.location, "")
}
return t
}
func (t TraceableError) FilterUnnecessaryWords() TraceableError {
if strings.Contains(t.message, "template:") {
t.message = strings.TrimSpace(strings.ReplaceAll(t.message, "template:", ""))
}
if strings.HasPrefix(t.message, ": ") {
t.message = strings.TrimSpace(strings.TrimPrefix(t.message, ": "))
}
return t
}
func cleanupExecError(filename string, err error) error { func cleanupExecError(filename string, err error) error {
if _, isExecError := err.(template.ExecError); !isExecError { if _, isExecError := err.(template.ExecError); !isExecError {
return err return err
@ -389,33 +418,24 @@ func cleanupExecError(filename string, err error) error {
prevMessage = currentMsg prevMessage = currentMsg
} }
for i := len(fileLocations) - 1; i >= 0; i-- { for i, fileLocation := range fileLocations {
if strings.Contains(fileLocations[i].message, fileLocations[i].location) { t := fileLocation.FilterLocation()
fileLocations[i].message = strings.ReplaceAll(fileLocations[i].message, fileLocations[i].location, "") fileLocations[i] = t
}
} }
for i := len(fileLocations) - 1; i >= 0; i-- { for i, fileLocation := range fileLocations {
if strings.Contains(fileLocations[i].message, "template:") { fileLocations[i] = fileLocation.FilterUnnecessaryWords()
fileLocations[i].message = strings.TrimSpace(strings.ReplaceAll(fileLocations[i].message, "template:", ""))
}
if strings.HasPrefix(fileLocations[i].message, ": ") {
fileLocations[i].message = strings.TrimSpace(strings.TrimPrefix(fileLocations[i].message, ": "))
}
} }
for i := len(fileLocations) - 1; i >= 0; i-- { for i, fileLocation := range fileLocations {
if fileLocations[i].message == "" { if fileLocation.message == "" {
continue continue
} }
executionLocationRegex, regexFindErr := regexp.Compile(`executing "[^\"]*" at <[^\<\>]*>:?\s*`) t, extractionErr := fileLocation.ExtractExecutedFunction()
if regexFindErr != nil { if extractionErr != nil {
continue continue
} }
byteArrayMsg := []byte(fileLocations[i].message) fileLocations[i] = t
executionLocations := executionLocationRegex.FindAll(byteArrayMsg, -1)
fileLocations[i].executedFunction = string(executionLocations[0])
fileLocations[i].message = strings.ReplaceAll(fileLocations[i].message, fileLocations[i].executedFunction, "")
} }
finalErrorString := "" finalErrorString := ""

Loading…
Cancel
Save