48 lines
824 B
Go
48 lines
824 B
Go
package exception
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"os"
|
|
)
|
|
|
|
type Custom interface {
|
|
error
|
|
With(innerErr ...error) Custom
|
|
}
|
|
|
|
func NewCustom(message string, innerErr ...error) Custom {
|
|
return &custom{
|
|
message: message,
|
|
innerErrors: innerErr,
|
|
}
|
|
}
|
|
|
|
type custom struct {
|
|
message string
|
|
innerErrors []error
|
|
}
|
|
|
|
func (ex *custom) With(innerErr ...error) Custom {
|
|
ex.innerErrors = append(ex.innerErrors, innerErr...)
|
|
return ex
|
|
}
|
|
|
|
func (ex *custom) Error() string {
|
|
buf := bytes.NewBufferString("")
|
|
if len(ex.innerErrors) > 0 {
|
|
for _, innerE := range ex.innerErrors {
|
|
buf.WriteString(getOsNewLine())
|
|
buf.WriteString(innerE.Error())
|
|
}
|
|
}
|
|
return fmt.Sprintf("[%s]:%s", ex.message, buf.String())
|
|
}
|
|
|
|
func getOsNewLine() string {
|
|
if fmt.Sprintf("%v", os.PathSeparator) != "/" {
|
|
return "\n"
|
|
}
|
|
return "\r\n"
|
|
}
|