2023-12-08 14:30:55 +00:00
|
|
|
package zerrors
|
2020-03-19 13:39:06 +00:00
|
|
|
|
|
|
|
import (
|
2022-03-28 08:05:09 +00:00
|
|
|
"errors"
|
2020-03-19 13:39:06 +00:00
|
|
|
"fmt"
|
2020-06-11 11:22:24 +00:00
|
|
|
"reflect"
|
2020-03-19 13:39:06 +00:00
|
|
|
)
|
|
|
|
|
2023-12-08 14:30:55 +00:00
|
|
|
var _ Error = (*ZitadelError)(nil)
|
2020-03-19 13:39:06 +00:00
|
|
|
|
2023-12-08 14:30:55 +00:00
|
|
|
type ZitadelError struct {
|
2020-03-19 13:39:06 +00:00
|
|
|
Parent error
|
|
|
|
Message string
|
|
|
|
ID string
|
|
|
|
}
|
|
|
|
|
|
|
|
func ThrowError(parent error, id, message string) error {
|
2023-12-08 14:30:55 +00:00
|
|
|
return CreateZitadelError(parent, id, message)
|
2020-03-19 13:39:06 +00:00
|
|
|
}
|
|
|
|
|
2023-12-08 14:30:55 +00:00
|
|
|
func CreateZitadelError(parent error, id, message string) *ZitadelError {
|
|
|
|
return &ZitadelError{
|
2020-03-19 13:39:06 +00:00
|
|
|
Parent: parent,
|
|
|
|
ID: id,
|
|
|
|
Message: message,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-08 14:30:55 +00:00
|
|
|
func (err *ZitadelError) Error() string {
|
2020-03-19 13:39:06 +00:00
|
|
|
if err.Parent != nil {
|
|
|
|
return fmt.Sprintf("ID=%s Message=%s Parent=(%v)", err.ID, err.Message, err.Parent)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("ID=%s Message=%s", err.ID, err.Message)
|
|
|
|
}
|
|
|
|
|
2023-12-08 14:30:55 +00:00
|
|
|
func (err *ZitadelError) Unwrap() error {
|
2020-03-19 13:39:06 +00:00
|
|
|
return err.GetParent()
|
|
|
|
}
|
|
|
|
|
2023-12-08 14:30:55 +00:00
|
|
|
func (err *ZitadelError) GetParent() error {
|
2020-03-19 13:39:06 +00:00
|
|
|
return err.Parent
|
|
|
|
}
|
|
|
|
|
2023-12-08 14:30:55 +00:00
|
|
|
func (err *ZitadelError) GetMessage() string {
|
2020-03-19 13:39:06 +00:00
|
|
|
return err.Message
|
|
|
|
}
|
|
|
|
|
2023-12-08 14:30:55 +00:00
|
|
|
func (err *ZitadelError) SetMessage(msg string) {
|
2020-08-28 07:44:43 +00:00
|
|
|
err.Message = msg
|
|
|
|
}
|
|
|
|
|
2023-12-08 14:30:55 +00:00
|
|
|
func (err *ZitadelError) GetID() string {
|
2020-03-19 13:39:06 +00:00
|
|
|
return err.ID
|
|
|
|
}
|
2020-04-28 14:01:00 +00:00
|
|
|
|
2023-12-08 14:30:55 +00:00
|
|
|
func (err *ZitadelError) Is(target error) bool {
|
|
|
|
t, ok := target.(*ZitadelError)
|
2022-03-28 08:05:09 +00:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if t.ID != "" && t.ID != err.ID {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if t.Message != "" && t.Message != err.Message {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if t.Parent != nil && !errors.Is(err.Parent, t.Parent) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2020-04-28 14:01:00 +00:00
|
|
|
}
|
2020-06-11 11:22:24 +00:00
|
|
|
|
2023-12-08 14:30:55 +00:00
|
|
|
func (err *ZitadelError) As(target interface{}) bool {
|
|
|
|
_, ok := target.(**ZitadelError)
|
2021-11-03 07:35:24 +00:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
2020-06-11 11:22:24 +00:00
|
|
|
reflect.Indirect(reflect.ValueOf(target)).Set(reflect.ValueOf(err))
|
|
|
|
return true
|
|
|
|
}
|