Files
orbit/internal/fault/fault.go

33 lines
1.3 KiB
Go

// Package fault 定义不包含数据库或敏感参数的稳定业务错误。
package fault
import "errors"
type Error struct {
Code string `json:"code"`
Message string `json:"message"`
}
func (e *Error) Error() string { return e.Code + ": " + e.Message }
func New(code, message string) *Error { return &Error{Code: code, Message: message} }
func Code(err error) string {
var e *Error
if errors.As(err, &e) {
return e.Code
}
return "UNAVAILABLE"
}
var (
Unauthorized = New("UNAUTHENTICATED", "valid credentials are required")
Forbidden = New("PERMISSION_DENIED", "operation is not allowed")
NotFound = New("NOT_FOUND", "resource not found")
Conflict = New("STATE_CONFLICT", "resource state does not allow this operation")
IdempotencyConflict = New("IDEMPOTENCY_CONFLICT", "idempotency key was used with different content")
Unavailable = New("UNAVAILABLE", "service temporarily unavailable; retry using the original operation identity")
QueueFull = New("QUEUE_FULL", "queue unfinished task limit reached; retry later using the original identity")
ApplicationFull = New("APPLICATION_FULL", "application unfinished task limit reached; retry later using the original identity")
)
func Invalid(message string) error { return New("INVALID_ARGUMENT", message) }