Skip to content

Commit

Permalink
Catch exceptions from the widget driver in the event relations endpoi…
Browse files Browse the repository at this point in the history
…nt (#73)

* Catch exceptions from the widget driver in the event relations endpoint

Signed-off-by: Dominik Henneke <[email protected]>

* Fix formatting

Signed-off-by: Dominik Henneke <[email protected]>

Signed-off-by: Dominik Henneke <[email protected]>
  • Loading branch information
dhenneke authored Sep 7, 2022
1 parent 434dddc commit aa9496b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 37 deletions.
80 changes: 44 additions & 36 deletions src/ClientWidgetApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,46 +587,54 @@ export class ClientWidgetApi extends EventEmitter {
});
}

const result = await this.driver.readEventRelations(
request.data.event_id, request.data.room_id, request.data.rel_type,
request.data.event_type, request.data.from, request.data.to,
request.data.limit, request.data.direction);

// check if the user is permitted to receive the event in question
if (result.originalEvent) {
if (result.originalEvent.state_key !== undefined) {
if (!this.canReceiveStateEvent(result.originalEvent.type, result.originalEvent.state_key)) {
return this.transport.reply<IWidgetApiErrorResponseData>(request, {
error: { message: "Cannot read state events of this type" },
});
}
} else {
if (!this.canReceiveRoomEvent(result.originalEvent.type, result.originalEvent.content['msgtype'])) {
return this.transport.reply<IWidgetApiErrorResponseData>(request, {
error: { message: "Cannot read room events of this type" },
});
try {
const result = await this.driver.readEventRelations(
request.data.event_id, request.data.room_id, request.data.rel_type,
request.data.event_type, request.data.from, request.data.to,
request.data.limit, request.data.direction,
);

// check if the user is permitted to receive the event in question
if (result.originalEvent) {
if (result.originalEvent.state_key !== undefined) {
if (!this.canReceiveStateEvent(result.originalEvent.type, result.originalEvent.state_key)) {
return this.transport.reply<IWidgetApiErrorResponseData>(request, {
error: { message: "Cannot read state events of this type" },
});
}
} else {
if (!this.canReceiveRoomEvent(result.originalEvent.type, result.originalEvent.content['msgtype'])) {
return this.transport.reply<IWidgetApiErrorResponseData>(request, {
error: { message: "Cannot read room events of this type" },
});
}
}
}
}

// only return events that the user has the permission to receive
const chunk = result.chunk.filter(e => {
if (e.state_key !== undefined) {
return this.canReceiveStateEvent(e.type, e.state_key);
} else {
return this.canReceiveRoomEvent(e.type, e.content['msgtype']);
}
});
// only return events that the user has the permission to receive
const chunk = result.chunk.filter(e => {
if (e.state_key !== undefined) {
return this.canReceiveStateEvent(e.type, e.state_key);
} else {
return this.canReceiveRoomEvent(e.type, e.content['msgtype']);
}
});

return this.transport.reply<IReadRelationsFromWidgetResponseData>(
request,
{
original_event: result.originalEvent,
chunk,
prev_batch: result.prevBatch,
next_batch: result.nextBatch,
},
);
return this.transport.reply<IReadRelationsFromWidgetResponseData>(
request,
{
original_event: result.originalEvent,
chunk,
prev_batch: result.prevBatch,
next_batch: result.nextBatch,
},
);
} catch (e) {
console.error("error getting the relations", e);
await this.transport.reply<IWidgetApiErrorResponseData>(request, {
error: { message: "Unexpected error while reading relations" },
});
}
}

private handleMessage(ev: CustomEvent<IWidgetApiRequest>) {
Expand Down
25 changes: 24 additions & 1 deletion test/ClientWidgetApi-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ describe('ClientWidgetApi', () => {
);
});


it('should reject requests without event_id', async () => {
const event: IWidgetApiRequest = {
api: WidgetApiDirection.FromWidget,
Expand Down Expand Up @@ -345,5 +344,29 @@ describe('ClientWidgetApi', () => {
});
});
});

it('should reject requests when the driver throws an exception', async () => {
driver.readEventRelations.mockRejectedValue(
new Error("M_FORBIDDEN: You don't have permission to access that event"),
);

const event: IReadRelationsFromWidgetActionRequest = {
api: WidgetApiDirection.FromWidget,
widgetId: 'test',
requestId: '0',
action: WidgetApiFromWidgetAction.MSC3869ReadRelations,
data: { event_id: '$event' },
};

await loadIframe();

emitEvent(new CustomEvent('', { detail: event }));

await waitFor(() => {
expect(transport.reply).toBeCalledWith(event, {
error: { message: 'Unexpected error while reading relations' },
});
});
});
});
});

0 comments on commit aa9496b

Please sign in to comment.