diff --git a/stomp.el b/stomp.el index 55a6a7c..20e6c8d 100644 --- a/stomp.el +++ b/stomp.el @@ -23,6 +23,15 @@ (require 'map) +(define-error 'stomp-error + "Error encountered in implementation of the STOMP protocol") + +(define-error 'stomp-invalid-header + "Invalid STOMP frame header encounterd") + +(define-error 'stomp-invalid-frame + "Invalid STOMP frame encountered") + (defun stomp-send-frame (process frame) "Send a message to a STOMP process" (process-send-string process (stomp-frame-to-string frame))) @@ -84,7 +93,7 @@ (let ((header (split-string (thing-at-point 'line) ":"))) (cond ((= (line-end-position) end-point) headers) - ((> (length header) 2) (throw 'invalid-header nil)) + ((> (length header) 2) (signal 'stomp-invalid-header (list header))) (t (stomp-read-headers buffer end-point @@ -125,7 +134,7 @@ 'command key. Other optional keys are 'headers (another alist) and 'content" (unless (assq 'command frame) - (throw 'invalid-frame "A STOMP frame must define a command")) + (signal 'stomp-invalid-frame (list "Expected frame to have a command" frame))) (let ((headers (if (assq 'headers frame) (alist-get 'headers frame) '())) (content (if (assq 'content frame) (alist-get 'content frame) ""))) (format "%s\n%s\n%s\u0000" diff --git a/test/test.el b/test/test.el index 5c84768..504d8c0 100755 --- a/test/test.el +++ b/test/test.el @@ -107,3 +107,20 @@ (setq frame '((headers . (("okay" . "bye"))))) (should (equal nil (stomp-frame-header "bla" frame)))) + +(ert-deftest will-signal-invald-frame () + "`stomp-frame-to-string` should signal an invalid frame error +when attempting to send a frame without a command defined in it" + (should-error (stomp-frame-to-string '()) :type 'stomp-invalid-frame)) + +(ert-deftest will-signal-wrong-type-filter-function () + "`stomp-filter-function` should signal a `wrong-type-argument` + error when `callback` is not a callback" + (should-error (stomp-filter-function '(not a function) :type 'wrong-type-argument))) + +(ert-deftest will-signal-invalid-header () + "`stomp-read-headers` should signal a `stomp-invalid-frame` +error when a header containing colons is encountered" + (with-testing-buffer + "invalid_header_frame.txt" + (should-error (stomp-read-headers (current-buffer) (point-max)) :type 'stomp-invalid-header))) diff --git a/test/testdata/invalid_header_frame.txt b/test/testdata/invalid_header_frame.txt new file mode 100644 index 0000000..9e8a10e Binary files /dev/null and b/test/testdata/invalid_header_frame.txt differ