fix(upload): plain-language upload failure text instead of the raw MatrixError (#213)
The upload card printed the SDK's toString — 'MatrixError: [413] nope (http://<hs>/_matrix/media/v3/upload?filename=…)'. describeUploadError() maps the common cases to one sentence: 413/M_TOO_LARGE → 'This file is larger than the server allows (limit N)' using m.upload.size when known, 429 → 'Slow down — try again in a moment.', 401/403 → 'The server refused this upload: <server text>', 5xx/transport after the retry loop → 'Couldn't reach the server. Check your connection and retry.', other 4xx → the server's own sentence, URL stripped. Both card renderers use it; the raw error is still console.warn-ed by uploadContent for debugging. Unit-tested; verified headless with routed 413/403/503 responses. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PPmy3tPq869XDW4njjVaKA
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
import { MatrixError } from 'matrix-js-sdk';
|
||||
import { bytesToSize } from './common';
|
||||
|
||||
/**
|
||||
* [Gitea #213] One plain sentence for a failed upload. `MatrixError.message`
|
||||
* is the SDK's toString — "MatrixError: [413] nope (http://hs/_matrix/media/…)"
|
||||
* — which is what the card used to print. The raw error still goes to the
|
||||
* console for debugging.
|
||||
*/
|
||||
export const describeUploadError = (error: MatrixError, limitBytes?: number): string => {
|
||||
const status = error.httpStatus;
|
||||
const errcode = error.errcode;
|
||||
const serverText = (error.data as { error?: string } | undefined)?.error;
|
||||
|
||||
if (status === 413 || errcode === 'M_TOO_LARGE') {
|
||||
const limit =
|
||||
typeof limitBytes === 'number' && Number.isFinite(limitBytes)
|
||||
? ` (limit ${bytesToSize(limitBytes)})`
|
||||
: '';
|
||||
return `This file is larger than the server allows${limit}.`;
|
||||
}
|
||||
if (status === 429 || errcode === 'M_LIMIT_EXCEEDED') {
|
||||
return 'Slow down — try again in a moment.';
|
||||
}
|
||||
if (status === 401 || status === 403 || errcode === 'M_FORBIDDEN') {
|
||||
return serverText
|
||||
? `The server refused this upload: ${serverText}`
|
||||
: 'The server refused this upload.';
|
||||
}
|
||||
if (typeof status !== 'number' || status === 408 || status >= 500) {
|
||||
// Transport failure after the retry loop gave up, or a server-side error.
|
||||
if (serverText === 'Upload cancelled') return 'Upload cancelled.';
|
||||
return "Couldn't reach the server. Check your connection and retry.";
|
||||
}
|
||||
// Any other 4xx: the server's own sentence, never the URL-bearing toString.
|
||||
return serverText ? serverText.replace(/\s*\(https?:\/\/[^)]*\)\s*$/, '') : 'Upload failed.';
|
||||
};
|
||||
Reference in New Issue
Block a user