Skip to content

Commit

Permalink
Merge pull request #1 from mundipagg/hotfix/return-error-when-parse-m…
Browse files Browse the repository at this point in the history
…essage

return error when the message does not meet the template
  • Loading branch information
paulolimarb authored Jan 21, 2020
2 parents 3b43d28 + 6d95b25 commit 0498ed8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
7 changes: 6 additions & 1 deletion splunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,13 @@ func (sw *Writer) Write(entry tracer.Entry) {
}

properties := NewEntry(append(entry.Args, sw.defaultPropertiesApp))

message := punctuation.FindStringSubmatch(s.Capitalize(entry.Message))[1]
message = s.ProcessString(r.Replace(message), properties)

message, err := s.ProcessString(r.Replace(message), properties)
if err != nil {
message = entry.Message
}

l := NewEntry(sw.configLineLog)
l.Add("time", time.Now().UTC().UnixNano())
Expand Down
11 changes: 5 additions & 6 deletions strings/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package strings

import (
"bytes"
"fmt"
"html/template"
"regexp"
"strings"
Expand Down Expand Up @@ -35,21 +34,21 @@ func Capitalize(str string) string {
return strings.ToUpper(string(str[0])) + str[1:]
}

func ProcessString(str string, vars interface{}) string {
func ProcessString(str string, vars interface{}) (string, error) {
tmpl, err := template.New("tmpl").Parse(str)

if err != nil {
fmt.Println(err.Error())
return "", err
}
return process(tmpl, vars)
}

func process(t *template.Template, vars interface{}) string {
func process(t *template.Template, vars interface{}) (string, error) {
var tmplBytes bytes.Buffer

err := t.Execute(&tmplBytes, vars)
if err != nil {
fmt.Println(err.Error())
return "", err
}
return tmplBytes.String()
return tmplBytes.String(), nil
}

0 comments on commit 0498ed8

Please sign in to comment.