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.'; };