Skip to content

Commit

Permalink
Handle bad responses from backend RPC in proxyJson
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrichina committed Oct 21, 2024
1 parent 9fba77e commit 2c31b10
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions json-rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,27 @@ const proxyJson = async (ctx, { archival = false } = {}) => {
const nodeUrl = archival ? ARCHIVAL_NODE_URL : NODE_URL;
const rawBody = ctx.request.body ? JSON.stringify(ctx.request.body) : await getRawBody(ctx.req);
debug('proxyJson', ctx.request.method, ctx.request.path, nodeUrl, rawBody.toString('utf8'));

ctx.type = 'json';
ctx.body = Buffer.from(await (await fetch(`${nodeUrl}${ctx.request.path}`, {
const response = await fetch(`${nodeUrl}${ctx.request.path}`, {
method: ctx.request.method,
headers: {
'Content-Type': 'application/json'
},
body: ctx.request.method != 'GET' ? rawBody : undefined
})).arrayBuffer());
});

const contentType = response.headers.get('content-type');
debug('response', response.status, response.statusText, contentType);
if (!response.ok) {
ctx.throw(502, `Backend returned ${response.status} ${response.statusText}`);
}
if (!contentType || !contentType.includes('application/json')) {
ctx.throw(500, 'Invalid content type from backend: expected application/json');
}

ctx.status = response.status;
ctx.body = Buffer.from(await response.arrayBuffer());
}

const viewCallError = ({ id, message }) => {
Expand Down

0 comments on commit 2c31b10

Please sign in to comment.