From d397b32328a60f818749a31d6636302df045714b Mon Sep 17 00:00:00 2001 From: Johan Sjolen Date: Sun, 21 May 2017 16:24:44 +0200 Subject: [PATCH] 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. --- plugins/static-pages.lisp | 5 ++++- src/packages.lisp | 5 ++++- src/util.lisp | 20 ++++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/plugins/static-pages.lisp b/plugins/static-pages.lisp index f83849e..cdd0195 100644 --- a/plugins/static-pages.lisp +++ b/plugins/static-pages.lisp @@ -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)))) diff --git a/src/packages.lisp b/src/packages.lisp index d49035f..fe07a90 100644 --- a/src/packages.lisp +++ b/src/packages.lisp @@ -46,4 +46,7 @@ #:add-document #:delete-document #:write-document - #:content-text)) + #:content-text + + ;; Error reporting + #:assert-field)) diff --git a/src/util.lisp b/src/util.lisp index 250d195..e24e7a6 100644 --- a/src/util.lisp +++ b/src/util.lisp @@ -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))