While refactoring this Common Lisp code snippet, how to succesfully remove the cond clause wrapping a handler-case (error handling) situation? - error-handling

I am using SBCL, Emacs, Slime, and Dexador (a library for HTTP requests). I have this function that works:
(defun old-handle-response-and-status (final-url method &optional user-content)
(let ((status-code)
(response))
(cond ((equal method "get")
(multiple-value-bind (bresponse bstatus-code)
(handler-case (dex:get final-url)
(dex:http-request-bad-request ()
(values nil
"The server returned a failed request of 400 (bad request) status."))
(dex:http-request-failed (e)
(values nil
(format nil "The server returned a failed request of ~a status." (dex:response-status e)))))
(list (setf response bresponse)
(setf status-code bstatus-code))))
((equal method "post")
(multiple-value-bind (bresponse bstatus-code)
(handler-case (dex:post final-url
:content user-content)
(dex:http-request-bad-request ()
(values nil
"The server returned a failed request of 400 (bad request) status."))
(dex:http-request-failed (e)
(values nil
(format nil "The server returned a failed request of ~a status." (dex:response-status e)))))
(list (setf response bresponse)
(setf status-code bstatus-code)))))))
It works for GET, POST, and the error handling works as expected when a HTTP request faces errors. The iconic examples to show it working are:
CL-USER> (old-handle-response-and-status "http://www.paulgraham.com" "get")
("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
<html>
(big HTML omitted)
</html>"
200)
CL-USER> (old-handle-response-and-status "https://httpbin.org/post" "post" '(("name" . "pedro")))
("{
medium JSON omitted
}
"
200)
CL-USER> (old-handle-response-and-status "https://httpstat.us/409" "get")
(NIL "The server returned a failed request of 409 status.")
Ok. While refactoring this code, I was trying to remove the cond clause. Thus I did a new shorter version:
(defun new-handle-response-and-status (method-call)
(let ((status-code)
(response))
(multiple-value-bind (bresponse bstatus-code)
(handler-case method-call
(dex:http-request-bad-request ()
(values nil
"The server returned a failed request of 400 (bad request) status."))
(dex:http-request-failed (e)
(values nil
(format nil "The server returned a failed request of ~a status." (dex:response-status e)))))
(list (setf response bresponse)
(setf status-code bstatus-code)))))
It majorly works, but only when the request is successful:
CL-USER> (new-handle-response-and-status (dex:get "http://www.paulgraham.com"))
("
HTML omitted
</html>"
NIL)
CL-USER> (new-handle-response-and-status (dex:post "https://httpbin.org/post" :content '(("name" . "pedro"))))
("{
medium JSON omitted
}
"
NIL)
When the request is a failed HTTP request, the refactoring does not work as expected!
When calling:
CL-USER> (new-handle-response-and-status (dex:get "https://httpstat.us/409"))
The Slime Debugger throws:
An HTTP request to "https://httpstat.us/409" returned 409 conflict.
I was expecting:
(NIL "The server returned a failed request of 409 status.")
I tried tweaking the input to be a quoted s-expression and inserting an eval:
(defun new-handle-response-and-status (method-call)
(let ((status-code)
(response))
(multiple-value-bind (bresponse bstatus-code)
(handler-case (eval method-call)
(dex:http-request-bad-request ()
(values nil
"The server returned a failed request of 400 (bad request) status."))
(dex:http-request-failed (e)
(values nil
(format nil "The server returned a failed request of ~a status." (dex:response-status e)))))
(list (setf response bresponse)
(setf status-code bstatus-code)))))
It works:
CL-USER> (new-handle-response-and-status '(dex:get "https://httpstat.us/409"))
(NIL "The server returned a failed request of 409 status.")
But, it feels as a bad practice - not really compatible with a refactoring effort. Is there a way to fix this without using eval?
Maybe using funcall?

The problem is that you're calling dex:get or dex:post before invoking the function, so the handler binding is not in effect.
You need to pass a function that calls it, and then call that function.
(defun new-handle-response-and-status (method-call)
(let ((status-code)
(response))
(multiple-value-bind (bresponse bstatus-code)
(handler-case (funcall method-call)
(dex:http-request-bad-request ()
(values nil
"The server returned a failed request of 400 (bad request) status."))
(dex:http-request-failed (e)
(values nil
(format nil "The server returned a failed request of ~a status." (dex:response-status e)))))
(list (setf response bresponse)
(setf status-code bstatus-code)))))
(new-handle-response-and-status (lambda () (dex:get "https://httpstat.us/409")))
Or you could convert it to a macro:
(defmacro new-handle-response-and-status (method-call)
`(let ((status-code)
(response))
(multiple-value-bind (bresponse bstatus-code)
(handler-case ,method-call
(dex:http-request-bad-request ()
(values nil
"The server returned a failed request of 400 (bad request) status."))
(dex:http-request-failed (e)
(values nil
(format nil "The server returned a failed request of ~a status." (dex:response-status e)))))
(list (setf response bresponse)
(setf status-code bstatus-code)))))

Remember evaluation rules.
CL-USER 36 > (defun foo (a)
(print 'foo1)
a
(print 'foo2)
'return-value)
FOO
CL-USER 37 > (foo (print 'bar1))
BAR1
FOO1
FOO2
RETURN-VALUE
(print 'bar1) is evaluated outside of foo.

Related

Error 403 when get request to server. Server contains core header

I'm running a server with clojure on localhost:3000. The server talks to the client, which is a react native app that's using axios to talk to the server. However the communication with the server is returning a 403 error.
Call to the server:
export const invest = (itemid, amount) => async dispatch => {
console.log("investing in actions")
const domain = 'localhost:3000'
const res = axios.post(domain + '/api/invest', {itemid: itemid, amount: amount});
console.log("response is", res)
dispatch({ type: INVESTED, payload:res.data});
}
Server contains a reitit route called "/api/invest", and this route will call a function called featured/invest. But instead I'm getting a 403 error in the client:
(ns humboiserver.routes.home
(:require
[humboiserver.layout :as layout]
[clojure.java.io :as io]
[humboiserver.middleware :as middleware]
[ring.util.response]
[ring.util.http-response :as response]
[humboiserver.routes.featured :as featured]))
(defn home-page [request]
(layout/render request "home.html" {:docs (-> "docs/docs.md" io/resource slurp)}))
(defn about-page [request]
(layout/render request "about.html"))
(defn home-routes []
[""
{:middleware [middleware/wrap-csrf
middleware/wrap-formats]}
["/" {:get home-page}]
["/api"
["/about" {:get about-page}]
["/featured" featured/get-featured]
["/invest" featured/invest]
]
])
Even the invested prn statement isn't printed in the logs.
;; featured/invest function.
(defn response [data & [status]]
{:status (or status 200)
:headers {"Content-Type" "application/json"
"Access-Control-Allow-Headers" "Content-Type"
"Access-Control-Request-Method" "GET, OPTIONS"
"Access-Control-Allow-Origin" "*"
"Access-Control-Allow-Credentials" true
}
:body (pr-str data)})
(defn invest [req]
(prn "invested")
(response (db/find "featured" {})))
403 error:
[Warning] Possible Unhandled Promise Rejection (id: 0): (AppEntry.bundle, line 42288)
Error: Request failed with status code 403
createError#http://127.0.0.1:19000/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:156390:26
settle#http://127.0.0.1:19000/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:156380:25
handleLoad#http://127.0.0.1:19000/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:156280:15
dispatchEvent#http://127.0.0.1:19000/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:32753:31
setReadyState#http://127.0.0.1:19000/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:31822:27
__didCompleteResponse#http://127.0.0.1:19000/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:31653:29
emit#http://127.0.0.1:19000/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:7566:42
__callFunction#http://127.0.0.1:19000/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:3195:36
http://127.0.0.1:19000/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:2927:31
__guard#http://127.0.0.1:19000/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:3149:15
callFunctionReturnFlushedQueue#http://127.0.0.1:19000/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:2926:21
callFunctionReturnFlushedQueue#[native code]
http://localhost:19000/debugger-ui/debuggerWorker.aca173c4.js:4:907
How to fix this error?
Hi I think you need to send an Anti-forgery token if I'm not mistaken...
https://github.com/ring-clojure/ring-anti-forgery
You can use curl to test accessing your server via the command line

Clojure :pre report failing value when destructuring

Following this SO post, I would like to print the value of the preconditions in my function. However it fails for me in the following case (probably destructuring) :
I have a dir? helper function (feel free to skip this one) :
(defn dir? [s]
"returns true if the string passed is is an existing directory"
(->> (clojure.java.io/file s)
((juxt #(.exists %) #(.isDirectory %)))
(every? true?)))
It works just fine, and using the is macro, I get some nice error messages where I can see both the test and the parameters that were passed :
(is (dir? (io/file "resources/static"))) ;; => true
(is (dir? (io/file "resources/statice"))) ;; typo, error below
FAIL in clojure.lang.PersistentList$EmptyList#1
(boot.user4515592986834245937.clj:86) expected: (dir? (io/file
"resources/statice")) actual: (not (dir? #object[java.io.File
0x6730a420 "resources/statice"]))
However, when trying to use it in the precondition :pre, I get an ugly error :
(defn make-something
[&{:keys [dir]
:or {dir "default-dir"}}]
{:pre [(is (dir? (clojure.java.io/file dir)))]}
;;... do something with these
)
(make-something :dir "resources/statices") ;; doesn't exist
clojure.lang.Compiler$CompilerException: java.lang.AssertionError:
Assert failed: (is (dir? (io/file dir))),
compiling:(boot.user4515592986834245937.clj:80:12)
java.lang.AssertionError: Assert failed: (is (dir? (io/file dir)))
How can I get a nice error message in my function just like the one above ?
In case it matters, I am using Clojure 1.7.
You need to check your code (dir? function). Following snippet works for me:
(require '[clojure.java.io :as io])
(defn dir? [s]
(let [dir (io/file s)]
(and (.exists dir)
(.isDirectory dir))))
(defn make-something
[& {:keys [dir] :or {dir "default-dir"}}]
{:pre [(is (dir? dir))]}
(println dir))
(make-something :dir "/tmp")
out => /tmp
ret => nil
(make-something :dir "/xxxx")
FAIL in clojure.lang.PersistentList$EmptyList#1 (form-init3332271312167175068.clj:1)
expected: (dir? dir)
actual: (not (dir? "/xxxx"))
AssertionError Assert failed: (is (dir? dir)) user/make-sth (form-init3332271312167175068.clj:1)

clojure.test unable to resolve symbol from `eval`. Works in REPL and lein run

Please bear with this contrived example but it was the simplest thing I could think of to recreate the issue.
(ns something.core)
(defn call-foo [something & args]
(let [a-foo (:foo (eval (:quux something)))]
(apply a-foo args)))
(def Something {
:foo (fn [& args] args)
:bar (fn [something] (call-foo something))
})
(defn make-something []
{:quux 'Something})
Running the following in the REPL or with lein run works well.
(let [subject (make-something)
actual (call-foo subject "hello" "greetings")]
(println actual))
;;=> (hello greetings)
The problem occurs only during this test and executing lein test:
(ns something.core-test
(:require [clojure.test :refer :all]
[something.core :refer :all]))
(deftest a-test
(let [subject (make-something)
actual (call-foo subject "hello" "greetings")]
(is (= ["hello" "greetings"] actual))))
This throws an error. An example output:
ERROR in (a-test) (Compiler.java:6464)
Uncaught exception, not in assertion.
expected: nil
actual: clojure.lang.Compiler$CompilerException: java.lang.RuntimeException: Unable to resolve symbol: Something in this context, compiling:(/private/var/folders/0n/c7q7860j34xfc2r1x4q51jrh0000gn/T/form-init9215140948330409114.clj:1:6436)
The line "Unable to resolve symbol: Something in this context" makes me think Something is not in context for some reason while I eval in call-foo. But why is this the case only in the test?
The problem is that eval does not see context. Your 'Something resolves in something.core and something.core-test since you have refered all. It won't resolve from whatever namespace where lein test runs its tests.
To fix the immediate problem change
'Something
to
`Something
so that it is namespace-qualified. The test will then run (and fail), but that's another issue (println returns nil for one thing).

Testing static routes in compojure

I am trying to write a test for the compojure static content route.
I am testing the routes by examining the ring response directly.
A minimal working example is as follows:
;; src/testing-webapps.core.clj
(ns testing-webapps.core
(:use [compojure.core]
[compojure.route :as route]))
(defroutes web-app
(route/resources "/")
(route/not-found "404"))
;; test/testing-webapps.core_test.clj
(ns testing-webapps.core-test
(:require [clojure.test :refer :all]
[testing-webapps.core :refer :all]))
(defn request [resource web-app & params]
(web-app {:request-method :get :uri resource :params (first params)}))
(deftest test-routes
(is (= 404 (:status (request "/flubber" web-app))))
(is (= "404" (:body (request "/flubber" web-app))))
(is (= 200 (:status (request "/test.txt" web-app)))))
Testing the 404 route works fine but calling (request "/test.txt" web-app) leads to an unexpected NullPointerException in ring.middleware.file-info/not-modified-since?.
Here is the top part of the stacktrace:
ERROR in (test-routes) (file_info.clj:27)
Uncaught exception, not in assertion.
expected: nil
actual: java.lang.NullPointerException: null
at ring.middleware.file_info$not_modified_since_QMARK_.invoke (file_info.clj:27)
ring.middleware.file_info$file_info_response.doInvoke (file_info.clj:44)
clojure.lang.RestFn.invoke (RestFn.java:442)
ring.middleware.file_info$wrap_file_info$fn__917.invoke (file_info.clj:64)
[...]
The static route works fine in the browser but not when called through my request function.
Is there an easier way to test a static route in compojure and why do I get a NullPointerException when calling the static route with my own request map?
Looking at the source for not-modified-since?, I think the issue is you have no headers in your request map, so it throws a NPE on this expr: (headers "if-modified-since"). Try changing your request method like so:
(defn request [resource web-app & params]
(web-app {:request-method :get
:headers {"content-type" "text/plain"} ; added a header
:uri resource
:params (first params)}))
You might also consider using ring-mock to create requests for testing. It insulates you a little bit from stuff like this.

Request.el "cannot create connection to api.stackexchange.com"

I'm using the request.el library (available via MELPA) to try and create a basic framework from which to start work in earnest on a Stack Exchange mode for Emacs. All I want to do is to be able to return the object as parsed by json-read to the calling function, but I can't even seem to make a connection.
I understand that to have my function return the object, the call must be made synchronously, so that is what :sync t is there for. I've considered making it an asynchronous call, but I don't think it would be beneficial considering its use-case.
At first, after I looked at the messages, I thought 'Maybe I don't have necessary binaries.' I did test request.el with some example calls that come with its documentation, and they work fine, so that's out.
I'm at a loss as to what is wrong. I don't yet have much experience, successful or otherwise, with anything dealing with a network, and don't fully understand the error message I'm getting. As far as I can tell, port 443 of the API is giving me the silent treatment, but I'm hesitant to think that's the case ;).
;; Works like a charm
(defun alist-to-json (alist)
"Converts the key-value pairs of `ALIST` into a JSON-friendly
string: \"key1=value1&key2=value2&\"."
(apply 'concat
(mapcar (lambda (kv)
(format "%s=%s&" (car kv)
(if (stringp (cdr kv))
(cdr kv)
(number-to-string (cdr kv)))))
alist)))
(defvar stack-api-root "https://api.stackexchange.com/2.1/")
(require 'json)
(require 'request)
(defun stack-api-request (call keys-alist)
"Makes the specified `CALL` to the Stack Exchange API with the
key-value pairs given `KEYS-ALIST`. For example,
(stack-api-request \"sites\" '((page . 2) (page_size . 25)))"
(let* ((base-call (concat stack-api-root call "?"))
(options (alist-to-json keys-alist)))
(request base-call
:params options
:parser 'json-read
:sync t)))
Backtrace
Debugger entered--Lisp error: (error "Could not create connection to api.stackexchange.com:443")
signal(error ("Could not create connection to api.stackexchange.com:443"))
error("Could not create connection to %s:%d" "api.stackexchange.com" 443)
url-http([cl-struct-url "https" nil nil "api.stackexchange.com" nil "/2.1/sites?&" nil nil t nil t] #[128 "\302\303\304p#\210\300\305\240\210\301p\240\207" [(nil) (nil) url-debug retrieval "Synchronous fetching done (%S)" t] 5 "\n\n(fn &rest IGNORED)"] (nil))
url-https([cl-struct-url "https" nil nil "api.stackexchange.com" nil "/2.1/sites?&" nil nil t nil t] #[128 "\302\303\304p#\210\300\305\240\210\301p\240\207" [(nil) (nil) url-debug retrieval "Synchronous fetching done (%S)" t] 5 "\n\n(fn &rest IGNORED)"] (nil))
url-retrieve-internal("https://api.stackexchange.com/2.1/sites?&" #[128 "\302\303\304p#\210\300\305\240\210\301p\240\207" [(nil) (nil) url-debug retrieval "Synchronous fetching done (%S)" t] 5 "\n\n(fn &rest IGNORED)"] (nil) nil nil)
url-retrieve("https://api.stackexchange.com/2.1/sites?&" #[128 "\302\303\304p#\210\300\305\240\210\301p\240\207" [(nil) (nil) url-debug retrieval "Synchronous fetching done (%S)" t] 5 "\n\n(fn &rest IGNORED)"])
url-retrieve-synchronously("https://api.stackexchange.com/2.1/sites?&")
request--url-retrieve-sync("https://api.stackexchange.com/2.1/sites?&" :params "page=2&page_size=25&" :parser json-read :sync t :error (closure (t) (&rest args) (apply (quote request-default-error-callback) (quote "https://api.stackexchange.com/2.1/sites?") args)) :url "https://api.stackexchange.com/2.1/sites?&" :response [cl-struct-request-response nil nil nil nil nil "https://api.stackexchange.com/2.1/sites?&" nil (:params "page=2&page_size=25&" :parser json-read :sync t :error (closure (t) (&rest args) (apply (quote request-default-error-callback) (quote "https://api.stackexchange.com/2.1/sites?") args)) :url "https://api.stackexchange.com/2.1/sites?&" :response #0) nil nil nil url-retrieve nil])
apply(request--url-retrieve-sync "https://api.stackexchange.com/2.1/sites?&" (:params "page=2&page_size=25&" :parser json-read :sync t :error (closure (t) (&rest args) (apply (quote request-default-error-callback) (quote "https://api.stackexchange.com/2.1/sites?") args)) :url "https://api.stackexchange.com/2.1/sites?&" :response [cl-struct-request-response nil nil nil nil nil "https://api.stackexchange.com/2.1/sites?&" nil #0 nil nil nil url-retrieve nil]))
request("https://api.stackexchange.com/2.1/sites?" :params "page=2&page_size=25&" :parser json-read :sync t)
(let* ((base-call (concat stack-api-root call "?")) (options (alist-to-json keys-alist))) (request base-call :params options :parser (quote json-read) :sync t))
stack-api-request("sites" ((page . 2) (page_size . 25)))
eval((stack-api-request "sites" (quote ((page . 2) (page_size . 25)))) nil)
eval-expression((stack-api-request "sites" (quote ((page . 2) (page_size . 25)))) nil)
call-interactively(eval-expression nil nil)
Messages:
Contacting host: api.stackexchange.com:443
Opening TLS connection to `api.stackexchange.com'...
Opening TLS connection with `gnutls-cli --insecure -p 443 api.stackexchange.com'...failed
Opening TLS connection with `gnutls-cli --insecure -p 443 api.stackexchange.com --protocols ssl3'...failed
Opening TLS connection with `openssl s_client -connect api.stackexchange.com:443 -no_ssl2 -ign_eof'...failed
Opening TLS connection to `api.stackexchange.com'...failed
I checked to make sure that this was not a problem with cURL. The call I use for curl is
curl api.stackexchange.com/2.1/sites --compressed
From the outside looking into the request repository, request is doing this as well. I don't know what could be going wrong.
I boiled down your example to the following snippet to reproduce your problem but it actually worked. Can you try this one?
(request
"http://api.stackexchange.com/2.1/sites"
:parser 'json-read
:params '((page . "2") (page_size . "25"))
:success (lambda (&rest args) (princ (plist-get args :data))))
It should print some data to *Messages* buffer and echo area.
EDIT: It seems that the problem in your example is that you are passing string to PARAMS which takes only alist. I will change code to raise error so that it is easier to debug.