Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit a5f1895b069615093ca119114cf9257667497640 1 parent 43687bd
Andrew Hyatt authored
75  websocket-test.el
@@ -28,30 +28,6 @@
28 28
 (require 'websocket)
29 29
 (eval-when-compile (require 'cl))
30 30
 
31  
-(defun websocket-test-get-filtered-response-with-error
32  
-  (outputs &optional callback)
33  
-  (let* ((packet-data nil)
34  
-         (websocket
35  
-          (make-websocket :conn "fake-conn"
36  
-                          :filter (lambda (packet)
37  
-                                    (push packet packet-data)
38  
-                                    (when callback (funcall callback)))
39  
-                          :close-callback (lambda (not-called) (assert nil))
40  
-                          :url "ws://foo/bar")))
41  
-         err-list)
42  
-    (dolist (output outputs)
43  
-      (condition-case err
44  
-          (websocket-outer-filter websocket output)
45  
-        (error (push err err-list))))
46  
-    (list (nreverse packet-data) (nreverse err-list)))
47  
-
48  
-(defun websocket-test-get-filtered-response (outputs)
49  
-  (destructuring-bind (packet-data err-list)
50  
-      (websocket-test-get-filtered-response-with-error outputs)
51  
-    (assert (eq (length err-list) 0))
52  
-    packet-data))
53  
-
54  
-
55 31
 (ert-deftest websocket-genbytes-length ()
56 32
   (loop repeat 100
57 33
         do (should (= (string-bytes (websocket-genbytes 16)) 16))))
@@ -262,3 +238,54 @@
262 238
       (should (websocket-header-read-p fake-ws))
263 239
       (websocket-outer-filter fake-ws (substring websocket-frames 2))
264 240
       (should (equal (list frame2 frame1) processed-frames)))))
  241
+
  242
+(defun websocket-test-get-filtered-response-with-error
  243
+  (frames &optional callback)
  244
+  (let* ((filter-frames)
  245
+         (websocket
  246
+          (make-websocket :conn "fake-conn"
  247
+                          :filter (lambda (frame)
  248
+                                    (push frame filter-frames)
  249
+                                    (when callback (funcall callback)))
  250
+                          :close-callback (lambda (not-called) (assert nil))
  251
+                          :url "ws://foo/bar"
  252
+                          :accept-string t))
  253
+         err-list)
  254
+    (dolist (frame frames)
  255
+      (condition-case err
  256
+          (websocket-process-frame websocket frame)
  257
+        (error (push err err-list))))
  258
+    (list (nreverse filter-frames) (nreverse err-list))))
  259
+
  260
+(defun websocket-test-get-filtered-response (frames)
  261
+  (destructuring-bind (filter-frames err-list)
  262
+      (websocket-test-get-filtered-response-with-error frames)
  263
+    (assert (eq (length err-list) 0))
  264
+    filter-frames))
  265
+
  266
+(ert-deftest websocket-filter-handle-error-in-filter ()
  267
+  (let ((foo-frame (make-websocket-frame :opcode 'text
  268
+                                   :payload "foo"
  269
+                                   :completep t))
  270
+        (bar-frame (make-websocket-frame :opcode 'text
  271
+                                         :payload "bar"
  272
+                                         :completep t)))
  273
+    (destructuring-bind (filter-frames err-list)
  274
+        (websocket-test-get-filtered-response-with-error
  275
+         (list foo-frame bar-frame)
  276
+         (lambda () (error "See if websocket can handle this")))
  277
+      (should (equal filter-frames (list foo-frame bar-frame)))
  278
+      (should (equal err-list nil)))
  279
+    (destructuring-bind (filter-frames err-list)
  280
+      (websocket-test-get-filtered-response-with-error
  281
+       (list foo-frame bar-frame)
  282
+       (lambda () "Raise another type of error" (/ 1 0)))
  283
+    (should (equal filter-frames (list foo-frame bar-frame)))
  284
+    (should (equal err-list nil)))
  285
+    (destructuring-bind (filter-frames err-list)
  286
+      (websocket-test-get-filtered-response-with-error
  287
+       (list foo-frame bar-frame)
  288
+       (lambda () (error "See if websocket can handle this")))
  289
+    (should (equal filter-frames (list foo-frame bar-frame)))
  290
+    (should (equal err-list nil)))))
  291
+
7  websocket.el
@@ -297,7 +297,7 @@ You can log errors by setting variable `websocket-debug' to t."
297 297
   (get-buffer-create (format " *websocket %s debug*"
298 298
                              (websocket-url websocket))))
299 299
 
300  
-(defun websocket-error (websocket msg &rest args)
  300
+(defun websocket-error (msg &rest args)
301 301
   "Report error message MSG."
302 302
   (unless websocket-ignore-error
303 303
     (apply 'message msg args))
@@ -336,7 +336,10 @@ slot of WEBSOCKET.  If the frame is a ping, we reply with a pong.
336 336
 If the frame is a close, we terminate the connection."
337 337
   (let ((opcode (websocket-frame-opcode frame)))
338 338
     (cond ((memq opcode '(continuation text binary))
339  
-           (funcall (websocket-filter websocket) frame))
  339
+           (condition-case err
  340
+               (funcall (websocket-filter websocket) frame)
  341
+             (error (websocket-error "Got error from the filter function: %s"
  342
+                                     (error-message-string err)))))
340 343
           ((eq opcode 'ping)
341 344
            (websocket-send websocket
342 345
                            (make-websocket-frame :opcode 'pong :completep t)))

0 notes on commit a5f1895

Please sign in to comment.
Something went wrong with that request. Please try again.