Improve error handling.
This commit is contained in:
parent
ed33aca596
commit
16bd475be2
2 changed files with 58 additions and 33 deletions
|
@ -55,11 +55,11 @@ async def view_pubsub(request: Request):
|
||||||
if settings['service']:
|
if settings['service']:
|
||||||
if settings['include'] in pubsub or not settings['include']:
|
if settings['include'] in pubsub or not settings['include']:
|
||||||
if pubsub and node and item_id:
|
if pubsub and node and item_id:
|
||||||
iq = await xmpp.plugin['xep_0060'].get_item(pubsub, node, item_id)
|
iq = await get_node_item(pubsub, node, item_id)
|
||||||
|
if iq:
|
||||||
link = 'xmpp:{pubsub}?;node={node};item={item}'.format(
|
link = 'xmpp:{pubsub}?;node={node};item={item}'.format(
|
||||||
pubsub=pubsub, node=node, item=item_id)
|
pubsub=pubsub, node=node, item=item_id)
|
||||||
xml_atom = generate_rfc_4287(iq, link)
|
xml_atom = generate_rfc_4287(iq, link)
|
||||||
result = append_stylesheet(xml_atom)
|
|
||||||
iq = await get_node_items(pubsub, node)
|
iq = await get_node_items(pubsub, node)
|
||||||
if iq:
|
if iq:
|
||||||
generate_json(iq, node)
|
generate_json(iq, node)
|
||||||
|
@ -70,6 +70,11 @@ async def view_pubsub(request: Request):
|
||||||
filename = 'data/{}.json'.format(node)
|
filename = 'data/{}.json'.format(node)
|
||||||
with open(filename, 'w', encoding='utf-8') as f:
|
with open(filename, 'w', encoding='utf-8') as f:
|
||||||
json.dump(json_data, f, ensure_ascii=False, indent=4)
|
json.dump(json_data, f, ensure_ascii=False, indent=4)
|
||||||
|
else:
|
||||||
|
text = 'Please check that PubSub node and item are valid and accessible.'
|
||||||
|
xml_atom = error_message(text)
|
||||||
|
result = append_stylesheet(xml_atom)
|
||||||
|
|
||||||
# try:
|
# try:
|
||||||
# iq = await get_node_items(pubsub, node)
|
# iq = await get_node_items(pubsub, node)
|
||||||
# generate_json(iq, 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)
|
# json.dump(json_data, f, ensure_ascii=False, indent=4)
|
||||||
elif pubsub and node:
|
elif pubsub and node:
|
||||||
iq = await get_node_items(pubsub, node)
|
iq = await get_node_items(pubsub, node)
|
||||||
|
if iq:
|
||||||
link = form_a_link(pubsub, node)
|
link = form_a_link(pubsub, node)
|
||||||
xml_atom = generate_rfc_4287(iq, link)
|
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)
|
result = append_stylesheet(xml_atom)
|
||||||
elif pubsub:
|
elif pubsub:
|
||||||
iq = await xmpp.plugin['xep_0060'].get_nodes(pubsub)
|
iq = await get_nodes(pubsub)
|
||||||
|
if iq:
|
||||||
link = 'xmpp:{pubsub}'.format(pubsub=pubsub)
|
link = 'xmpp:{pubsub}'.format(pubsub=pubsub)
|
||||||
result = pubsub_to_opml(iq)
|
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:
|
elif node:
|
||||||
text = 'PubSub parameter is missing.'
|
text = 'PubSub parameter is missing.'
|
||||||
xml_atom = error_message(text)
|
xml_atom = error_message(text)
|
||||||
|
@ -130,11 +144,25 @@ def get_configuration(section):
|
||||||
return result
|
return result
|
||||||
|
|
||||||
#@timeout(5)
|
#@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):
|
async def get_node_items(pubsub, node):
|
||||||
try:
|
try:
|
||||||
iq = await xmpp.plugin['xep_0060'].get_items(pubsub, node, timeout=5)
|
iq = await xmpp.plugin['xep_0060'].get_items(pubsub, node, timeout=5)
|
||||||
return iq
|
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)
|
print(e)
|
||||||
|
|
||||||
def form_a_link(pubsub, node):
|
def form_a_link(pubsub, node):
|
||||||
|
|
|
@ -23,10 +23,10 @@ window.onload = async function(){
|
||||||
element.innerHTML = marked.parse(markDown);
|
element.innerHTML = marked.parse(markDown);
|
||||||
}
|
}
|
||||||
// Build a journal list
|
// Build a journal list
|
||||||
if (locationHref.searchParams.get('item')) {
|
itemsList = await openJson(node)
|
||||||
|
if (itemsList && locationHref.searchParams.get('item')) {
|
||||||
node = locationHref.searchParams.get('node')
|
node = locationHref.searchParams.get('node')
|
||||||
pubsub = locationHref.searchParams.get('pubsub')
|
pubsub = locationHref.searchParams.get('pubsub')
|
||||||
itemsList = await openJson(node)
|
|
||||||
let elementDiv = document.createElement('div');
|
let elementDiv = document.createElement('div');
|
||||||
elementDiv.id = 'journal';
|
elementDiv.id = 'journal';
|
||||||
let elementH3 = document.createElement('h3');
|
let elementH3 = document.createElement('h3');
|
||||||
|
@ -74,10 +74,14 @@ window.onload = async function(){
|
||||||
for (let xmppLink of document.querySelectorAll('a[href^="xmpp:"]')) {
|
for (let xmppLink of document.querySelectorAll('a[href^="xmpp:"]')) {
|
||||||
xmppUri = new URL(xmppLink);
|
xmppUri = new URL(xmppLink);
|
||||||
let parameters = xmppUri.search.split(';');
|
let parameters = xmppUri.search.split(';');
|
||||||
|
try {
|
||||||
let node = parameters.find(parameter => parameter.startsWith('node=')).split('=')[1];
|
let node = parameters.find(parameter => parameter.startsWith('node=')).split('=')[1];
|
||||||
let item = parameters.find(parameter => parameter.startsWith('item=')).split('=')[1];
|
let item = parameters.find(parameter => parameter.startsWith('item=')).split('=')[1];
|
||||||
let pubsub = xmppUri.pathname;
|
let pubsub = xmppUri.pathname;
|
||||||
xmppLink.href = `atom?pubsub=${pubsub}&node=${node}&item=${item}`
|
xmppLink.href = `atom?pubsub=${pubsub}&node=${node}&item=${item}`
|
||||||
|
} catch (err) {
|
||||||
|
console.warn(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// Display a selection of suggested software.
|
// Display a selection of suggested software.
|
||||||
const selection = {
|
const selection = {
|
||||||
|
@ -173,13 +177,6 @@ async function openJson(nodeId) {
|
||||||
return json;
|
return json;
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.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 };
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue