فهرست منبع

Handle VK API errors

Innokenty Enikev 10 سال پیش
والد
کامیت
54fcd9c6b1
2فایلهای تغییر یافته به همراه34 افزوده شده و 28 حذف شده
  1. 17 12
      chatikbot.lisp
  2. 17 16
      vk.lisp

+ 17 - 12
chatikbot.lisp

@@ -131,7 +131,7 @@
           (when (> id *akb-last-id*)
             (send-akb (format-akb post))
             (setf *akb-last-id* id))))
-    (error (e) (log:error e))))
+    (condition (e) (log:error e))))
 
 (defun send-akb (text)
   (log:info "send-akb: ~A" text)
@@ -139,20 +139,25 @@
     (handler-case
         (telegram-send-message chat-id text
                                :disable-web-preview 1)
-      (error (e) (log:error e)))))
+      (condition (e) (log:error e)))))
 
 (defun handle-cmd-akb (chat-id message-id args)
   (log:info "handle-cmd-akb" chat-id message-id args)
-  (let ((total-aneks
-         (aget "count" (vk-wall-get :domain +akb-vk-domain+ :count 1 :offset 10000000))))
-    (dolist (post (aget "items" (vk-wall-get :domain +akb-vk-domain+
-                                             :count (or (ignore-errors (parse-integer (car args))) 1)
-                                             :offset (random total-aneks))))
-      (handler-case
-          (telegram-send-message chat-id
-                                 (format-akb post)
-                                 :disable-web-preview 1)
-        (error (e) (log:error e))))))
+  (handler-case
+      (progn
+	(let ((total-aneks
+	       (aget "count" (vk-wall-get :domain +akb-vk-domain+ :count 1 :offset 10000000))))
+	  (dolist (post (aget "items" (vk-wall-get :domain +akb-vk-domain+
+						   :count (or (ignore-errors (parse-integer (car args))) 1)
+						   :offset (random total-aneks))))
+	    (handler-case
+		(telegram-send-message chat-id
+				       (format-akb post)
+				       :disable-web-preview 1)
+	      (condition (e) (log:error e))))))
+    (condition (e)
+      (log:error e)
+      (telegram-send-message chat-id "some shit happened"))))
 
 
 (defun handle-cmd-rates (chat-id message-id args)

+ 17 - 16
vk.lisp

@@ -3,22 +3,23 @@
 (defparameter +vk-api-url+ "https://api.vk.com/method/~A?v=5.34" "VK.com API endpoint")
 
 (defun %vk-api-call (method &optional args)
-  (let* ((params (loop for (k . v) in args
-                    when v
-                    collect (cons
-                             (princ-to-string k)
-                             (princ-to-string v))))
-         (response (yason:parse
-                    (flexi-streams:octets-to-string
-                     (drakma:http-request (format nil +vk-api-url+ method)
-                                          :method :post
-                                          :parameters params
-                                          :external-format-out :utf8)
-                     :external-format :utf8)
-                    :object-as :alist)))
-    (when (aget "error" response)
-      (error (aget "error_msg" (aget "error" response))))
-    (aget "response" response)))
+  (bordeaux-threads:with-timeout (5)
+    (let* ((params (loop for (k . v) in args
+		      when v
+		      collect (cons
+			       (princ-to-string k)
+			       (princ-to-string v))))
+	   (response (yason:parse
+		      (flexi-streams:octets-to-string
+		       (drakma:http-request (format nil +vk-api-url+ method)
+					    :method :post
+					    :parameters params
+					    :external-format-out :utf8)
+		       :external-format :utf8)
+		      :object-as :alist)))
+      (when (aget "error" response)
+	(error (aget "error_msg" (aget "error" response))))
+      (aget "response" response))))
 
 (defun vk-wall-get (&key owner-id domain offset count filter extended)
   (%vk-api-call "wall.get"