Skip to content

Commit

Permalink
Add a recovery logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
leekchan committed Jul 14, 2015
1 parent 3134947 commit a991913
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ func main() {
}
```


## Safety
All gtf functions have their own recovery logics. The basic behaviour of the recovery logic is silently swallowing all unexpected panics. All gtf functions would not make any panics in runtime. (**Production Ready!**)

If a panic occurs inside a gtf function, the function will silently swallow the panic and return "" (empty string). If you meet any unexpected empty output, [please make an issue](https://github.com/leekchan/gtf/issues/new)! :)



## Reference
### stringReplace

Expand Down
25 changes: 24 additions & 1 deletion gtf.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,43 @@ import (
"net/url"
)

// recovery will silently swallow all unexpected panics.
func recovery() {
recover()
}

var GtfFuncMap = template.FuncMap {
"stringReplace": func(s1 string, s2 string) string {
defer recovery()

return strings.Replace(s2, s1, "", -1)
},
"stringDefault": func(s1 string, s2 string) string {
defer recovery()

if len(s2) > 0 {
return s2
}
return s1
},
"stringLength": func(s string) int {
defer recovery()

return len(s)
},
"stringLower": func(s string) string {
defer recovery()

return strings.ToLower(s)
},
"stringUpper": func(s string) string {
defer recovery()

return strings.ToUpper(s)
},
"stringTruncatechars": func(n int, s string) string {
defer recovery()

if n < 0 {
return s
}
Expand All @@ -43,8 +60,14 @@ var GtfFuncMap = template.FuncMap {

return string(r[:n])
},
"stringUrlencode": url.QueryEscape,
"stringUrlencode": func(s string) string {
defer recovery()

return url.QueryEscape(s)
},
"stringWordcount": func(s string) int {
defer recovery()

return len(strings.Fields(s))
},
}
Expand Down

0 comments on commit a991913

Please sign in to comment.