Skip to content

Commit 994a8b2

Browse files
committed
Pass HTTP status code & errcode from CS-API errors
1 parent a294bb6 commit 994a8b2

File tree

3 files changed

+59
-13
lines changed

3 files changed

+59
-13
lines changed

src/ClientWidgetApi.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { WidgetApiDirection } from "./interfaces/WidgetApiDirection";
2222
import { IWidgetApiRequest, IWidgetApiRequestEmptyData } from "./interfaces/IWidgetApiRequest";
2323
import { IContentLoadedActionRequest } from "./interfaces/ContentLoadedAction";
2424
import { WidgetApiFromWidgetAction, WidgetApiToWidgetAction } from "./interfaces/WidgetApiAction";
25-
import { IWidgetApiErrorResponseData } from "./interfaces/IWidgetApiErrorResponse";
25+
import { IWidgetApiErrorResponseData, isMatrixError } from "./interfaces/IWidgetApiErrorResponse";
2626
import { Capability, MatrixCapabilities } from "./interfaces/Capabilities";
2727
import { IOpenIDUpdate, ISendEventDetails, ISendDelayedEventDetails, WidgetDriver } from "./driver/WidgetDriver";
2828
import {
@@ -554,10 +554,13 @@ export class ClientWidgetApi extends EventEmitter {
554554
delay_id: sentEvent.delayId,
555555
}),
556556
});
557-
}).catch(e => {
557+
}).catch((e: unknown) => {
558558
console.error("error sending event: ", e);
559559
return this.transport.reply<IWidgetApiErrorResponseData>(request, {
560-
error: {message: "Error sending event"},
560+
error: {
561+
message: "Error sending event",
562+
...(isMatrixError(e) && e),
563+
},
561564
});
562565
});
563566
}
@@ -581,10 +584,13 @@ export class ClientWidgetApi extends EventEmitter {
581584
case UpdateDelayedEventAction.Send:
582585
this.driver.updateDelayedEvent(request.data.delay_id, request.data.action).then(() => {
583586
return this.transport.reply<IWidgetApiAcknowledgeResponseData>(request, {});
584-
}).catch(e => {
587+
}).catch((e: unknown) => {
585588
console.error("error updating delayed event: ", e);
586589
return this.transport.reply<IWidgetApiErrorResponseData>(request, {
587-
error: {message: "Error updating delayed event"},
590+
error: {
591+
message: "Error updating delayed event",
592+
...(isMatrixError(e) && e),
593+
},
588594
});
589595
});
590596
break;
@@ -736,7 +742,10 @@ export class ClientWidgetApi extends EventEmitter {
736742
} catch (e) {
737743
console.error("error getting the relations", e);
738744
await this.transport.reply<IWidgetApiErrorResponseData>(request, {
739-
error: { message: "Unexpected error while reading relations" },
745+
error: {
746+
message: "Unexpected error while reading relations",
747+
...(isMatrixError(e) && e),
748+
},
740749
});
741750
}
742751
}
@@ -779,7 +788,10 @@ export class ClientWidgetApi extends EventEmitter {
779788
} catch (e) {
780789
console.error("error searching in the user directory", e);
781790
await this.transport.reply<IWidgetApiErrorResponseData>(request, {
782-
error: { message: "Unexpected error while searching in the user directory" },
791+
error: {
792+
message: "Unexpected error while searching in the user directory",
793+
...(isMatrixError(e) && e),
794+
},
783795
});
784796
}
785797
}
@@ -801,7 +813,10 @@ export class ClientWidgetApi extends EventEmitter {
801813
} catch (e) {
802814
console.error("error while getting the media configuration", e);
803815
await this.transport.reply<IWidgetApiErrorResponseData>(request, {
804-
error: { message: "Unexpected error while getting the media configuration" },
816+
error: {
817+
message: "Unexpected error while getting the media configuration",
818+
...(isMatrixError(e) && e),
819+
},
805820
});
806821
}
807822
}
@@ -823,7 +838,10 @@ export class ClientWidgetApi extends EventEmitter {
823838
} catch (e) {
824839
console.error("error while uploading a file", e);
825840
await this.transport.reply<IWidgetApiErrorResponseData>(request, {
826-
error: { message: "Unexpected error while uploading a file" },
841+
error: {
842+
message: "Unexpected error while uploading a file",
843+
...(isMatrixError(e) && e),
844+
},
827845
});
828846
}
829847
}

src/interfaces/IWidgetApiErrorResponse.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,24 @@
1616

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

19+
interface IWidgetApiErrorData {
20+
message: string;
21+
}
22+
23+
interface IMatrixErrorData {
24+
httpStatus?: number;
25+
errcode?: string;
26+
}
27+
1928
export interface IWidgetApiErrorResponseData extends IWidgetApiResponseData {
20-
error: {
21-
message: string;
22-
};
29+
error: IWidgetApiErrorData & IMatrixErrorData;
30+
}
31+
32+
export function isMatrixError(err: unknown): err is IMatrixErrorData {
33+
return typeof err === "object" && err !== null && (
34+
"httpStatus" in err && typeof err.httpStatus === "number" ||
35+
"errcode" in err && typeof err.errcode === "string"
36+
);
2337
}
2438

2539
export interface IWidgetApiErrorResponse extends IWidgetApiResponse {

src/transport/PostmessageTransport.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ interface IOutboundRequest {
3535
reject: (err: Error) => void;
3636
}
3737

38+
class MatrixError extends Error {
39+
public constructor(
40+
msg: string,
41+
public readonly httpStatus?: number,
42+
public readonly errcode?: string,
43+
) {
44+
super(msg);
45+
}
46+
}
47+
3848
/**
3949
* Transport for the Widget API over postMessage.
4050
*/
@@ -195,7 +205,11 @@ export class PostmessageTransport extends EventEmitter implements ITransport {
195205

196206
if (isErrorResponse(response.response)) {
197207
const err = <IWidgetApiErrorResponseData>response.response;
198-
req.reject(new Error(err.error.message));
208+
req.reject(
209+
err.error.httpStatus !== undefined || err.error.errcode !== undefined
210+
? new MatrixError(err.error.message, err.error.httpStatus, err.error.errcode)
211+
: new Error(err.error.message),
212+
);
199213
} else {
200214
req.resolve(response);
201215
}

0 commit comments

Comments
 (0)