From bb6a885116bd72d64c52679589c8cde682b1926c Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Tue, 1 Feb 2022 15:38:55 +0530 Subject: [PATCH] Specify what errors are ignored in options --- src/matrix/net/HomeServerApi.ts | 13 ++----------- src/matrix/net/HomeServerRequest.ts | 13 ++++++++----- src/platform/types/types.ts | 1 + 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/src/matrix/net/HomeServerApi.ts b/src/matrix/net/HomeServerApi.ts index 6a0e0bca..a722da30 100644 --- a/src/matrix/net/HomeServerApi.ts +++ b/src/matrix/net/HomeServerApi.ts @@ -58,15 +58,6 @@ export class HomeServerApi { private _baseRequest(method: RequestMethod, url: string, queryParams?: Record, body?: Record, options?: IRequestOptions, accessToken?: string): IHomeServerRequest { const queryString = encodeQueryParams(queryParams); url = `${url}?${queryString}`; - let log: ILogItem | undefined; - if (options?.log) { - const parent = options?.log; - log = parent.child({ - t: "network", - url, - method, - }, parent.level.Info); - } let encodedBody: EncodedBody["body"]; const headers: Map = new Map(); if (accessToken) { @@ -89,7 +80,7 @@ export class HomeServerApi { cache: options?.cache ?? false }); - const hsRequest = new HomeServerRequest(method, url, requestResult, log); + const hsRequest = new HomeServerRequest(method, url, requestResult, options); if (this._reconnector) { hsRequest.response().catch(err => { @@ -169,7 +160,7 @@ export class HomeServerApi { // todo: This is so that we disable cache-buster because it would cause the hs to respond with error // see https://github.com/matrix-org/synapse/issues/7722 const _options = options ?? {}; - Object.assign(_options, { cache: true }); + Object.assign(_options, { cache: true, allowedErrors: [401] }); const body = { auth, password, diff --git a/src/matrix/net/HomeServerRequest.ts b/src/matrix/net/HomeServerRequest.ts index d9c31275..cbbb4e90 100644 --- a/src/matrix/net/HomeServerRequest.ts +++ b/src/matrix/net/HomeServerRequest.ts @@ -18,6 +18,7 @@ limitations under the License. import {HomeServerError, ConnectionError} from "../error.js"; import type {RequestResult} from "../../platform/web/dom/request/fetch.js"; import type {ILogItem} from "../../logging/types"; +import type {IRequestOptions} from "../../platform/types/types.js"; export interface IHomeServerRequest { abort(): void; @@ -30,16 +31,18 @@ export class HomeServerRequest implements IHomeServerRequest { // as we add types for expected responses from hs, this could be a generic class instead private readonly _promise: Promise; - constructor(method: string, url: string, sourceRequest: RequestResult, log?: ILogItem) { + constructor(method: string, url: string, sourceRequest: RequestResult, options?: IRequestOptions) { + let log: ILogItem | undefined; + if (options?.log) { + const parent = options?.log; + log = parent.child({ t: "network", url, method, }, parent.level.Info); + } this._log = log; this._sourceRequest = sourceRequest; this._promise = sourceRequest.response().then(response => { log?.set("status", response.status); // ok? - // todo: register endpoint indicates our progress in using the user interactive - // authentication using 401 responses - // passing through all 401 responses as a temporary fix - if (response.status >= 200 && response.status < 300 || response.status === 401) { + if (response.status >= 200 && response.status < 300 || options?.allowedErrors?.find(e => e === response.status)) { log?.finish(); return response.body; } else { diff --git a/src/platform/types/types.ts b/src/platform/types/types.ts index 58d2216f..0ad569b4 100644 --- a/src/platform/types/types.ts +++ b/src/platform/types/types.ts @@ -28,6 +28,7 @@ export interface IRequestOptions { prefix?: string; method?: string; format?: string; + allowedErrors?: number[]; } export type RequestFunction = (url: string, options: IRequestOptions) => RequestResult;