Improve error handling.

This commit is contained in:
Schimon Jehudah, Adv. 2024-07-11 19:01:45 +03:00
parent ed33aca596
commit 16bd475be2
2 changed files with 58 additions and 33 deletions

View file

@ -55,21 +55,26 @@ async def view_pubsub(request: Request):
if settings['service']:
if settings['include'] in pubsub or not settings['include']:
if pubsub and node and item_id:
iq = await xmpp.plugin['xep_0060'].get_item(pubsub, node, item_id)
link = 'xmpp:{pubsub}?;node={node};item={item}'.format(
pubsub=pubsub, node=node, item=item_id)
xml_atom = generate_rfc_4287(iq, link)
result = append_stylesheet(xml_atom)
iq = await get_node_items(pubsub, node)
iq = await get_node_item(pubsub, node, item_id)
if iq:
generate_json(iq, node)
link = 'xmpp:{pubsub}?;node={node};item={item}'.format(
pubsub=pubsub, node=node, item=item_id)
xml_atom = generate_rfc_4287(iq, link)
iq = await get_node_items(pubsub, node)
if iq:
generate_json(iq, node)
else:
operator = get_configuration('settings')['operator']
json_data = [{'title' : 'Timeout Error: Press here to contact the operator.',
'link' : 'xmpp:{}?message'.format(operator)}]
filename = 'data/{}.json'.format(node)
with open(filename, 'w', encoding='utf-8') as f:
json.dump(json_data, f, ensure_ascii=False, indent=4)
else:
operator = get_configuration('settings')['operator']
json_data = [{'title' : 'Timeout Error: Press here to contact the operator.',
'link' : 'xmpp:{}?message'.format(operator)}]
filename = 'data/{}.json'.format(node)
with open(filename, 'w', encoding='utf-8') as f:
json.dump(json_data, f, ensure_ascii=False, indent=4)
text = 'Please check that PubSub node and item are valid and accessible.'
xml_atom = error_message(text)
result = append_stylesheet(xml_atom)
# try:
# iq = await get_node_items(pubsub, node)
# generate_json(iq, node)
@ -82,13 +87,22 @@ async def view_pubsub(request: Request):
# json.dump(json_data, f, ensure_ascii=False, indent=4)
elif pubsub and node:
iq = await get_node_items(pubsub, node)
link = form_a_link(pubsub, node)
xml_atom = generate_rfc_4287(iq, link)
if iq:
link = form_a_link(pubsub, node)
xml_atom = generate_rfc_4287(iq, link)
else:
text = 'Please check that PubSub node is valid and accessible.'
xml_atom = error_message(text)
result = append_stylesheet(xml_atom)
elif pubsub:
iq = await xmpp.plugin['xep_0060'].get_nodes(pubsub)
link = 'xmpp:{pubsub}'.format(pubsub=pubsub)
result = pubsub_to_opml(iq)
iq = await get_nodes(pubsub)
if iq:
link = 'xmpp:{pubsub}'.format(pubsub=pubsub)
result = pubsub_to_opml(iq)
else:
text = 'Please check that PubSub Jabber ID is valid and accessible.'
xml_atom = error_message(text)
result = append_stylesheet(xml_atom)
elif node:
text = 'PubSub parameter is missing.'
xml_atom = error_message(text)
@ -130,11 +144,25 @@ def get_configuration(section):
return result
#@timeout(5)
async def get_node_item(pubsub, node, item_id):
try:
iq = await xmpp.plugin['xep_0060'].get_item(pubsub, node, item_id, timeout=5)
return iq
except (IqError, IqTimeout) as e:
print(e)
async def get_node_items(pubsub, node):
try:
iq = await xmpp.plugin['xep_0060'].get_items(pubsub, node, timeout=5)
return iq
except IqTimeout as e:
except (IqError, IqTimeout) as e:
print(e)
async def get_nodes(pubsub):
try:
await xmpp.plugin['xep_0060'].get_nodes(pubsub, timeout=5)
return iq
except (IqError, IqTimeout) as e:
print(e)
def form_a_link(pubsub, node):

View file

@ -23,10 +23,10 @@ window.onload = async function(){
element.innerHTML = marked.parse(markDown);
}
// Build a journal list
if (locationHref.searchParams.get('item')) {
itemsList = await openJson(node)
if (itemsList && locationHref.searchParams.get('item')) {
node = locationHref.searchParams.get('node')
pubsub = locationHref.searchParams.get('pubsub')
itemsList = await openJson(node)
let elementDiv = document.createElement('div');
elementDiv.id = 'journal';
let elementH3 = document.createElement('h3');
@ -74,10 +74,14 @@ window.onload = async function(){
for (let xmppLink of document.querySelectorAll('a[href^="xmpp:"]')) {
xmppUri = new URL(xmppLink);
let parameters = xmppUri.search.split(';');
let node = parameters.find(parameter => parameter.startsWith('node=')).split('=')[1];
let item = parameters.find(parameter => parameter.startsWith('item=')).split('=')[1];
let pubsub = xmppUri.pathname;
xmppLink.href = `atom?pubsub=${pubsub}&node=${node}&item=${item}`
try {
let node = parameters.find(parameter => parameter.startsWith('node=')).split('=')[1];
let item = parameters.find(parameter => parameter.startsWith('item=')).split('=')[1];
let pubsub = xmppUri.pathname;
xmppLink.href = `atom?pubsub=${pubsub}&node=${node}&item=${item}`
} catch (err) {
console.warn(err)
}
}
// Display a selection of suggested software.
const selection = {
@ -173,13 +177,6 @@ async function openJson(nodeId) {
return json;
})
.catch(err => {
throw new Error('Error: ' + err);
console.warn(err);
})
}
function parseXmppPubsubLink(link) {
const parts = link.split(';');
const node = parts.find(part => part.startsWith('node=')).split('=')[1];
const item = parts.find(part => part.startsWith('item=')).split('=')[1];
return { node, item };
}