Handle media download errors better (#12848)

* Handle media download errors better

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

* Add test

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

* Show error if media download failed

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

* More tests

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

---------

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
Michael Telatynski 2024-07-31 15:07:59 +01:00 committed by GitHub
parent b55653ddf0
commit f3ac6692da
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 140 additions and 8 deletions

View file

@ -14,7 +14,7 @@
* limitations under the License.
*/
import { MatrixClient, ResizeMethod } from "matrix-js-sdk/src/matrix";
import { MatrixClient, parseErrorResponse, ResizeMethod } from "matrix-js-sdk/src/matrix";
import { MediaEventContent } from "matrix-js-sdk/src/types";
import { Optional } from "matrix-events-sdk";
@ -144,12 +144,16 @@ export class Media {
* Downloads the source media.
* @returns {Promise<Response>} Resolves to the server's response for chaining.
*/
public downloadSource(): Promise<Response> {
public async downloadSource(): Promise<Response> {
const src = this.srcHttp;
if (!src) {
throw new UserFriendlyError("error|download_media");
}
return fetch(src);
const res = await fetch(src);
if (!res.ok) {
throw parseErrorResponse(res, await res.text());
}
return res;
}
}