Файли
2026-11-08 03:46:11 +00:00

44 рядки
1.1 KiB
Go

package shared
import (
"errors"
"git.entityseven.com/rilaxik/pllist/internal/domain"
)
func UserMessage(err error) string {
var validation domain.ValidationError
if errors.As(err, &validation) {
return validation.Message
}
switch {
case errors.Is(err, domain.ErrNotFound):
return "Not found or no access."
case errors.Is(err, domain.ErrForbidden):
return "You cannot do that."
case errors.Is(err, domain.ErrConflict):
return "This changed while you were looking. Try again."
case errors.Is(err, domain.ErrInvalidInput):
return "Invalid input."
case errors.Is(err, ErrBadCallback):
return "This button is out of date. Use /lists."
default:
return "Something went wrong. Try again."
}
}
// IsExpectedError reports whether an error carries a user-facing explanation
// rather than an internal failure.
func IsExpectedError(err error) bool {
var validation domain.ValidationError
if errors.As(err, &validation) {
return true
}
return errors.Is(err, domain.ErrNotFound) ||
errors.Is(err, domain.ErrForbidden) ||
errors.Is(err, domain.ErrConflict) ||
errors.Is(err, domain.ErrInvalidInput) ||
errors.Is(err, ErrBadCallback)
}