From 750f0ef5b146ed6e42ac1c922badf9c21b10c3ff Mon Sep 17 00:00:00 2001
From: Yash Rajpal <58601732+yash-rajpal@users.noreply.github.com>
Date: Thu, 16 Jun 2022 09:25:21 +0530
Subject: [PATCH] Regression: TOTP Modal with new rest api package (#25893)

<!-- This is a pull request template, you do not need to uncomment or remove the comments, they won't show up in the PR text. -->

<!-- Your Pull Request name should start with one of the following tags
  [NEW] For new features
  [IMPROVE] For an improvement (performance or little improvements) in existing features
  [FIX] For bug fixes that affect the end-user
  [BREAK] For pull requests including breaking changes
  Chore: For small tasks
  Doc: For documentation
-->

<!-- Checklist!!! If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.
  - I have read the Contributing Guide - https://github.com/RocketChat/Rocket.Chat/blob/develop/.github/CONTRIBUTING.md#contributing-to-rocketchat doc
  - I have signed the CLA - https://cla-assistant.io/RocketChat/Rocket.Chat
  - Lint and unit tests pass locally with my changes
  - I have added tests that prove my fix is effective or that my feature works (if applicable)
  - I have added necessary documentation (if applicable)
  - Any dependent changes have been merged and published in downstream modules
-->

## Proposed changes (including videos or screenshots)
<!-- CHANGELOG -->
<!--
  Describe the big picture of your changes here to communicate to the maintainers why we should accept this pull request.
  If it fixes a bug or resolves a feature request, be sure to link to that issue below.
  This description will appear in the release notes if we accept the contribution.
-->

<!-- END CHANGELOG -->

## Issue(s)
<!-- Link the issues being closed by or related to this PR. For example, you can use #594 if this PR closes issue number 594 -->

## Steps to test or reproduce
<!-- Mention how you would reproduce the bug if not mentioned on the issue page already. Also mention which screens are going to have the changes if applicable -->

## Further comments
<!-- If this is a relatively large or complex change, kick off the discussion by explaining why you chose the solution you did and what alternatives you considered, etc... -->


Co-authored-by: Guilherme Gazzo <5263975+ggazzo@users.noreply.github.com>
---
 apps/meteor/app/utils/client/lib/RestApiClient.ts | 13 +++++++++----
 packages/api-client/src/index.ts                  |  5 +++++
 2 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/apps/meteor/app/utils/client/lib/RestApiClient.ts b/apps/meteor/app/utils/client/lib/RestApiClient.ts
index 0621fe9850d..38b2dc44b5e 100644
--- a/apps/meteor/app/utils/client/lib/RestApiClient.ts
+++ b/apps/meteor/app/utils/client/lib/RestApiClient.ts
@@ -28,11 +28,16 @@ export const APIClient = new RestApiClient({
 	baseUrl: baseURI.replace(/\/$/, ''),
 });
 
-APIClient.use(function (request, next) {
+APIClient.use(async function (request, next) {
 	try {
-		return next(...request);
-	} catch (e) {
-		return new Promise((resolve, reject) => {
+		return await next(...request);
+	} catch (error) {
+		if (!(error instanceof Response) || error.status !== 400) {
+			throw error;
+		}
+
+		return new Promise(async (resolve, reject) => {
+			const e = await error.json();
 			process2faReturn({
 				error: e,
 				result: null,
diff --git a/packages/api-client/src/index.ts b/packages/api-client/src/index.ts
index 90905a7812b..c75b39da3b3 100644
--- a/packages/api-client/src/index.ts
+++ b/packages/api-client/src/index.ts
@@ -161,6 +161,11 @@ export class RestClient implements RestClientInterface {
 			...options,
 			headers: { ...this.getCredentialsAsHeaders(), ...this.headers, ...headers },
 			method,
+		}).then(function (response) {
+			if (!response.ok) {
+				return Promise.reject(response);
+			}
+			return response;
 		});
 	}
 
-- 
GitLab