mirror of
https://github.com/karl0ss/homepage.git
synced 2025-04-29 12:03:41 +01:00
fix fatal jsonrpc error, error handling, add content-length
This commit is contained in:
parent
165add7f53
commit
ccfafe1b31
@ -15,22 +15,22 @@ export async function sendJsonRpcRequest(url, method, params, username, password
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (username && password) {
|
if (username && password) {
|
||||||
const authorization = Buffer.from(`${username}:${password}`).toString("base64");
|
headers.authorization = `Basic ${Buffer.from(`${username}:${password}`).toString("base64")}`;
|
||||||
headers.authorization = `Basic ${authorization}`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const client = new JSONRPCClient(async (rpcRequest) => {
|
const client = new JSONRPCClient(async (rpcRequest) => {
|
||||||
|
const body = JSON.stringify(rpcRequest);
|
||||||
|
headers['content-length'] = Buffer.byteLength(body);
|
||||||
const httpRequestParams = {
|
const httpRequestParams = {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers,
|
headers,
|
||||||
body: JSON.stringify(rpcRequest)
|
body
|
||||||
};
|
};
|
||||||
|
|
||||||
// eslint-disable-next-line no-unused-vars
|
// eslint-disable-next-line no-unused-vars
|
||||||
const [status, contentType, data] = await httpProxy(url, httpRequestParams);
|
const [status, contentType, data] = await httpProxy(url, httpRequestParams);
|
||||||
const dataString = data.toString();
|
|
||||||
if (status === 200) {
|
if (status === 200) {
|
||||||
const json = JSON.parse(dataString);
|
const json = JSON.parse(data.toString());
|
||||||
|
|
||||||
// in order to get access to the underlying error object in the JSON response
|
// in order to get access to the underlying error object in the JSON response
|
||||||
// you must set `result` equal to undefined
|
// you must set `result` equal to undefined
|
||||||
@ -40,7 +40,7 @@ export async function sendJsonRpcRequest(url, method, params, username, password
|
|||||||
return client.receive(json);
|
return client.receive(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Promise.reject(new Error(dataString));
|
return Promise.reject(data?.error ? data : new Error(data.toString()));
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -49,6 +49,7 @@ export async function sendJsonRpcRequest(url, method, params, username, password
|
|||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
if (e instanceof JSONRPCErrorException) {
|
if (e instanceof JSONRPCErrorException) {
|
||||||
|
logger.warn("Error calling JSONPRC endpoint: %s. %s", url, e.message);
|
||||||
return [200, "application/json", JSON.stringify({result: null, error: {code: e.code, message: e.message}})];
|
return [200, "application/json", JSON.stringify({result: null, error: {code: e.code, message: e.message}})];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,7 +74,7 @@ export default async function jsonrpcProxyHandler(req, res) {
|
|||||||
|
|
||||||
// eslint-disable-next-line no-unused-vars
|
// eslint-disable-next-line no-unused-vars
|
||||||
const [status, contentType, data] = await sendJsonRpcRequest(url, method, null, widget.username, widget.password);
|
const [status, contentType, data] = await sendJsonRpcRequest(url, method, null, widget.username, widget.password);
|
||||||
res.status(status).end(data);
|
return res.status(status).end(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user