From 70fb6f4ab26e9fae67d7b7900a0c6ec132199112 Mon Sep 17 00:00:00 2001
From: Marc Ihm <marc@ihm.name>
Date: Sat, 12 Jun 2021 15:49:50 +0200
Subject: [PATCH] choices in org-id-find for handling missing ids

---
 lisp/org-id.el | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/lisp/org-id.el b/lisp/org-id.el
index 784f41ccd..d5b818f85 100644
--- a/lisp/org-id.el
+++ b/lisp/org-id.el
@@ -248,6 +248,27 @@ This variable is only relevant when `org-id-track-globally' is set."
   :group 'org-id
   :type 'boolean)
 
+(defcustom org-id-action-on-missing-id 'update
+  "Special action to be taken, if `org-id-find' cannot find an id.
+
+With 'update (default), org-id-find will silently call
+`org-id-update-id-locations' and try again; this will help,
+e.g. if you have moved the id to another file.
+
+With 'exception, the exception 'not-found will be thrown, which
+can be useful, if `org-id-find' is called from within lisp and
+the caller wants to handle the situation.
+
+With 'error, a descriptive error will be raised, causing the
+current command to terminate."
+  :group 'org-id
+  :type '(choice
+	  (const :tag "Run `org-id-update-id-locations' if an id cannot be found" update)
+	  (const :tag "Throw exception `not-found'" exception)
+	  (const :tag "Raise an error" error)
+          ))
+
+
 ;;; The API functions
 
 ;;;###autoload
@@ -347,7 +368,10 @@ With optional argument MARKERP, return the position as a new marker."
     (when file
       (setq where (org-id-find-id-in-file id file markerp)))
     (unless where
-      (org-id-update-id-locations nil t)
+      (cl-case org-id-action-on-missing-id
+        ('exception (throw 'not-found id))
+        ('error (error "Could not find id '%s'; but running `org-id-update-id-locations' might help" id))
+        (t (org-id-update-id-locations nil t)))
       (setq file (org-id-find-id-file id))
       (when file
 	(setq where (org-id-find-id-in-file id file markerp))))
-- 
2.30.2

