Moved error-handling of failed requests out so providers must specify it

Moved error-lambda out, there's currently two predefined ones. One
that does allow failover and one that doesn't. This also kills the
newly introduced no-failover key. But it's for the better anyways.

This will make it much easier to actually do proper testing of
different cases in the code that simply wasn't possible before.

Also added a test of running the success lambda.
master
Elis Axelsson 9 years ago
parent 1bfa9ecaba
commit 69166a3c3a
No known key found for this signature in database
GPG Key ID: D57EFA625C9A925F
  1. 29
      test/webpaste-test.el
  2. 45
      webpaste.el

@ -10,18 +10,37 @@
(ert-deftest webpaste-test/provider ()
"Test creation of providers."
(let ((success-lambda t)
(let ((used-lambda nil)
(provider (webpaste-provider
:uri "http://invalid-domain-name/"
:post-field "data"
:no-failover t
:sync t
:success-lambda (cl-function (lambda (&allow-other-keys)
(setq success-lambda nil))))))
:success-lambda (cl-function
(lambda (&key data &allow-other-keys)
(setq used-lambda "success")))
:error-lambda (cl-function
(lambda (&key error-thrown &allow-other-keys)
(setq used-lambda "error"))))))
(funcall provider "dummy-text")
(should (equal t success-lambda))))
(should (equal "error" used-lambda)))
(let ((used-lambda nil)
(provider (webpaste-provider
:uri "https://httpbin.org/status/200"
:post-field "data"
:sync t
:success-lambda (cl-function
(lambda (&key data &allow-other-keys)
(setq used-lambda "success")))
:error-lambda (cl-function
(lambda (&key error-thrown &allow-other-keys)
(setq used-lambda "error"))))))
(funcall provider "dummy-text")
(should (equal "success" used-lambda))))

@ -61,6 +61,20 @@ each run.")
;;; Predefined error lambda for providers
(defvar webpaste/providers-error-lambda
(cl-function (lambda (&key error-thrown &allow-other-keys)
(message "Got error: %S" error-thrown)
(webpaste-paste-text text)))
"Predefined error callback for providers that always does failover.")
(defvar webpaste/providers-error-lambda-no-failover
(cl-function (lambda (&key error-thrown &allow-other-keys)
(message "Got error: %S" error-thrown)))
"Predefined error callback for providers that shouldn't do failover.")
;;; Predefined success lambdas for providers
(defvar webpaste/providers-success-location-header
(cl-function (lambda (&key response &allow-other-keys)
@ -83,9 +97,9 @@ each run.")
(type "POST")
(parser 'buffer-string)
(post-data '())
(no-failover nil)
(sync nil)
post-field
error-lambda
success-lambda)
"Function to create the lambda function for a provider.
@ -99,11 +113,15 @@ Usage:
`request'. This defaults to 'buffer-string.
:post-data Default post fields sent to service. Defaults to nil.
:post-field Name of the field to insert the code into.
:no-failover Set to t to not allow doing failovers. Defaults to nil.
:sync Set to t to wait until request is done. Defaults to nil. This
should only be used for debugging purposes.
:success-lambda Callback sent to `request', look up how to write these in the
documentation for `request'."
documentation for `request'.
:error-lambda Callback sent to `request', look up how to write these in the
documentation for `request'. A good default value forr this is
`webpaste/providers-error-lambda', but there's also
`webpaste/providers-error-lambda-no-failover' available if you
need a provider that isn't allowed to failover."
(lambda (text)
"Paste TEXT to provider"
@ -118,11 +136,7 @@ Usage:
:parser parser
:success success-lambda
:sync sync
:error
(cl-function (lambda (&key error-thrown &allow-other-keys)
(message "Got error: %S" error-thrown)
(unless no-failover
(webpaste-paste-text text))))))))
:error error-lambda))))
@ -132,19 +146,22 @@ Usage:
,(webpaste-provider
:uri "https://ptpb.pw/"
:post-field "c"
:success-lambda webpaste/providers-success-location-header))
:success-lambda webpaste/providers-success-location-header
:error-lambda webpaste/providers-error-lambda))
("ix.io"
,(webpaste-provider
:uri "http://ix.io/"
:post-field "f:1"
:success-lambda webpaste/providers-success-returned-string))
:success-lambda webpaste/providers-success-returned-string
:error-lambda webpaste/providers-error-lambda))
("sprunge.us"
,(webpaste-provider
:uri "http://sprunge.us/"
:post-field "sprunge"
:success-lambda webpaste/providers-success-returned-string))
:success-lambda webpaste/providers-success-returned-string
:error-lambda webpaste/providers-error-lambda))
("dpaste.com"
,(webpaste-provider
@ -154,7 +171,8 @@ Usage:
("poster" . "")
("expiry_days" . 1))
:post-field "content"
:success-lambda webpaste/providers-success-location-header))
:success-lambda webpaste/providers-success-location-header
:error-lambda webpaste/providers-error-lambda))
("dpaste.de"
,(webpaste-provider
@ -163,7 +181,8 @@ Usage:
("format" . "url")
("expires" . 86400))
:post-field "content"
:success-lambda webpaste/providers-success-returned-string)))
:success-lambda webpaste/providers-success-returned-string
:error-lambda webpaste/providers-error-lambda)))
"Define all webpaste.el providers.
Consists of provider name and lambda function to do the actuall call to the

Loading…
Cancel
Save