From f55f4d17160bdfc0e7e5cf19ce95c2e1ffdaab1b Mon Sep 17 00:00:00 2001 From: Andrew Wong Date: Thu, 2 May 2024 19:13:52 -0400 Subject: [PATCH 1/4] =?UTF-8?q?Replaced=20=E2=80=98org-anki--report-error?= =?UTF-8?q?=E2=80=99=20calls=20with=20more=20correct=20=E2=80=98error?= =?UTF-8?q?=E2=80=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- org-anki.el | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/org-anki.el b/org-anki.el index 5d16040..cc1577a 100644 --- a/org-anki.el +++ b/org-anki.el @@ -48,6 +48,9 @@ (defconst org-anki-note-type "ANKI_NOTE_TYPE") (defconst org-anki-prop-global-tags "ANKI_TAGS") +;; Errors +(define-error org-anki-error "Org Anki error") + ;; Customizable variables (defcustom org-anki-default-deck nil @@ -161,7 +164,7 @@ with result." :error (cl-function (lambda (&key error-thrown &allow-other-keys) - (org-anki--report-error + (error "Can't connect to Anki: is the application running and is AnkiConnect installed?\n\nGot error: %s" (cdr error-thrown)))) @@ -173,7 +176,7 @@ with result." (if the-error (if on-error (funcall on-error the-error) - (org-anki--report-error "Unhandled error: %s" the-error)) + (error "Unhandled error: %s" the-error)) (funcall on-result the-result)))))))) (defun org-anki--get-current-tags (ids) @@ -274,7 +277,7 @@ with result." ((= fields-length (+ 1 found-length)) (let ((missing-field (car (-difference fields found-fields)))) `(,type ,@(plist-put found missing-field content)))) - (t (org-anki--report-error + (t (error "org-anki--get-fields: fields required: %s, fields found: %s, at character: %s" fields found-fields (point))))))) @@ -371,7 +374,8 @@ be removed from the Anki app, return actions that do that." (org-anki--string-to-anki-mathjax (org-export-string-as string 'html t '(:with-toc nil))))) -(defun org-anki--report-error (format &rest args) +(defun org-anki--report-error (format &rest args) ;this is not the correct way to signal errors, and + ;will not trigger debug if debug-on-error is set "FORMAT the ERROR and prefix it with `org-anki error'." (let ((fmt (concat "org-anki error: " format))) (apply #'message fmt args))) @@ -401,7 +405,7 @@ be removed from the Anki app, return actions that do that." ((stringp prop-item) prop-item) ((stringp prop-global) prop-global) ((stringp default) default) - (t (error "No property '%s' in item nor file nor set as default!" + (t (error "No property '%s' in item, file or default" name))))) (defun org-anki--get-match () @@ -478,7 +482,7 @@ be removed from the Anki app, return actions that do that." (ignore &rest) (if error-msg ;; report error - (org-anki--report-error "Couldn't add note, received error: %s" error-msg) + (error "Couldn't add note, received error: %s" error-msg) (cond ;; added note ((equal "addNote" action-value) @@ -521,7 +525,7 @@ be removed from the Anki app, return actions that do that." ) (-map 'org-anki--handle-pair sorted))) (lambda (the-error) - (org-anki--report-error + (error "Couldn't update note, received: %s" the-error))))) @@ -579,7 +583,7 @@ be removed from the Anki app, return actions that do that." "note succesfully updated: %s" (org-anki--note-maybe-id note))) (lambda (the-error) - (org-anki--report-error + (error "Couldn't update note, received: %s" the-error))) @@ -607,8 +611,8 @@ be removed from the Anki app, return actions that do that." (reverse notes)) ) (lambda (the-error) - (org-anki--report-error - "org-anki-delete-all error: %s" + (error + "Couldn't delete note, received error: %s" the-error)))))) (defun org-anki--get-model-fields (model) @@ -722,7 +726,7 @@ syntax." (lambda (_the-result) (org-anki--report "send request succesfully, please switch to anki")) (lambda (the-error) - (org-anki--report-error + (error "Browse error, received: %s" the-error) ))) @@ -827,9 +831,9 @@ Pandoc is required to be installed." (org-anki--write-note (org-anki--parse-note json name))) the-result)) (lambda (the-error) - (org-anki--report-error "Get deck error, received: %s" the-error)))) + (error "Get deck error, received: %s" the-error)))) (lambda (the-error) - (org-anki--report-error "Get deck error, received: %s" the-error))))) + (error "Get deck error, received: %s" the-error))))) (provide 'org-anki) From 49ad9e517d72a1a0e71463174351b0303f455b1a Mon Sep 17 00:00:00 2001 From: Andrew Wong Date: Thu, 2 May 2024 20:57:44 -0400 Subject: [PATCH 2/4] Added error symbols --- org-anki.el | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/org-anki.el b/org-anki.el index cc1577a..c05b6ce 100644 --- a/org-anki.el +++ b/org-anki.el @@ -49,7 +49,11 @@ (defconst org-anki-prop-global-tags "ANKI_TAGS") ;; Errors -(define-error org-anki-error "Org Anki error") + +(define-error 'org-anki-error "Org-Anki error") +(define-error 'org-anki-error-note-add "Could not add note" 'org-anki-error) +(define-error 'org-anki-error-note-delete "Could not delete note" 'org-anki-error) +(define-error 'org-anki-error-note-update "Could not update note" 'org-anki-error) ;; Customizable variables @@ -176,7 +180,7 @@ with result." (if the-error (if on-error (funcall on-error the-error) - (error "Unhandled error: %s" the-error)) + (signal 'org-anki-error '(the-error))) (funcall on-result the-result)))))))) (defun org-anki--get-current-tags (ids) @@ -482,7 +486,7 @@ be removed from the Anki app, return actions that do that." (ignore &rest) (if error-msg ;; report error - (error "Couldn't add note, received error: %s" error-msg) + (signal 'org-anki-error-note-add '(error-msg)) (cond ;; added note ((equal "addNote" action-value) @@ -525,9 +529,7 @@ be removed from the Anki app, return actions that do that." ) (-map 'org-anki--handle-pair sorted))) (lambda (the-error) - (error - "Couldn't update note, received: %s" - the-error))))) + (signal 'org-anki-error-note-update '(the-error)))))) (defun org-anki--sync-notes (notes) ;; :: [Note] -> IO () @@ -583,9 +585,7 @@ be removed from the Anki app, return actions that do that." "note succesfully updated: %s" (org-anki--note-maybe-id note))) (lambda (the-error) - (error - "Couldn't update note, received: %s" - the-error))) + (signal 'org-anki-error-note-update the-error))) ;; Update tags (if any) for the single note, too: (if notes-and-tag-actions2 @@ -611,9 +611,7 @@ be removed from the Anki app, return actions that do that." (reverse notes)) ) (lambda (the-error) - (error - "Couldn't delete note, received error: %s" - the-error)))))) + (signal 'org-anki-error-note-delete '(the-error))))))) (defun org-anki--get-model-fields (model) ;; :: String -> [FieldName] From 801bb293953e7c77c9faa2da5b87878f1e9c0a05 Mon Sep 17 00:00:00 2001 From: Andrew Wong Date: Thu, 2 May 2024 20:59:52 -0400 Subject: [PATCH 3/4] =?UTF-8?q?Removed=20unneeded=20=E2=80=98org-anki--rep?= =?UTF-8?q?ort-error=E2=80=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- org-anki.el | 6 ------ 1 file changed, 6 deletions(-) diff --git a/org-anki.el b/org-anki.el index c05b6ce..17cd7d5 100644 --- a/org-anki.el +++ b/org-anki.el @@ -378,12 +378,6 @@ be removed from the Anki app, return actions that do that." (org-anki--string-to-anki-mathjax (org-export-string-as string 'html t '(:with-toc nil))))) -(defun org-anki--report-error (format &rest args) ;this is not the correct way to signal errors, and - ;will not trigger debug if debug-on-error is set - "FORMAT the ERROR and prefix it with `org-anki error'." - (let ((fmt (concat "org-anki error: " format))) - (apply #'message fmt args))) - (defun org-anki--report (format_ &rest args) "FORMAT_ the ARGS and prefix it with `org-anki'." (let* ((fmt (concat "org-anki: " format_))) From 78f9a789e1818c87cb56beed730031a1d0c5f12e Mon Sep 17 00:00:00 2001 From: Andrew Wong Date: Fri, 3 May 2024 22:52:27 -0400 Subject: [PATCH 4/4] Restructured errors --- org-anki.el | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/org-anki.el b/org-anki.el index 17cd7d5..c152ec6 100644 --- a/org-anki.el +++ b/org-anki.el @@ -51,9 +51,12 @@ ;; Errors (define-error 'org-anki-error "Org-Anki error") -(define-error 'org-anki-error-note-add "Could not add note" 'org-anki-error) -(define-error 'org-anki-error-note-delete "Could not delete note" 'org-anki-error) -(define-error 'org-anki-error-note-update "Could not update note" 'org-anki-error) +(define-error 'org-anki-bad-get "Bad GET" 'org-anki-error) +(define-error 'org-anki-bad-get-connection + "Can't connect (is AnkiConnect running?)" 'org-anki-bad-get) +(define-error 'org-anki-bad-get-add "Could not add note" 'org-anki-bad-get) +(define-error 'org-anki-bad-get-delete "Could not delete note" 'org-anki-bad-get) +(define-error 'org-anki-bad-get-update "Could not update note" 'org-anki-bad-get) ;; Customizable variables @@ -168,9 +171,7 @@ with result." :error (cl-function (lambda (&key error-thrown &allow-other-keys) - (error - "Can't connect to Anki: is the application running and is AnkiConnect installed?\n\nGot error: %s" - (cdr error-thrown)))) + (signal 'org-anki-bad-get-connection (list (cdr error-thrown))))) :success (cl-function @@ -480,7 +481,7 @@ be removed from the Anki app, return actions that do that." (ignore &rest) (if error-msg ;; report error - (signal 'org-anki-error-note-add '(error-msg)) + (signal 'org-anki-error-note-add (list error-msg)) (cond ;; added note ((equal "addNote" action-value)