diff --git a/org-anki.el b/org-anki.el index 5d16040..c152ec6 100644 --- a/org-anki.el +++ b/org-anki.el @@ -48,6 +48,16 @@ (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") +(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 (defcustom org-anki-default-deck nil @@ -161,9 +171,7 @@ with result." :error (cl-function (lambda (&key error-thrown &allow-other-keys) - (org-anki--report-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 @@ -173,7 +181,7 @@ with result." (if the-error (if on-error (funcall on-error the-error) - (org-anki--report-error "Unhandled error: %s" the-error)) + (signal 'org-anki-error '(the-error))) (funcall on-result the-result)))))))) (defun org-anki--get-current-tags (ids) @@ -274,7 +282,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,11 +379,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) - "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_))) @@ -401,7 +404,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 +481,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) + (signal 'org-anki-error-note-add (list error-msg)) (cond ;; added note ((equal "addNote" action-value) @@ -521,9 +524,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 - "Couldn't update note, received: %s" - the-error))))) + (signal 'org-anki-error-note-update '(the-error)))))) (defun org-anki--sync-notes (notes) ;; :: [Note] -> IO () @@ -579,9 +580,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 - "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 @@ -607,9 +606,7 @@ 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" - the-error)))))) + (signal 'org-anki-error-note-delete '(the-error))))))) (defun org-anki--get-model-fields (model) ;; :: String -> [FieldName] @@ -722,7 +719,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 +824,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)