From 26abbeadf9b2586bfe3893849872c69dad07f15a Mon Sep 17 00:00:00 2001 From: Andreas Domanowski <andreas@domanowski.net> Date: Thu, 2 Mar 2023 12:28:28 +0100 Subject: [PATCH] Exit script if importing fails --- import_md/hedgedoc_import.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/import_md/hedgedoc_import.py b/import_md/hedgedoc_import.py index 9ee907c..971e9b3 100644 --- a/import_md/hedgedoc_import.py +++ b/import_md/hedgedoc_import.py @@ -17,7 +17,11 @@ def import_single_document(instance_url, hedgedoc_free_url, content, session_id) req = urllib.request.Request(request_url, data=str.encode(content), method='POST', headers=headers) # unfortunately, no error is thrown if a document is not created when session cookie is invalid + # HTTP 409 is ignored for the sake of simplicity. Handled in import_into_hedgedoc(..) + # Not optimal, but nobody ain't got time for that with urllib.request.urlopen(req) as response: + if response.url == instance_url + "/": + raise SystemExit("Could not import document. Please check your HedgeDoc session cookie. Aborting...") return response.url @@ -75,8 +79,9 @@ def process_archive_export(archive_file, instance_url, lookup_map, session_id, u try_generate_free_url_document(document_content, document_title, instance_url, lookup_map, session_id, urls_to_visit) else: - print(f"According to your history, you did not visit \"{document_title}.md\" in the CodiMD " - "instance recently. Migrating the document and generating a new, random URL/path for it") + print( + f"According to your history (or lack thereof) , you did not visit \"{document_title}.md\" in the " + f"CodiMD instance recently. Migrating the document and generating a new, random URL/path for it") # empty string implies HedgeDoc should create a new ID generated_url = import_single_document(instance_url, "", document_content, session_id) print(f"New URL after document migration with new, random URL/subpath: " -- GitLab