Report when a required field is missing in the static-pages plugin

Add ASSERT-FIELD to the API as the entry point for plugins to report to the
user when a content field is missing.
This commit is contained in:
Johan Sjolen 2017-05-21 16:24:44 +02:00 committed by Javier Olaechea
parent 7d87d483a0
commit d397b32328
3 changed files with 28 additions and 2 deletions

View file

@ -2,6 +2,7 @@
(:use :cl)
(:export #:enable)
(:import-from :coleslaw #:*config*
#:assert-field
#:content
#:find-all
#:render
@ -19,7 +20,9 @@
(:default-initargs :format :md))
(defmethod initialize-instance :after ((object page) &key)
(with-slots (coleslaw::url coleslaw::text format) object
(assert-field 'title object)
(assert-field 'coleslaw::url object)
(with-slots (coleslaw::url coleslaw::text format title) object
(setf coleslaw::url (make-pathname :defaults coleslaw::url)
format (alexandria:make-keyword (string-upcase format))
coleslaw::text (render-text coleslaw::text format))))

View file

@ -46,4 +46,7 @@
#:add-document
#:delete-document
#:write-document
#:content-text))
#:content-text
;; Error reporting
#:assert-field))

View file

@ -1,5 +1,25 @@
(in-package :coleslaw)
(define-condition coleslaw-condition ()
())
(define-condition field-missing (error coleslaw-condition)
((field-name :initarg :field-name :reader missing-field-field-name)
(file :initarg :file :reader missing-field-file
:documentation "The path of the file where the field is missing."))
(:report
(lambda (c s)
(format s "~A: The required field ~A is missing."
(missing-field-file c)
(missing-field-field-name c)))))
(defmacro assert-field (field-name content)
`(when (not (slot-boundp ,content ,field-name))
(error 'field-missing
:field-name ,field-name
:file (content-file ,content))))
(defun construct (class-name args)
"Create an instance of CLASS-NAME with the given ARGS."
(apply 'make-instance class-name args))