branch: externals/ement
commit 850f6b277226a6b164cce30ccaf6aa499fe46fe4
Author: Adam Porter <a...@alphapapa.net>
Commit: Adam Porter <a...@alphapapa.net>

    Fix: (ement--sync) Retry for HTTP 502 errors
    
    Thanks to Simon Pugnet (@polaris64) for reporting.
---
 README.org |  3 +++
 ement.el   | 38 +++++++++++++++++++++++---------------
 2 files changed, 26 insertions(+), 15 deletions(-)

diff --git a/README.org b/README.org
index 240ac362d7..3d233912f8 100644
--- a/README.org
+++ b/README.org
@@ -295,6 +295,9 @@ Note that, while ~matrix-client~ remains usable, and 
probably will for some time
 *Changes*
 + Improve ~ement-describe-room~ command (formatting, bindings).
 
+*Fixes*
++ Retry sync for HTTP 502 "Bad Gateway" errors.
+
 ** 0.5.2
 
 *Fixes*
diff --git a/ement.el b/ement.el
index ab12d38b44..b789a455f6 100644
--- a/ement.el
+++ b/ement.el
@@ -450,21 +450,29 @@ a filter ID).  When unspecified, the value of
                           :then (apply-partially #'ement--sync-callback 
session)
                           :else (lambda (plz-error)
                                   (setf (map-elt ement-syncs session) nil)
-                                  (pcase (plz-error-curl-error plz-error)
-                                    (`(,(or 28 429 502) . ,_)
-                                     ;; Timeout (28), "Too Many Requests" 
(429), or "Bad Gateway" (502): sync again if enabled.
-                                     (if (not ement-auto-sync)
-                                         (run-hook-with-args 
'ement-interrupted-sync-hook session)
-                                       (let ((reason (pcase-exhaustive (car 
(plz-error-curl-error plz-error))
-                                                       (28 "timed out")
-                                                       ((or 429 502) 
"failed"))))
-                                         (message "Ement: Sync %s (%s).  
Syncing again..."
-                                                  reason (ement-user-id 
(ement-session-user session))))
-                                       ;; Set QUIET to allow the just-printed 
message to remain visible.
-                                       (ement--sync session :timeout timeout 
:quiet t)))
-                                    (`(,code . ,message)
-                                     (signal 'ement-api-error (list (format 
"Ement: Network error: %s: %s" code message) plz-error)))
-                                    (_ (signal 'ement-api-error (list "Ement: 
Unrecognized network error" plz-error)))))
+                                  ;; TODO: plz probably needs nicer error 
handling.
+                                  ;; Ideally we would use `condition-case', 
but since the
+                                  ;; error is signaled in `plz--sentinel'...
+                                  (pcase-let (((cl-struct plz-error curl-error 
response) plz-error)
+                                              (reason))
+                                    (cond ((when response
+                                             (pcase (plz-response-status 
response)
+                                               ((or 429 502) (setf reason 
"failed")))))
+                                          ((pcase curl-error
+                                             (28 (setf reason "timed out")))))
+                                    (if reason
+                                        (if (not ement-auto-sync)
+                                            (run-hook-with-args 
'ement-interrupted-sync-hook session)
+                                          (message "Ement: Sync %s (%s).  
Syncing again..."
+                                                   reason (ement-user-id 
(ement-session-user session)))
+                                          ;; Set QUIET to allow the 
just-printed message to remain visible.
+                                          (ement--sync session :timeout 
timeout :quiet t))
+                                      ;; Unrecognized errors:
+                                      (pcase curl-error
+                                        (`(,code . ,message)
+                                         (signal 'ement-api-error (list 
(format "Ement: Network error: %s: %s" code message)
+                                                                        
plz-error)))
+                                        (_ (signal 'ement-api-error (list 
"Ement: Unrecognized network error" plz-error)))))))
                           :json-read-fn (lambda ()
                                           "Print a message, then call 
`json-read'."
                                           (when (ement--sync-messages-p 
session)

Reply via email to