// 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") ) func Invalid(message string) error { return New("INVALID_ARGUMENT", message) }