From a34426a7f663ccb397dd1abc6c5339fcc592fe12 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 25 Jan 2022 10:45:39 +0000 Subject: [PATCH] Improve signature of MatrixClient::isUsernameAvailable to not rely on throwing (#2130) --- src/client.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/client.ts b/src/client.ts index 8eca1797c..a7b13ed9f 100644 --- a/src/client.ts +++ b/src/client.ts @@ -6618,13 +6618,18 @@ export class MatrixClient extends EventEmitter { * Check whether a username is available prior to registration. An error response * indicates an invalid/unavailable username. * @param {string} username The username to check the availability of. - * @return {Promise} Resolves: to `true`. + * @return {Promise} Resolves: to boolean of whether the username is available. */ - public isUsernameAvailable(username: string): Promise { + public isUsernameAvailable(username: string): Promise { return this.http.authedRequest<{ available: true }>( - undefined, Method.Get, '/register/available', { username: username }, + undefined, Method.Get, '/register/available', { username }, ).then((response) => { return response.available; + }).catch(response => { + if (response.errcode === "M_USER_IN_USE") { + return false; + } + return Promise.reject(response); }); }