writeInternalError logs the real error server-side and sends a generic message to the client. This prevents leaking SQL errors, file paths, and other internal details.
(w http.ResponseWriter, err error)
| 3047 | // message to the client. This prevents leaking SQL errors, file paths, |
| 3048 | // and other internal details. |
| 3049 | func writeInternalError(w http.ResponseWriter, err error) { |
| 3050 | // A database that was too busy (statement, lock or idle timeout, deadlock, |
| 3051 | // context deadline) is retryable and rolled back: answer it as such, never |
| 3052 | // as a 500 (TASK-4278). First, so no later arm can claim it. This funnel |
| 3053 | // has no request, so it cannot tell a write from a read and uses the |
| 3054 | // neutral wording. |
| 3055 | if store.IsDBRetryable(err) { |
| 3056 | slog.Warn("database busy", "error", err) |
| 3057 | writeBusy(w, err, false) |
| 3058 | return |
| 3059 | } |
| 3060 | // The store's NUL refusal is a DECISION, not a fault, and it is mapped |
| 3061 | // here rather than at each handler for the same reason Layer A lives at |
| 3062 | // the driver: this is the one funnel every 500 already passes through, so |
| 3063 | // mapping it once covers every handler that exists and every one that will |
| 3064 | // (DOC-2823 S1). Enumerating error blocks is what the item-title unit had |
| 3065 | // to do three times in one function before a structural test caught the |
| 3066 | // third. |
| 3067 | // |
| 3068 | // 400, matching what the HTTP gate answers for the SAME value refused at |
| 3069 | // the door. Two statuses for one rule would be the layers disagreeing |
| 3070 | // again, in the response this time instead of in the predicate. |
| 3071 | // |
| 3072 | // The honest residual: a value can reach the store from something the |
| 3073 | // SERVER composed rather than the caller supplied — that is BUG-2814's |
| 3074 | // re-emit population — and for those a 400 tells the caller their request |
| 3075 | // was bad when it was our stored data. It is still the better answer than |
| 3076 | // 500, because the request is understood and will be refused identically |
| 3077 | // on retry, and the log line below keeps the detail. If the re-emit case |
| 3078 | // ever needs its own status, it needs its own error type first. |
| 3079 | if reason, ok := nulRefusalReason(err); ok { |
| 3080 | slog.Warn("write refused: invalid text parameter", "error", err) |
| 3081 | writeError(w, http.StatusBadRequest, "bad_request", reason) |
| 3082 | return |
| 3083 | } |
| 3084 | // The outbox row cap is likewise a decision, not a fault, and is mapped in |
| 3085 | // the same funnel for the same reason (BUG-2827). 413 with a *_too_large |
| 3086 | // code follows the house precedent set by rename_cascade_too_large rather |
| 3087 | // than inventing a second spelling for "this operation produces more than |
| 3088 | // the server will process in one go". |
| 3089 | // |
| 3090 | // The literal reading of 413 is that the REQUEST entity is too large, and |
| 3091 | // here the request is small while the event it derives is not. Taken |
| 3092 | // alone that argues for 422. It loses to consistency: an operator watching |
| 3093 | // for cascade refusals should not have to know which of two size bounds |
| 3094 | // fired to know which status to grep for, and the message says plainly |
| 3095 | // what was actually too big. |
| 3096 | // |
| 3097 | // Composed from the TYPED fields, never by splicing err.Error(), so no |
| 3098 | // wrapper the call path added is published to the caller. |
| 3099 | var oversized *store.OversizedOutboxPayloadError |
| 3100 | if errors.As(err, &oversized) { |
| 3101 | slog.Warn("write refused: outbox payload over the size limit", |
| 3102 | "event_type", oversized.EventType, "bytes", oversized.Bytes, "limit", oversized.Limit) |
| 3103 | // The message names WHAT was measured, because the store refuses on two |
| 3104 | // different measurements — the member content before marshalling, and |
| 3105 | // the row as stored — and a caller told only "%d bytes" for both |
| 3106 | // cannot reconcile two different numbers for one mutation. |