2024-04-06 21:11:22 +07:00
|
|
|
package errs
|
|
|
|
|
|
|
|
import "errors"
|
|
|
|
|
|
|
|
func FindCodeOrDefault(err error, def int) int {
|
2024-04-24 21:57:13 +07:00
|
|
|
unwrap := errors.Unwrap(err)
|
|
|
|
for unwrap != nil {
|
2024-04-06 21:11:22 +07:00
|
|
|
if coder, ok := err.(interface{ GetCode() int }); ok {
|
|
|
|
code := coder.GetCode()
|
|
|
|
if code != 0 {
|
2024-04-24 21:57:13 +07:00
|
|
|
return code
|
2024-04-06 21:11:22 +07:00
|
|
|
}
|
|
|
|
}
|
2024-04-24 21:57:13 +07:00
|
|
|
unwrap = errors.Unwrap(unwrap)
|
2024-04-06 21:11:22 +07:00
|
|
|
}
|
2024-04-24 21:57:13 +07:00
|
|
|
|
2024-04-06 21:11:22 +07:00
|
|
|
return def
|
|
|
|
}
|
2024-04-24 21:57:13 +07:00
|
|
|
|
|
|
|
func FindMessage(err error) string {
|
|
|
|
unwrap := errors.Unwrap(err)
|
|
|
|
for unwrap != nil {
|
|
|
|
if messager, ok := err.(interface{ GetMessage() string }); ok {
|
|
|
|
message := messager.GetMessage()
|
|
|
|
if message != "" {
|
|
|
|
return message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err.Error()
|
|
|
|
}
|
2024-04-24 22:26:04 +07:00
|
|
|
|
|
|
|
func HTTPMessage(err error) (code int, message string) {
|
|
|
|
code = FindCodeOrDefault(err, 500)
|
|
|
|
if code >= 500 {
|
|
|
|
return code, err.Error()
|
|
|
|
}
|
|
|
|
return code, FindMessage(err)
|
|
|
|
}
|