Specify what errors are ignored in options

This commit is contained in:
RMidhunSuresh 2022-02-01 15:38:55 +05:30
parent 420c12f202
commit bb6a885116
3 changed files with 11 additions and 16 deletions

View File

@ -58,15 +58,6 @@ export class HomeServerApi {
private _baseRequest(method: RequestMethod, url: string, queryParams?: Record<string, any>, body?: Record<string, any>, options?: IRequestOptions, accessToken?: string): IHomeServerRequest { private _baseRequest(method: RequestMethod, url: string, queryParams?: Record<string, any>, body?: Record<string, any>, options?: IRequestOptions, accessToken?: string): IHomeServerRequest {
const queryString = encodeQueryParams(queryParams); const queryString = encodeQueryParams(queryParams);
url = `${url}?${queryString}`; 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"]; let encodedBody: EncodedBody["body"];
const headers: Map<string, string | number> = new Map(); const headers: Map<string, string | number> = new Map();
if (accessToken) { if (accessToken) {
@ -89,7 +80,7 @@ export class HomeServerApi {
cache: options?.cache ?? false cache: options?.cache ?? false
}); });
const hsRequest = new HomeServerRequest(method, url, requestResult, log); const hsRequest = new HomeServerRequest(method, url, requestResult, options);
if (this._reconnector) { if (this._reconnector) {
hsRequest.response().catch(err => { 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 // 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 // see https://github.com/matrix-org/synapse/issues/7722
const _options = options ?? {}; const _options = options ?? {};
Object.assign(_options, { cache: true }); Object.assign(_options, { cache: true, allowedErrors: [401] });
const body = { const body = {
auth, auth,
password, password,

View File

@ -18,6 +18,7 @@ limitations under the License.
import {HomeServerError, ConnectionError} from "../error.js"; import {HomeServerError, ConnectionError} from "../error.js";
import type {RequestResult} from "../../platform/web/dom/request/fetch.js"; import type {RequestResult} from "../../platform/web/dom/request/fetch.js";
import type {ILogItem} from "../../logging/types"; import type {ILogItem} from "../../logging/types";
import type {IRequestOptions} from "../../platform/types/types.js";
export interface IHomeServerRequest { export interface IHomeServerRequest {
abort(): void; 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 // as we add types for expected responses from hs, this could be a generic class instead
private readonly _promise: Promise<any>; private readonly _promise: Promise<any>;
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._log = log;
this._sourceRequest = sourceRequest; this._sourceRequest = sourceRequest;
this._promise = sourceRequest.response().then(response => { this._promise = sourceRequest.response().then(response => {
log?.set("status", response.status); log?.set("status", response.status);
// ok? // ok?
// todo: register endpoint indicates our progress in using the user interactive if (response.status >= 200 && response.status < 300 || options?.allowedErrors?.find(e => e === response.status)) {
// authentication using 401 responses
// passing through all 401 responses as a temporary fix
if (response.status >= 200 && response.status < 300 || response.status === 401) {
log?.finish(); log?.finish();
return response.body; return response.body;
} else { } else {

View File

@ -28,6 +28,7 @@ export interface IRequestOptions {
prefix?: string; prefix?: string;
method?: string; method?: string;
format?: string; format?: string;
allowedErrors?: number[];
} }
export type RequestFunction = (url: string, options: IRequestOptions) => RequestResult; export type RequestFunction = (url: string, options: IRequestOptions) => RequestResult;