Files
descrybe/apps/api/internal/feeds/errors.go
T

60 lines
1.6 KiB
Go
Raw Normal View History

package feeds
import (
"errors"
"github.com/jackc/pgx/v5"
)
// ErrFormatMismatch is returned when the public export URL extension does not
// match the feed's configured format. Public HTTP handlers must map this to the
// same opaque 404 as an unknown token (no existence oracle).
var ErrFormatMismatch = errors.New("format mismatch")
// ErrNotFound is returned when a company-scoped feed (or related row) is missing.
var ErrNotFound = errors.New("not found")
// clientError is a validation/business message safe to return to API clients.
type clientError struct {
msg string
}
func (e *clientError) Error() string { return e.msg }
// ClientMsg marks a message as safe to expose in HTTP 4xx responses.
func ClientMsg(msg string) error {
return &clientError{msg: msg}
}
// ClientError reports whether err is a known client-facing feeds error.
func ClientError(err error) (msg string, ok bool) {
if err == nil {
return "", false
}
var ce *clientError
if errors.As(err, &ce) {
return ce.msg, true
}
switch {
case errors.Is(err, ErrNotFound), errors.Is(err, pgx.ErrNoRows):
return "not found", true
case errors.Is(err, ErrFormatMismatch),
errors.Is(err, errURLRequired),
errors.Is(err, errURLScheme),
errors.Is(err, errURLPrivate),
errors.Is(err, errURLFTP),
errors.Is(err, errDownloadTooLarge),
errors.Is(err, errParseTooManyRows),
errors.Is(err, errSourceRequired),
errors.Is(err, errLocalSource):
return err.Error(), true
default:
return "", false
}
}
// IsNotFound reports whether err means a missing feed/resource.
func IsNotFound(err error) bool {
return errors.Is(err, ErrNotFound) || errors.Is(err, pgx.ErrNoRows)
}