Skip to content

Commit ad8b185

Browse files
authored
Better error reporting (#64)
- In `el-patch--resolve-template`, include name of failed template in message. - Use `user-error` instead of `error` where appropriate. - Demote errors in `el-patch-define-and-eval-template` to warnings unless debugging.
1 parent 156c61b commit ad8b185

1 file changed

Lines changed: 25 additions & 16 deletions

File tree

el-patch-template.el

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ TABLE is a hashtable containing the bindings of `el-patch-let'"
148148
(mapcar
149149
(lambda (kv)
150150
(unless (symbolp (car kv))
151-
(error "Non-symbol (%s) as binding for `el-patch-let'"
152-
(car kv)))
151+
(user-error "Non-symbol (%s) as binding for `el-patch-let'"
152+
(car kv)))
153153
(list (car kv)
154154
(cons (cadr kv)
155155
;; The cdr is the resolution, nil for
@@ -373,7 +373,7 @@ remaining unmatched forms."
373373
(cons match template)
374374
template)
375375
remainder-form)
376-
('no-match
376+
(no-match
377377
;; Ultimately, the matching did not
378378
;; work, so undo the symbol resolution
379379
(puthash template old-entry table)
@@ -474,7 +474,7 @@ matched to a form in DEFINITION."
474474
(plist-get ptemplate :old))))
475475
(when matched
476476
(when matched-ptemplate
477-
(error "A form matches multiple templates"))
477+
(user-error "A form matches multiple templates"))
478478
(setq matched-forms-count matched
479479
matched-ptemplate ptemplate))))
480480
(cond
@@ -486,7 +486,7 @@ matched to a form in DEFINITION."
486486
ptemplates))
487487
definition))
488488
((plist-get matched-ptemplate :matched)
489-
(error "A template matches multiple forms"))
489+
(user-error "A template matches multiple forms"))
490490
((and (consp definition)
491491
(or
492492
(el-patch--any-template-p (car definition)
@@ -496,7 +496,7 @@ matched to a form in DEFINITION."
496496
(el-patch--any-template-p (cdr definition)
497497
ptemplates
498498
(1- matched-forms-count)))))
499-
(error "A form matching a template has subforms matching\
499+
(user-error "A form matching a template has subforms matching\
500500
other templates"))
501501
(t
502502
;; The old resolution of the template uniquely matches the definition
@@ -571,21 +571,28 @@ being patched; TYPE is a symbol `defun', `defmacro', etc."
571571
(templates (cdr template-def))
572572
(old-name (car (el-patch--resolve unresolved-name nil))))
573573
(unless template-def
574-
(error "The template definition of %S was not found" name))
574+
(user-error "Resolving `%s' template failed -- \
575+
Cannot find template definition" name))
575576
(let* ((definition (or (el-patch--locate (list type old-name))
576-
(error "Cannot find definition for `%s'"
577-
name)))
577+
(user-error "Resolving `%s' template failed --\
578+
Cannot find definition" name)))
578579
(ptemplates (mapcar
579580
(lambda (template)
580581
(list :template template
581582
:old (el-patch--partial-old-resolve template)
582583
:matched nil))
583584
templates))
584-
(patch (prog1 (el-patch--apply-template definition ptemplates)
585+
(patch (prog1
586+
(condition-case err-handle
587+
(el-patch--apply-template definition ptemplates)
588+
(error
589+
(user-error "Resolving `%s' template failed -- %s"
590+
name (cdr err-handle))))
585591
(cl-dolist (ptemplate ptemplates)
586592
(unless (plist-get ptemplate :matched)
587-
(error
588-
"At least one template did not match any form")))))
593+
(user-error
594+
"Resolving `%s' template failed -- at least \
595+
one template did not match any form in" name)))))
589596
(props (alist-get type el-patch-deftype-alist)))
590597
(cons (intern
591598
(or (plist-get props :macro-name)
@@ -699,8 +706,10 @@ if `el-patch-warn-on-eval-template' is non-nil, print a warning."
699706
(when el-patch-warn-on-eval-template
700707
(display-warning 'el-patch "Runtime evaluation of el-patch templates \
701708
can be slow, consider byte-compiling."))
702-
(el-patch-eval-template resolved-name
703-
(car qtype-name)))))
709+
(condition-case-unless-debug err
710+
(el-patch-eval-template resolved-name
711+
(car qtype-name))
712+
(error (display-warning 'el-patch (error-message-string err)))))))
704713

705714

706715
;; Stolen from `el-patch-validate'
@@ -745,8 +754,8 @@ See also `el-patch-validate-all'."
745754
(error
746755
(progn
747756
(display-warning 'el-patch
748-
(format "`%S' failed -- %s" name
749-
(cadr err-handle)))
757+
(format "`%s' failed -- %s" name
758+
(error-message-string err-handle)))
750759
nil)))
751760
(when run-hooks
752761
(run-hooks 'el-patch-post-validate-hook))))

0 commit comments

Comments
 (0)