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

Add support for helpful error text in stanzas #178

Merged
merged 3 commits into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/178.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Some errors now report helpful error text
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"lint": "tslint --project tsconfig.json --format stylish",
"start": "node build/src/Program.js -c config.yaml",
"genreg": "node build/src/Program.js -r -c config.yaml",
"pretest": "npm run build",
"test": "mocha -r ts-node/register test/test.ts test/*.ts test/**/*.ts",
"changelog": "scripts/towncrier.sh",
"coverage": "nyc mocha -r ts-node/register test/test.ts test/*.ts test/**/*.ts"
Expand Down
4 changes: 1 addition & 3 deletions src/GatewayHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,7 @@ export class GatewayHandler {
try {
const res = await this.bridge.getIntent().getClient().resolveRoomAlias(ev.roomAlias);
log.info(`Found ${res.room_id}`);
if (ev.onlyCheck) {
ev.result(null, res.room_id);
}
ev.result(null, res.room_id);
} catch (ex) {
log.warn("Room not found:", ex);
ev.result(Error("Room not found"));
Expand Down
3 changes: 1 addition & 2 deletions src/bifrost/Events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ export interface IGatewayRequest {
}

export interface IGatewayRoomQuery extends IGatewayRequest {
onlyCheck: boolean;
result: (err: Error|null, res?: string|IGatewayRoom) => void;
result: (err: Error|null, res?: string) => void;
}

export interface IGatewayPublicRoomsQuery extends IGatewayRequest {
Expand Down
10 changes: 5 additions & 5 deletions src/xmppjs/ServiceHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ export class ServiceHandler {
log.warn(`Failed to search rooms: ${ex}`);
// XXX: There isn't a very good way to explain why it failed,
// so we use service unavailable.
await this.xmpp.xmppSend(new SztaIqError(from, to, id, "cancel", 503, "service-unavailable"));
await this.xmpp.xmppSend(new SztaIqError(from, to, id, "cancel", 503, "service-unavailable", undefined,
`Failure fetching public rooms from ${homeserver}`));
return;
}

Expand All @@ -237,11 +238,10 @@ export class ServiceHandler {
await this.xmpp.xmppSend(response);
}

private queryRoom(roomAlias: string, onlyCheck: boolean = false): Promise<string|IGatewayRoom> {
private queryRoom(roomAlias: string): Promise<string|IGatewayRoom> {
return new Promise((resolve, reject) => {
this.xmpp.emit("gateway-queryroom", {
roomAlias,
onlyCheck,
result: (err, res) => {
if (err) {
reject(err);
Expand All @@ -262,7 +262,7 @@ export class ServiceHandler {
log.debug(`Running room discovery for ${toStr}`);
let roomId = this.existingAliases.get(alias);
if (!roomId) {
roomId = await this.queryRoom(alias, true) as string;
roomId = await this.queryRoom(alias) as string;
this.existingAliases.set(alias, roomId);
}
log.info(`Response for alias request ${toStr} (${alias}) -> ${roomId}`);
Expand All @@ -283,7 +283,7 @@ export class ServiceHandler {
});
await this.xmpp.xmppSend(discoInfo);
} catch (ex) {
await this.xmpp.xmppSend(new SztaIqError(toStr, from, id, "cancel", 404, "item-not-found"));
await this.xmpp.xmppSend(new SztaIqError(toStr, from, id, "cancel", 404, "item-not-found", undefined, "Room could not be found"));
}
}

Expand Down
12 changes: 9 additions & 3 deletions src/xmppjs/Stanzas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,17 @@ export class StzaPresenceError extends StzaPresence {
public by: string,
public errType: string,
public innerError: string,
public text?: string,

) {
super(from, to, id, "error");
}

public get presenceContent() {
return `<error type='${this.errType}' by='${this.by}'><${this.innerError}`
+ ` xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error>`;
+ " xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>"
+ (this.text ? `<text>${he.encode(this.text)}</text>` : "")
+ "</error>";
}
}

Expand Down Expand Up @@ -415,7 +418,8 @@ export class SztaIqError extends StzaBase {
private errorType: string,
private errorCode: number|null,
private innerError: string,
private by: string|null = null,
private by?: string,
private text?: string,
) {
super(from, to, id);
}
Expand All @@ -434,7 +438,9 @@ export class SztaIqError extends StzaBase {
}
return `<iq from='${this.from}' to='${this.to}' id='${this.id}' type='error' xml:lang='en'>`
+ `<error type='${this.errorType}'${errorParams}><${this.innerError} ` +
"xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error></iq>";
"xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>" +
(this.text ? `<text>${he.encode(this.text)}</text>` : "") +
"</error></iq>";
}
}

Expand Down
12 changes: 11 additions & 1 deletion test/xmppjs/test_Stanzas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ describe("Stanzas", () => {
});
});
describe("SztaIqError", () => {
it("should create a valid stanza", () => {
it("should create a an error", () => {
const xml = new SztaIqError("foo@bar", "baz@bar", "someid", "cancel", null, "not-acceptable", "foo").xml;
assertXML(xml);
expect(xml).to.equal(
Expand All @@ -104,4 +104,14 @@ describe("Stanzas", () => {
);
});
});
it("should create a an error with custom text", () => {
const xml = new SztaIqError("foo@bar", "baz@bar", "someid", "cancel", null, "not-acceptable", "foo", "Something isn't right").xml;
assertXML(xml);
expect(xml).to.equal(
"<iq from='foo@bar' to='baz@bar' id='someid' type='error' xml:lang='en'>" +
"<error type='cancel' by='foo'><not-acceptable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>" +
"<text>Something isn&#x27;t right</text>" +
"</error></iq>",
);
});
});