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
49 lines
1.9 KiB
TypeScript
49 lines
1.9 KiB
TypeScript
import { test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { MatrixError } from 'matrix-js-sdk';
|
|
import { describeUploadError } from './uploadError';
|
|
|
|
const err = (status: number | undefined, body: { errcode?: string; error?: string }) =>
|
|
new MatrixError(body, status, 'http://hs/_matrix/media/v3/upload?filename=x.png');
|
|
|
|
test('413 names the limit when known and never leaks the URL', () => {
|
|
const e = err(413, { errcode: 'M_TOO_LARGE', error: 'nope' });
|
|
assert.equal(
|
|
describeUploadError(e, 50 * 1024 * 1024),
|
|
'This file is larger than the server allows (limit 52.4 MB).',
|
|
);
|
|
assert.equal(describeUploadError(e), 'This file is larger than the server allows.');
|
|
assert.ok(!describeUploadError(e).includes('http'));
|
|
});
|
|
|
|
test('429, 403, 5xx and transport failures each get their sentence', () => {
|
|
assert.equal(
|
|
describeUploadError(err(429, { errcode: 'M_LIMIT_EXCEEDED', error: 'Too Many Requests' })),
|
|
'Slow down — try again in a moment.',
|
|
);
|
|
assert.equal(
|
|
describeUploadError(err(403, { errcode: 'M_FORBIDDEN', error: 'Uploads disabled' })),
|
|
'The server refused this upload: Uploads disabled',
|
|
);
|
|
assert.equal(
|
|
describeUploadError(err(502, { errcode: 'M_UNKNOWN', error: 'Bad Gateway' })),
|
|
"Couldn't reach the server. Check your connection and retry.",
|
|
);
|
|
assert.equal(
|
|
describeUploadError(new MatrixError({ error: 'request failed' })),
|
|
"Couldn't reach the server. Check your connection and retry.",
|
|
);
|
|
});
|
|
|
|
test('a cancel and an unknown 4xx fall through to the server text', () => {
|
|
assert.equal(
|
|
describeUploadError(new MatrixError({ error: 'Upload cancelled' })),
|
|
'Upload cancelled.',
|
|
);
|
|
assert.equal(
|
|
describeUploadError(err(400, { errcode: 'M_BAD_JSON', error: 'Unsupported file type' })),
|
|
'Unsupported file type',
|
|
);
|
|
assert.equal(describeUploadError(err(400, {})), 'Upload failed.');
|
|
});
|