photoprism/pkg/clean/error.go

34 lines
666 B
Go

package clean
import "strings"
// Error sanitizes an error message so that it can be safely logged or displayed.
func Error(err error) string {
if err == nil {
return "no error"
} else if s := strings.TrimSpace(err.Error()); s == "" {
return "unknown error"
} else {
// Limit error message length.
if len(s) > MaxLength {
s = s[:MaxLength]
}
// Remove non-printable and other potentially problematic characters.
return strings.Map(func(r rune) rune {
if r < 32 || r == 127 {
return -1
}
switch r {
case '`', '"':
return '\''
case '\\', '$', '<', '>', '{', '}':
return '?'
default:
return r
}
}, s)
}
}