withCollectionAliasFallback runs op against the RAW collection slug the user typed, and — only if that fails specifically because the collection was not found — retries once with the legacy singular→plural alias (collections.NormalizeSlug). The result of whichever attempt succeeds is returned; if bo
(rawSlug string, notFoundIsAuthoritative func() bool, op func(slug string) (T, error))
| 57 | // send-raw-then-retry dance (BUG-2630: one shared helper, not |
| 58 | // copied at the call sites). |
| 59 | func WithCollectionAliasFallback[T any](rawSlug string, notFoundIsAuthoritative func() bool, op func(slug string) (T, error)) (T, error) { |
| 60 | result, err := op(rawSlug) |
| 61 | if err == nil { |
| 62 | return result, nil |
| 63 | } |
| 64 | // The failure was not a missing collection (auth, validation, a 5xx): return |
| 65 | // it untouched. Retrying such an error against the alias could silently |
| 66 | // succeed on a DIFFERENT collection that does exist — BUG-2630 in a new |
| 67 | // costume — so a collection that exists is never retried away from. |
| 68 | if !isCollectionNotFound(err) { |
| 69 | return result, err |
| 70 | } |
| 71 | normalized := collections.NormalizeSlug(rawSlug) |
| 72 | if normalized == rawSlug { |
| 73 | // No alias to try (the input is not one of the aliased singulars): the |
| 74 | // slug genuinely names no collection. Name it in the error the user |
| 75 | // sees, since it is their own word. |
| 76 | return result, wrapCollectionNotFound(err, rawSlug) |
| 77 | } |
| 78 | // When the server's not-found is authoritative — it resolves collections |
| 79 | // itself (BUG-2578/2630), so it already tried the alias and enforced |
| 80 | // exact-match-first + the archived/hidden refusal — retrying the alias would |
| 81 | // only defeat that protection (BUG-2630 #1). Only fall back to the |
| 82 | // client-side retry for an OLDER server that never had the protection. |
| 83 | if notFoundIsAuthoritative != nil && notFoundIsAuthoritative() { |
| 84 | return result, wrapCollectionNotFound(err, rawSlug) |
| 85 | } |
| 86 | result2, err2 := op(normalized) |
| 87 | if err2 != nil { |
| 88 | if isCollectionNotFound(err2) { |
| 89 | // Neither the raw slug nor its alias names a collection. Surface an |
| 90 | // error naming the RAW slug the user typed, not the alias the |
| 91 | // client tried on their behalf — their own words are the ones they |
| 92 | // can act on. |
| 93 | return result, wrapCollectionNotFound(err, rawSlug) |
| 94 | } |
| 95 | // The alias DOES name a real collection, but the operation failed there |
| 96 | // for a substantive reason (plan limit, open children, validation, |
| 97 | // forbidden). That error is about a real collection and is far more |
| 98 | // useful than a misleading "collection not found" — surface it verbatim. |
| 99 | return result2, err2 |
| 100 | } |
| 101 | return result2, nil |
| 102 | } |
| 103 | |
| 104 | // wrapCollectionNotFound rewrites the server's context-free "Collection not |
| 105 | // found" into one that names the slug the user actually typed, so a failed |