From 550ab3afe1fb685b4882239e89625b89227a4d2a Mon Sep 17 00:00:00 2001 From: plugd Date: Sun, 3 Sep 2023 15:09:23 +0200 Subject: [PATCH] Exception handling. --- botbot.scm | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/botbot.scm b/botbot.scm index 456d3a7..187f0cb 100644 --- a/botbot.scm +++ b/botbot.scm @@ -21,6 +21,7 @@ (chicken string) (chicken pathname) (chicken process-context) + (chicken condition) (chicken irregex) matchable srfi-13 srfi-1 srfi-18 tcp6 openssl) @@ -45,7 +46,6 @@ (define (launch-bot) ;; (let-values (((in-port out-port) (tcp-connect host port))) - (load-bot) (let-values (((in-port out-port) (if usetls (ssl-connect* hostname: irc-host port: (or irc-port 6697)) @@ -75,8 +75,18 @@ (loop))))) (define (load-bot) - (set! bot-proc (eval (with-input-from-file bot-proc-file read))) - (print "Loaded bot procedure file.")) + (let ((new-bot-proc + (condition-case + (eval (with-input-from-file bot-proc-file read)) + (o (exn) + (print-error-message o) + #f)))) + (if new-bot-proc + (begin + (set! bot-proc new-bot-proc) + (print "Loaded bot procedure file.")) + (print "Error loading procedure file.")) + new-bot-proc)) (define (bot-loop in-port out-port) (let ((privmsg (lambda (to . args) @@ -93,15 +103,20 @@ ((source "PRIVMSG" target "bbreload") (when (and allow-reload (string=? target bot-nick)) (print "Reveived reload command from " source) - (load-bot) - (privmsg source "Reloaded bot script."))) + (if (load-bot) + (privmsg source "Reloaded bot script.") + (privmsg source "Error loading bot script.")))) ((source "PRIVMSG" target args ...) (when (string=? target bot-nick) (when (>= verbosity-level 1) (display "Received msg: ") (write msg) (newline)) - (bot-proc source args privmsg))) + (condition-case + (bot-proc source args privmsg) + (o (exn) + (print "Error executing bot script.") + (print-error-message o))))) (_ ;; Do nothing )) @@ -171,7 +186,7 @@ (print "Usage:\n" progname " [-h/--help]\n" progname " [-p/--port PORT] [--notls] [-c/--channnel CHANNEL]\n" - indent-str " [-v/--verbose] [-a/--allow-reload]" + indent-str " [-v/--verbose] [-a/--allow-reload]\n" indent-str " proc-file host nick"))) (define (main) @@ -215,7 +230,9 @@ (set! bot-proc-file procfile) (set! irc-host host) (set! bot-nick nick) - (launch-bot)) + (if (load-bot) + (launch-bot) + (error "Could not load bot procedure."))) (else (print "One or more invalid arguments.") (print-usage progname))))))))) -- 2.20.1