Skip to content

Commit

Permalink
feat(errors): improve error handling and reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
goshatch committed Mar 1, 2024
1 parent 13a2049 commit 3bffce2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/main.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
;;; https://lispcookbook.github.io/cl-cookbook/scripting.html#with-sbcl---images-and-executables
(defun main ()
"Entry point for Homestead"
(format t "Initialising...~%")
(conf:init)
(format t "Building...~%")
(process-metadata-tree (load-metadata)))

(defun build-full-permalink (permalink &optional parent-permalink)
Expand Down
4 changes: 2 additions & 2 deletions src/node.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@
:if-exists :supersede
:if-does-not-exist :create)
(write-sequence output-html stream))
(format t "[OK] ~a~%" (a-get node :permalink)))
(format t "~a ~a~%" (util:ansi-green " [OK]") (util:ansi-strong (a-get node :permalink))))
(error (err)
(format t "[ERROR] processing failed: ~a (~a)~%" (a-get node :permalink) err))))
(format t "~a ~a:~%~8T~a~%" (util:ansi-red "[ERROR]") (util:ansi-strong (a-get node :permalink)) err))))
37 changes: 26 additions & 11 deletions src/util.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#:join
#:slurp
#:merge-plists
#:ansi-red
#:ansi-green
#:ansi-strong
#:build-path
#:build-output-path
#:find-file-path
Expand All @@ -19,17 +22,17 @@

(defun slurp (pathname)
"Reads the entire contents of the file at PATHNAME and returns it as a string.
NIL is returned if the file at PATHNAME cannot be accessed."
(when pathname
(if (probe-file pathname)
(with-open-file (stream pathname
:direction :input
:if-does-not-exist nil)
(with-output-to-string (out)
(loop for line = (read-line stream nil nil)
while line do (write-string line out)
(write-char #\Newline out))))
nil)))
An error of the type file-error is signaled if the file at PATHNAME does not
exist."
(if (probe-file pathname)
(with-open-file (stream pathname
:direction :input
:if-does-not-exist :error)
(with-output-to-string (out)
(loop for line = (read-line stream nil nil)
while line do (write-string line out)
(write-char #\Newline out))))
nil))

(defun merge-plists (p1 p2)
"Merge two plists, P1 and P2. Values from P2 override values from P1."
Expand All @@ -38,6 +41,18 @@
do (setf (getf result key) value))
result))

(defun ansi-red (string)
"Wrap STRING in ANSI escape codes for red text"
(format nil "~c[31m~a~c[0m" #\Esc string #\Esc))

(defun ansi-green (string)
"Wrap STRING in ANSI escape codes for green text"
(format nil "~c[32m~a~c[0m" #\Esc string #\Esc))

(defun ansi-strong (string)
"Wrap STRING in ANSI escape codes for strong text."
(format nil "~c[1;37m~a~c[0m" #\Esc string #\Esc))

(defun build-path (permalink &optional (extension "html"))
"Return path for file with extension EXTENSION representing PERMALINK"
(let ((permalink (if (string= permalink "/") "index" permalink)))
Expand Down

0 comments on commit 3bffce2

Please sign in to comment.