Fix bug for xattrs + stdin feeds; Add user-friendly exception handling

Now exception's error message is printed to stderr, like a good neighbor.
This commit is contained in:
Jaidyn Ann 2022-12-27 11:38:28 -06:00
parent e21895cbfc
commit d9eb86434c

View File

@ -347,8 +347,9 @@
(main main) (main main)
(import scheme (import scheme
(chicken base) (chicken file) (chicken file posix) (chicken io) (chicken base) (chicken condition) (chicken file) (chicken file posix)
(chicken port) (chicken process-context) (chicken process-context posix) (chicken io) (chicken port) (chicken process-context)
(chicken process-context posix)
srfi-1 srfi-19 srfi-1 srfi-19
date-strings date-strings
feedsnake feedsnake-helpers feedsnake feedsnake-helpers
@ -358,9 +359,12 @@
(define *help-msg* (define *help-msg*
(string-append (string-append
"usage: feedsnake [-h] FILE...\n" "usage: feedsnake [-hn] [-s|S] [-o|d] FILE...\n"
"Feedsnake is a program for converting Atom feeds into mbox/maildir files.\n" "Feedsnake is a program for converting Atom feeds into mbox/maildir files.\n"
"Any Atom feeds passed as an argument will be output in mbox format.\n\n")) "Any Atom feeds passed as an argument will be output in mbox format.\n\n"
"If a FILE value is '-', or no FILE is provided, feedsnake will read a feed\n"
"from standard input. --since-last and similar arguments have no impact on\n"
"these feeds.\n\n"))
(define *opts* (define *opts*
@ -393,15 +397,38 @@
(write-string (usage *opts*) #f (open-output-file* fileno/stderr))) (write-string (usage *opts*) #f (open-output-file* fileno/stderr)))
(define-syntax exception-condom
(syntax-rules ()
((exception-condom expr)
(handle-exceptions exn
(begin
(write-string
(string-append (get-condition-property exn 'exn 'message)
" ("
(symbol->string (get-condition-property exn 'exn 'location))
")\n")
#f (open-output-file* fileno/stderr))
(exit 2))
expr))))
;; Just ignore whatever exceptions the expression throws our way
(define-syntax ignore-errors
(syntax-rules ()
((ignore-errors expr)
(handle-exceptions exn #f expr))))
;; The `main` procedure that should be called to run feedsnake-unix for use as script. ;; The `main` procedure that should be called to run feedsnake-unix for use as script.
(define (main) (define (main)
(let* ([args (getopt-long (command-line-arguments) *opts*)] (exception-condom
[free-args (alist-ref '@ args)]) (let* ([args (getopt-long (command-line-arguments) *opts*)]
(if (alist-ref 'help args) [free-args (alist-ref '@ args)])
(help) (if (alist-ref 'help args)
(map (lambda (feed-pair) (help)
(process-feed args feed-pair)) (map (lambda (feed-pair)
(get-feeds free-args))))) (process-feed args feed-pair))
(get-feeds free-args))))))
;; Turn the scripts free-args into parsed Feedsnake feed alists ;; Turn the scripts free-args into parsed Feedsnake feed alists
@ -432,7 +459,8 @@
[filter (entry-filter feed-pair args)]) [filter (entry-filter feed-pair args)])
;; Save the parsing date, unless the user doesn't want that ;; Save the parsing date, unless the user doesn't want that
(if (not (alist-ref 'no-save-date args)) (if (and (file-exists? (first feed-pair))
(not (alist-ref 'no-save-date args)))
(set-xattr (first feed-pair) "user.feedsnake.parsed" (set-xattr (first feed-pair) "user.feedsnake.parsed"
(date->rfc339-string (current-date-utc)))) (date->rfc339-string (current-date-utc))))
@ -455,11 +483,10 @@
[entry-date (lambda (entry) [entry-date (lambda (entry)
(or (alist-car-ref 'updated entry) (or (alist-car-ref 'updated entry)
(alist-car-ref 'published entry)))] (alist-car-ref 'published entry)))]
[last-update (or [last-string (or (ignore-errors
(date->utc-date (get-xattr (first feed-pair) "user.feedsnake.parsed"))
(rfc339-string->date "1971-01-01T00:00:00Z")]
(get-xattr (first feed-pair) "user.feedsnake.parsed"))) [last-update (rfc339-string->date last-string)])
(date->utc-date (make-date 0 0 0 0 01 01 1971)))])
(lambda (entry) (lambda (entry)
(cond [since (cond [since
(date>=? (entry-date entry) since)] (date>=? (entry-date entry) since)]