2020-11-17 13:07:30 -08:00
|
|
|
package sanitizederror
|
2020-03-06 03:00:18 +05:30
|
|
|
|
2020-12-07 11:26:04 -08:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
2020-06-12 18:23:59 -07:00
|
|
|
|
2020-03-06 03:00:18 +05:30
|
|
|
type customError struct {
|
|
|
|
message string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c customError) Error() string {
|
|
|
|
return c.message
|
|
|
|
}
|
|
|
|
|
2020-12-07 11:26:04 -08:00
|
|
|
func New(msg string) error {
|
|
|
|
return customError{message: msg}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewWithErrors(message string, errors []error) error {
|
|
|
|
bldr := strings.Builder{}
|
|
|
|
bldr.WriteString(message + "\n")
|
|
|
|
for _, err := range errors {
|
|
|
|
bldr.WriteString(err.Error() + "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
return customError{message: bldr.String()}
|
2020-03-06 03:00:18 +05:30
|
|
|
}
|
|
|
|
|
2020-11-17 13:07:30 -08:00
|
|
|
// NewWithError creates a new sanitized error with given message and error
|
2020-06-12 18:23:59 -07:00
|
|
|
func NewWithError(message string, err error) error {
|
2020-07-03 17:42:08 -07:00
|
|
|
if err == nil {
|
|
|
|
return customError{message: message}
|
|
|
|
}
|
|
|
|
|
2020-06-12 18:23:59 -07:00
|
|
|
msg := fmt.Sprintf("%s \nCause: %s", message, err.Error())
|
|
|
|
return customError{message: msg}
|
|
|
|
}
|
|
|
|
|
2020-11-17 13:07:30 -08:00
|
|
|
// IsErrorSanitized checks if the error is sanitized error
|
2020-03-06 03:00:18 +05:30
|
|
|
func IsErrorSanitized(err error) bool {
|
|
|
|
if _, ok := err.(customError); !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|