[FIX] web: rpc: not parsable request response

Connect in your browser to a db on runbot with mail installed and wait
until the runbot put to sleep the db. After a while, a new request to
get im_status will be sent and nginx responds to it with an error 404
and an invalid JSON response. A traceback with a particularly bad
message is then shown. Here we improve the traceback message.

closes odoo/odoo#99430

Signed-off-by: Aaron Bohy (aab) <aab@odoo.com>
This commit is contained in:
Mathieu Duckerts-Antoine
2022-09-02 23:16:16 +02:00
parent d982c4f319
commit 5c4a54022b
2 changed files with 35 additions and 1 deletions
@@ -22,6 +22,8 @@ export class ConnectionLostError extends Error {}
export class ConnectionAbortedError extends Error {}
export class HTTPError extends Error {}
// -----------------------------------------------------------------------------
// Main RPC method
// -----------------------------------------------------------------------------
@@ -64,7 +66,18 @@ export function jsonrpc(env, rpcId, url, params, settings = {}) {
reject(new ConnectionLostError());
return;
}
const { error: responseError, result: responseResult } = JSON.parse(request.response);
let params;
try {
params = JSON.parse(request.response);
} catch (_) {
reject(
new HTTPError(
`server responded with invalid JSON response (HTTP${request.status}): ${request.response}`
)
);
return;
}
const { error: responseError, result: responseResult } = params;
if (!settings.silent) {
bus.trigger("RPC:RESPONSE", data.id);
}
@@ -252,3 +252,24 @@ QUnit.test("check connection aborted", async (assert) => {
connection.abort();
assert.rejects(connection, ConnectionAbortedError);
});
QUnit.test(
"Response with status 404 and invalid JSON response result in a rerror with a readable message",
async (assert) => {
const env = await makeTestEnv({ serviceRegistry });
const MockXHR = makeMockXHR({}, () => {});
const request = new MockXHR();
request.response = "<h...";
request.status = "404";
try {
await env.services.rpc("/test/", null, { xhr: request });
} catch (_e) {
assert.strictEqual(
_e.message,
"server responded with invalid JSON response (HTTP404): <h..."
);
}
}
);