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