Skip to content

Commit ebe75fe

Browse files
committed
Send/receive error details with widgets
1 parent d04135c commit ebe75fe

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

src/embedded.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
import {
1818
WidgetApi,
1919
WidgetApiToWidgetAction,
20+
WidgetApiResponseError,
2021
MatrixCapabilities,
2122
IWidgetApiRequest,
2223
IWidgetApiAcknowledgeResponseData,
@@ -45,6 +46,7 @@ import {
4546
} from "./client.ts";
4647
import { SyncApi, SyncState } from "./sync.ts";
4748
import { SlidingSyncSdk } from "./sliding-sync-sdk.ts";
49+
import { MatrixError } from "./http-api/errors.ts";
4850
import { User } from "./models/user.ts";
4951
import { Room } from "./models/room.ts";
5052
import { 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+
}

src/http-api/errors.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17+
import { IMatrixApiError as IWidgetMatrixError } from "matrix-widget-api";
18+
1719
import { IUsageLimit } from "../@types/partials.ts";
1820
import { MatrixEvent } from "../models/event.ts";
1921

@@ -131,6 +133,42 @@ export class MatrixError extends HTTPError {
131133
}
132134
return null;
133135
}
136+
137+
/**
138+
* @returns this error expressed as a {@link IWidgetMatrixError}
139+
* for use by Widget API error responses.
140+
*/
141+
public asWidgetApiErrorData(): IWidgetMatrixError {
142+
const headers: Record<string, string> = {};
143+
if (this.httpHeaders) {
144+
for (const [name, value] of this.httpHeaders) {
145+
headers[name] = value;
146+
}
147+
}
148+
return {
149+
http_status: this.httpStatus ?? 400,
150+
http_headers: headers,
151+
url: this.url ?? "",
152+
response: {
153+
errcode: this.errcode ?? "M_UNKNOWN",
154+
error: this.name,
155+
...this.data,
156+
},
157+
};
158+
}
159+
160+
public static fromWidgetApiErrorData(data: IWidgetMatrixError): MatrixError {
161+
return new MatrixError(
162+
{
163+
errcode: data.response.errcode,
164+
error: data.response.error,
165+
},
166+
data.http_status,
167+
data.url,
168+
undefined,
169+
new Headers(data.http_headers),
170+
);
171+
}
134172
}
135173

136174
/**

0 commit comments

Comments
 (0)