Permalink
Browse files

Re-apply the error handling, removed by accident during the merge.

  • Loading branch information...
1 parent 43687bd commit a5f1895b069615093ca119114cf9257667497640 @ahyatt committed May 27, 2012
Showing with 56 additions and 26 deletions.
  1. +51 −24 websocket-test.el
  2. +5 −2 websocket.el
View
75 websocket-test.el
@@ -28,30 +28,6 @@
(require 'websocket)
(eval-when-compile (require 'cl))
-(defun websocket-test-get-filtered-response-with-error
- (outputs &optional callback)
- (let* ((packet-data nil)
- (websocket
- (make-websocket :conn "fake-conn"
- :filter (lambda (packet)
- (push packet packet-data)
- (when callback (funcall callback)))
- :close-callback (lambda (not-called) (assert nil))
- :url "ws://foo/bar")))
- err-list)
- (dolist (output outputs)
- (condition-case err
- (websocket-outer-filter websocket output)
- (error (push err err-list))))
- (list (nreverse packet-data) (nreverse err-list)))
-
-(defun websocket-test-get-filtered-response (outputs)
- (destructuring-bind (packet-data err-list)
- (websocket-test-get-filtered-response-with-error outputs)
- (assert (eq (length err-list) 0))
- packet-data))
-
-
(ert-deftest websocket-genbytes-length ()
(loop repeat 100
do (should (= (string-bytes (websocket-genbytes 16)) 16))))
@@ -262,3 +238,54 @@
(should (websocket-header-read-p fake-ws))
(websocket-outer-filter fake-ws (substring websocket-frames 2))
(should (equal (list frame2 frame1) processed-frames)))))
+
+(defun websocket-test-get-filtered-response-with-error
+ (frames &optional callback)
+ (let* ((filter-frames)
+ (websocket
+ (make-websocket :conn "fake-conn"
+ :filter (lambda (frame)
+ (push frame filter-frames)
+ (when callback (funcall callback)))
+ :close-callback (lambda (not-called) (assert nil))
+ :url "ws://foo/bar"
+ :accept-string t))
+ err-list)
+ (dolist (frame frames)
+ (condition-case err
+ (websocket-process-frame websocket frame)
+ (error (push err err-list))))
+ (list (nreverse filter-frames) (nreverse err-list))))
+
+(defun websocket-test-get-filtered-response (frames)
+ (destructuring-bind (filter-frames err-list)
+ (websocket-test-get-filtered-response-with-error frames)
+ (assert (eq (length err-list) 0))
+ filter-frames))
+
+(ert-deftest websocket-filter-handle-error-in-filter ()
+ (let ((foo-frame (make-websocket-frame :opcode 'text
+ :payload "foo"
+ :completep t))
+ (bar-frame (make-websocket-frame :opcode 'text
+ :payload "bar"
+ :completep t)))
+ (destructuring-bind (filter-frames err-list)
+ (websocket-test-get-filtered-response-with-error
+ (list foo-frame bar-frame)
+ (lambda () (error "See if websocket can handle this")))
+ (should (equal filter-frames (list foo-frame bar-frame)))
+ (should (equal err-list nil)))
+ (destructuring-bind (filter-frames err-list)
+ (websocket-test-get-filtered-response-with-error
+ (list foo-frame bar-frame)
+ (lambda () "Raise another type of error" (/ 1 0)))
+ (should (equal filter-frames (list foo-frame bar-frame)))
+ (should (equal err-list nil)))
+ (destructuring-bind (filter-frames err-list)
+ (websocket-test-get-filtered-response-with-error
+ (list foo-frame bar-frame)
+ (lambda () (error "See if websocket can handle this")))
+ (should (equal filter-frames (list foo-frame bar-frame)))
+ (should (equal err-list nil)))))
+
View
7 websocket.el
@@ -297,7 +297,7 @@ You can log errors by setting variable `websocket-debug' to t."
(get-buffer-create (format " *websocket %s debug*"
(websocket-url websocket))))
-(defun websocket-error (websocket msg &rest args)
+(defun websocket-error (msg &rest args)
"Report error message MSG."
(unless websocket-ignore-error
(apply 'message msg args))
@@ -336,7 +336,10 @@ slot of WEBSOCKET. If the frame is a ping, we reply with a pong.
If the frame is a close, we terminate the connection."
(let ((opcode (websocket-frame-opcode frame)))
(cond ((memq opcode '(continuation text binary))
- (funcall (websocket-filter websocket) frame))
+ (condition-case err
+ (funcall (websocket-filter websocket) frame)
+ (error (websocket-error "Got error from the filter function: %s"
+ (error-message-string err)))))
((eq opcode 'ping)
(websocket-send websocket
(make-websocket-frame :opcode 'pong :completep t)))

0 comments on commit a5f1895

Please sign in to comment.