mirror of
https://github.com/bitwarden/browser
synced 2025-12-10 21:33:27 +00:00
[PM-10259] improve forwarder rpc error handling (#10330)
* only accept `application/json` responses
This commit is contained in:
@@ -48,7 +48,7 @@ export class RestClient {
|
|||||||
const message = await this.tryGetErrorMessage(response);
|
const message = await this.tryGetErrorMessage(response);
|
||||||
const key = message ? "forwaderInvalidTokenWithMessage" : "forwaderInvalidToken";
|
const key = message ? "forwaderInvalidTokenWithMessage" : "forwaderInvalidToken";
|
||||||
return [key, message];
|
return [key, message];
|
||||||
} else if (response.status === 429 || response.status >= 500) {
|
} else if (response.status >= 400) {
|
||||||
const message = await this.tryGetErrorMessage(response);
|
const message = await this.tryGetErrorMessage(response);
|
||||||
const key = message ? "forwarderError" : "forwarderUnknownError";
|
const key = message ? "forwarderError" : "forwarderUnknownError";
|
||||||
return [key, message];
|
return [key, message];
|
||||||
@@ -59,6 +59,7 @@ export class RestClient {
|
|||||||
const body = (await response.text()) ?? "";
|
const body = (await response.text()) ?? "";
|
||||||
|
|
||||||
// nullish continues processing; false returns undefined
|
// nullish continues processing; false returns undefined
|
||||||
|
// FIXME: inspect content-type header to determine extraction process
|
||||||
const error =
|
const error =
|
||||||
this.tryFindErrorAsJson(body) ?? this.tryFindErrorAsText(body) ?? response.statusText;
|
this.tryFindErrorAsJson(body) ?? this.tryFindErrorAsText(body) ?? response.statusText;
|
||||||
|
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ describe("CreateForwardingAddressRpc", () => {
|
|||||||
expect(result.cache).toEqual("no-store");
|
expect(result.cache).toEqual("no-store");
|
||||||
expect(result.method).toEqual("POST");
|
expect(result.method).toEqual("POST");
|
||||||
expect(result.headers.get("Content-Type")).toEqual("application/json");
|
expect(result.headers.get("Content-Type")).toEqual("application/json");
|
||||||
expect(result.headers.get("X-Requested-With")).toEqual("XMLHttpRequest");
|
expect(result.headers.get("Accept")).toEqual("application/json");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("provides the request and context to the rpc definition functions", () => {
|
it("provides the request and context to the rpc definition functions", () => {
|
||||||
|
|||||||
@@ -29,10 +29,8 @@ export class CreateForwardingAddressRpc<
|
|||||||
method: "POST",
|
method: "POST",
|
||||||
headers: new Headers({
|
headers: new Headers({
|
||||||
...token,
|
...token,
|
||||||
// X-Requested-With header required by some endpoints for
|
|
||||||
// detailed error descriptions (see #5565)
|
|
||||||
"X-Requested-With": "XMLHttpRequest",
|
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
|
Accept: "application/json",
|
||||||
}),
|
}),
|
||||||
body,
|
body,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ describe("GetAccountIdRpc", () => {
|
|||||||
expect(result.cache).toEqual("no-store");
|
expect(result.cache).toEqual("no-store");
|
||||||
expect(result.method).toEqual("GET");
|
expect(result.method).toEqual("GET");
|
||||||
expect(result.headers.get("Content-Type")).toEqual("application/json");
|
expect(result.headers.get("Content-Type")).toEqual("application/json");
|
||||||
|
expect(result.headers.get("Accept")).toEqual("application/json");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("provides the request and context to the rpc definition functions", () => {
|
it("provides the request and context to the rpc definition functions", () => {
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ export class GetAccountIdRpc<
|
|||||||
headers: new Headers({
|
headers: new Headers({
|
||||||
...token,
|
...token,
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
|
Accept: "application/json",
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user