@@ -17,6 +17,7 @@ limitations under the License.
1717import {
1818 WidgetApi ,
1919 WidgetApiToWidgetAction ,
20+ WidgetApiResponseError ,
2021 MatrixCapabilities ,
2122 IWidgetApiRequest ,
2223 IWidgetApiAcknowledgeResponseData ,
@@ -45,6 +46,7 @@ import {
4546} from "./client.ts" ;
4647import { SyncApi , SyncState } from "./sync.ts" ;
4748import { SlidingSyncSdk } from "./sliding-sync-sdk.ts" ;
49+ import { MatrixError } from "./http-api/errors.ts" ;
4850import { User } from "./models/user.ts" ;
4951import { Room } from "./models/room.ts" ;
5052import { ToDeviceBatch , ToDevicePayload } from "./models/ToDeviceMessage.ts" ;
@@ -147,6 +149,26 @@ export class RoomWidgetClient extends MatrixClient {
147149 ) {
148150 super ( opts ) ;
149151
152+ const transportSend = this . widgetApi . transport . send . bind ( this . widgetApi . transport ) ;
153+ // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
154+ this . widgetApi . transport . send = async ( action , data ) => {
155+ try {
156+ return await transportSend ( action , data ) ;
157+ } catch ( error ) {
158+ processAndThrow ( error ) ;
159+ }
160+ } ;
161+
162+ const transportSendComplete = this . widgetApi . transport . sendComplete . bind ( this . widgetApi . transport ) ;
163+ // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
164+ this . widgetApi . transport . sendComplete = async ( action , data ) => {
165+ try {
166+ return await transportSendComplete ( action , data ) ;
167+ } catch ( error ) {
168+ processAndThrow ( error ) ;
169+ }
170+ } ;
171+
150172 this . widgetApiReady = new Promise < void > ( ( resolve ) => this . widgetApi . once ( "ready" , resolve ) ) ;
151173
152174 // Request capabilities for the functionality this client needs to support
@@ -523,3 +545,9 @@ export class RoomWidgetClient extends MatrixClient {
523545 }
524546 }
525547}
548+
549+ function processAndThrow ( error : unknown ) : never {
550+ throw error instanceof WidgetApiResponseError && error . data . matrix_api_error
551+ ? MatrixError . fromWidgetApiErrorData ( error . data . matrix_api_error )
552+ : error ;
553+ }
0 commit comments