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 {
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<string, string | number> = 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,

View file

@ -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<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._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 {

View file

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