From 9506bf1b811fd6e1717c254bbd948a41d47382db Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Wed, 10 Mar 2021 13:43:55 +0100 Subject: [PATCH] clean up video upload error handling --- src/domain/session/room/RoomViewModel.js | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/domain/session/room/RoomViewModel.js b/src/domain/session/room/RoomViewModel.js index a6d5454a..43eeb75c 100644 --- a/src/domain/session/room/RoomViewModel.js +++ b/src/domain/session/room/RoomViewModel.js @@ -188,20 +188,29 @@ export class RoomViewModel extends ViewModel { } async _pickAndSendVideo() { - let file; try { if (!this.platform.hasReadPixelPermission()) { alert("Please allow canvas image data access, so we can scale your images down."); return; } - file = await this.platform.openFile("video/*"); + const file = await this.platform.openFile("video/*"); if (!file) { return; } if (!file.blob.mimeType.startsWith("video/")) { return this._sendFile(file); } - let video = await this.platform.loadVideo(file.blob); + let video; + try { + video = await this.platform.loadVideo(file.blob); + } catch (err) { + // TODO: extract platform dependent code from view model + if (err instanceof window.MediaError && err.code === 4) { + throw new Error(`this browser does not support videos of type ${file?.blob.mimeType}.`); + } else { + throw err; + } + } const content = { body: file.name, msgtype: "m.video", @@ -219,11 +228,7 @@ export class RoomViewModel extends ViewModel { this._room.createAttachment(thumbnail.blob, file.name); await this._room.sendEvent("m.room.message", content, attachments); } catch (err) { - if (err instanceof window.MediaError && err.code === 4) { - this._sendError = new Error(`this browser does not support videos of type ${file?.blob.mimeType}.`); - } else { - this._sendError = err; - } + this._sendError = err; this.emitChange("error"); console.error(err.stack); }