diff --git a/plz-media-type.el b/plz-media-type.el index c5aee2f..aa9504a 100644 --- a/plz-media-type.el +++ b/plz-media-type.el @@ -44,6 +44,29 @@ (require 'eieio) (require 'plz) +(defcustom plz-media-type-debug-response-buffer "*plz-media-type-response*" + "The name of the buffer to which the HTTP response is written to. + +When `plz-media-type-debug-p' is non-nil, the HTTP response will +be written to this buffer. The buffer will be erased on each +HTTP request, unless configured otherwise with +`plz-media-type-debug-erase-buffer-p'." + :group 'plz-media-type + :safe #'stringp + :type 'string) + +(defcustom plz-media-type-debug-erase-buffer-p t + "Whether to erase the response buffer on each new request or not." + :group 'plz-media-type + :safe #'booleanp + :type 'boolean) + +(defcustom plz-media-type-debug-p nil + "Whether to write the HTTP response to the debug buffer or not." + :group 'plz-media-type + :safe #'booleanp + :type 'boolean) + (defclass plz-media-type () ((coding-system :documentation "The coding system to use for the media type." @@ -221,6 +244,9 @@ MEDIA-TYPES is an association list from media type to an instance of a content type class. STRING which is output just received from the process." + (when plz-media-type-debug-p + (with-current-buffer (get-buffer-create plz-media-type-debug-response-buffer) + (insert string))) (when (buffer-live-p (process-buffer process)) (with-current-buffer (process-buffer process) (let ((moving (= (point) (process-mark process)))) @@ -677,6 +703,9 @@ not. (`(media-types ,media-types) media-types))) (let ((buffer)) + (when (and plz-media-type-debug-p plz-media-type-debug-erase-buffer-p) + (with-current-buffer (get-buffer-create plz-media-type-debug-response-buffer) + (erase-buffer))) (condition-case error (let* ((plz-curl-default-args (cons "--no-buffer" plz-curl-default-args)) (result (plz method url diff --git a/plz-media-type.org b/plz-media-type.org index 97ce27e..448ebbc 100644 --- a/plz-media-type.org +++ b/plz-media-type.org @@ -28,6 +28,8 @@ media type formats. - [[#usage][Usage]] - [[#quick-start][Quick Start]] - [[#media-types][Media Types]] +- [[#troubleshooting][Troubleshooting]] + - [[#debugging-http-responses][Debugging HTTP responses]] - [[#credits][Credits]] - [[#copyright-assignment][Copyright assignment]] :END: @@ -333,6 +335,19 @@ does not parse the response body in any way. "{\n \"slideshow\": {\n \"author\": \"Yours Truly\", \n \"date\": \"date of publication\", \n \"slides\": [\n {\n \"title\": \"Wake up to WonderWidgets!\", \n \"type\": \"all\"\n }, \n {\n \"items\": [\n \"Why WonderWidgets are great\", \n \"Who buys WonderWidgets\"\n ], \n \"title\": \"Overview\", \n \"type\": \"all\"\n }\n ], \n \"title\": \"Sample Slide Show\"\n }\n}\n") #+end_src +* Troubleshooting + +** Debugging HTTP responses + +It might be useful to see the full HTTP response while developing a +new media type format or ~plz-media-type~ is failing to parse a +response of an HTTP request. When the ~plz-media-type-debug-p~ custom +variable is set to a non-nil value, each chunk of the HTTP response is +written to the buffer configured by the +~plz-media-type-debug-response-buffer~ custom variable as it +arrives. The buffer is cleared on each new request, unless +~plz-media-type-debug-erase-buffer-p~ is set to a non-nil value. + * Credits - Thanks to [[https://github.com/ahyatt][ahyatt]] and [[https://github.com/alphapapa][alphapapa]] for their help and advice.