Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass HTTP status code & errcode from CS-API errors #100

Merged
merged 12 commits into from
Nov 8, 2024
36 changes: 27 additions & 9 deletions src/ClientWidgetApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import { IWidgetApiRequest, IWidgetApiRequestEmptyData } from "./interfaces/IWidgetApiRequest";
import { IContentLoadedActionRequest } from "./interfaces/ContentLoadedAction";
import { WidgetApiFromWidgetAction, WidgetApiToWidgetAction } from "./interfaces/WidgetApiAction";
import { IWidgetApiErrorResponseData } from "./interfaces/IWidgetApiErrorResponse";
import { IWidgetApiErrorResponseData, isMatrixError } from "./interfaces/IWidgetApiErrorResponse";
import { Capability, MatrixCapabilities } from "./interfaces/Capabilities";
import { IOpenIDUpdate, ISendEventDetails, ISendDelayedEventDetails, WidgetDriver } from "./driver/WidgetDriver";
import {
Expand Down Expand Up @@ -330,7 +330,7 @@
});
}

const onErr = (e: any) => {

Check warning on line 333 in src/ClientWidgetApi.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
console.error("[ClientWidgetApi] Failed to handle navigation: ", e);
return this.transport.reply<IWidgetApiErrorResponseData>(request, {
error: {message: "Error handling navigation"},
Expand Down Expand Up @@ -437,7 +437,7 @@
if (request.data.room_ids) {
askRoomIds = request.data.room_ids as string[];
if (!Array.isArray(askRoomIds)) {
askRoomIds = [askRoomIds as any as string];

Check warning on line 440 in src/ClientWidgetApi.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
}
for (const roomId of askRoomIds) {
if (!this.canUseRoomTimeline(roomId)) {
Expand Down Expand Up @@ -554,10 +554,13 @@
delay_id: sentEvent.delayId,
}),
});
}).catch(e => {
}).catch((e: unknown) => {
console.error("error sending event: ", e);
return this.transport.reply<IWidgetApiErrorResponseData>(request, {
error: {message: "Error sending event"},
error: {
message: "Error sending event",
...(isMatrixError(e) && e),
},
});
});
}
Expand All @@ -581,10 +584,13 @@
case UpdateDelayedEventAction.Send:
this.driver.updateDelayedEvent(request.data.delay_id, request.data.action).then(() => {
return this.transport.reply<IWidgetApiAcknowledgeResponseData>(request, {});
}).catch(e => {
}).catch((e: unknown) => {
console.error("error updating delayed event: ", e);
return this.transport.reply<IWidgetApiErrorResponseData>(request, {
error: {message: "Error updating delayed event"},
error: {
message: "Error updating delayed event",
...(isMatrixError(e) && e),
},
});
});
break;
Expand Down Expand Up @@ -736,7 +742,10 @@
} catch (e) {
console.error("error getting the relations", e);
await this.transport.reply<IWidgetApiErrorResponseData>(request, {
error: { message: "Unexpected error while reading relations" },
error: {
message: "Unexpected error while reading relations",
...(isMatrixError(e) && e),
},
});
}
}
Expand Down Expand Up @@ -779,7 +788,10 @@
} catch (e) {
console.error("error searching in the user directory", e);
await this.transport.reply<IWidgetApiErrorResponseData>(request, {
error: { message: "Unexpected error while searching in the user directory" },
error: {
message: "Unexpected error while searching in the user directory",
...(isMatrixError(e) && e),
},
});
}
}
Expand All @@ -801,7 +813,10 @@
} catch (e) {
console.error("error while getting the media configuration", e);
await this.transport.reply<IWidgetApiErrorResponseData>(request, {
error: { message: "Unexpected error while getting the media configuration" },
error: {
message: "Unexpected error while getting the media configuration",
...(isMatrixError(e) && e),
},
});
}
}
Expand All @@ -823,7 +838,10 @@
} catch (e) {
console.error("error while uploading a file", e);
await this.transport.reply<IWidgetApiErrorResponseData>(request, {
error: { message: "Unexpected error while uploading a file" },
error: {
message: "Unexpected error while uploading a file",
...(isMatrixError(e) && e),
},
});
}
}
Expand Down
20 changes: 17 additions & 3 deletions src/interfaces/IWidgetApiErrorResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,24 @@

import { IWidgetApiResponse, IWidgetApiResponseData } from "./IWidgetApiResponse";

interface IWidgetApiErrorData {
message: string;
}

interface IMatrixErrorData {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps there's a better place to put this, or a better way to have it as part of IWidgetApiErrorResponseData.

Also, are interface types still supposed to start with I?

httpStatus?: number;
errcode?: string;
}

AndrewFerr marked this conversation as resolved.
Show resolved Hide resolved
export interface IWidgetApiErrorResponseData extends IWidgetApiResponseData {
error: {
message: string;
};
error: IWidgetApiErrorData & IMatrixErrorData;
}

export function isMatrixError(err: unknown): err is IMatrixErrorData {
return typeof err === "object" && err !== null && (
"httpStatus" in err && typeof err.httpStatus === "number" ||
"errcode" in err && typeof err.errcode === "string"
);
}

export interface IWidgetApiErrorResponse extends IWidgetApiResponse {
Expand Down
16 changes: 15 additions & 1 deletion src/transport/PostmessageTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ interface IOutboundRequest {
reject: (err: Error) => void;
}

class MatrixError extends Error {
public constructor(
msg: string,
public readonly httpStatus?: number,
public readonly errcode?: string,
) {
super(msg);
}
}

/**
* Transport for the Widget API over postMessage.
*/
Expand Down Expand Up @@ -195,7 +205,11 @@ export class PostmessageTransport extends EventEmitter implements ITransport {

if (isErrorResponse(response.response)) {
const err = <IWidgetApiErrorResponseData>response.response;
req.reject(new Error(err.error.message));
req.reject(
err.error.httpStatus !== undefined || err.error.errcode !== undefined
? new MatrixError(err.error.message, err.error.httpStatus, err.error.errcode)
: new Error(err.error.message),
);
} else {
req.resolve(response);
}
Expand Down
Loading