3
0

Safe errors

This commit is contained in:
Denis Arh 2020-11-04 15:13:53 +01:00
parent ed8fc547f7
commit 111beae959
2 changed files with 18 additions and 1 deletions

View File

@ -1,6 +1,7 @@
package types
import (
"encoding/json"
"errors"
"fmt"
)
@ -17,6 +18,9 @@ type (
}
)
// safe to show details of this error
func (RecordValueErrorSet) Safe() bool { return true }
func (v *RecordValueErrorSet) Push(err ...RecordValueError) {
v.Set = append(v.Set, err...)
}
@ -29,6 +33,16 @@ func (v *RecordValueErrorSet) Error() string {
return fmt.Sprintf("%d issue(s) found", len(v.Set))
}
func (v RecordValueErrorSet) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Message string `json:"message"`
Set []RecordValueError `json:"set,omitempty"`
}{
Message: v.Error(),
Set: v.Set,
})
}
// IsRecordValueErrorSet tests if given error is RecordValueErrorSet (or it wraps it) and it has errors
// If not is not (or !IsValid), it return nil!
func IsRecordValueErrorSet(err error) *RecordValueErrorSet {

View File

@ -59,6 +59,9 @@ func Newf(k kind, m string, a ...interface{}) *Error {
return err(k, fmt.Sprintf(m, a...))
}
// safe to show details of this error
func (Error) Safe() bool { return true }
// Error message
func (e Error) Error() string {
return e.message
@ -115,6 +118,6 @@ func Is(err, target error) bool {
}
// As is alias for errors.As so users can avoid importing both errors packages
func As(err, target error) bool {
func As(err error, target interface{}) bool {
return errors.As(err, target)
}