STARTED Improve error checking

  • State "STARTED" from "TODO" 2010-03-01 Mon 10:00
  • State "TODO" from "DEFERRED" 2010-03-01 Mon 05:49

Current solution for :results value mode:

  • store shell exit code and stderr
  • if non-zero exit code:
    • write stderr to Org-Babel Error Output
    • display Org-Babel Error Output

:results output mixes stderr with stdout and does not otherwise notify on error.

Questions:

  • What should we do when stderr is non-empty but exit code is zero?
  • What should we do in the case of shell non-session :results value?
  • What should we do in the case of :session?

Return error structure?

Could use the following at outset of org-babel-insert-result.

(if (and (consp result) (eq (first result 'org-babel-error-flag)))
    (progn
      (message "Shell command exited with error %d" (second result))
      (unless (= (length (third result)) 0)
        (let (error-buffer (get-buffer-create "*Org-Babel Error Output"))
          (with-current-buffer error-buffer (insert (third result)))
          (display-buffer error-buffer))))
    )

older notes

E.g. when trying to execute sass block, I did not have sass installed, and so shell-command returned code 127, but org-babel did not warn me that anything had gone wrong. I expect it will be hard to do this properly, but ultimately it would be nice to be able to specify somewhere to receive STDERR, and to be warned if it is non-empty.

Probably simpler in non-session evaluation than session? At least the mechanism will be different I guess.

R has a try function, with error handling, along the lines of python. I bet ruby does too. Maybe more of an issue for functional style; in my proposed scripting style the error just gets dumped to the org buffer and the user is thus alerted.

For now I think the current behavior of returning any error messages generated by the source language is sufficient.

Error checking in R sessions

A simple thing to do is to wrap the R code in try(…), as in the patch below. That results in some improved behaviour:

  • You get the error message from R
  • Execution halts at first error E.g.
f <- function() {
    cat("hello\n")
    x <- log("a")
    cat("bye\n")
}
f()

patch

diff –git a/contrib/babel/lisp/langs/org-babel-R.el b/contrib/babel/lisp/langs/org-babel-R.el index 1ef21db..45f8409 100644 — a/contrib/babel/lisp/langs/org-babel-R.el +++ b/contrib/babel/lisp/langs/org-babel-R.el @@ -103,8 +103,8 @@ last statement in BODY, as elisp." (out-tmp-file (make-temp-file "R-out-functional-results"))) (case result-type (output

  • (with-temp-file in-tmp-file (insert body))
  • (shell-command-to-string (format "R –slave –no-save < '%s' > '%s'"
  • (with-temp-file in-tmp-file (insert (concat "try({" body "})")))
  • (shell-command-to-string (format "R –slave –no-save < '%s' > '%s' 2>&1" in-tmp-file out-tmp-file)) (with-temp-buffer (insert-file-contents out-tmp-file) (buffer-string))) (value

@@ -124,7 +124,7 @@ last statement in BODY, as elisp." (format "write.table(.Last.value, file=\"%s\", sep=\"\\t\", na=\"nil\",row.names=FALSE, col.names=%s, quote=FALSE)" tmp-file (if column-names-p "TRUE" "FALSE")) org-babel-R-eoe-indicator) "\n")) (output

  • (mapconcat #'org-babel-chomp (list body org-babel-R-eoe-indicator) "\n"))))
  • (mapconcat #'org-babel-chomp (list (concat "try({" body "})") org-babel-R-eoe-indicator) "\n")))) (raw (org-babel-comint-with-output buffer org-babel-R-eoe-output nil (insert full-body) (inferior-ess-send-input))) broke results)

diff –git a/contrib/babel/lisp/org-babel-ref.el b/contrib/babel/lisp/org-babel-ref.el index 0e8695f..060f880 100644 — a/contrib/babel/lisp/org-babel-ref.el +++ b/contrib/babel/lisp/org-babel-ref.el @@ -139,7 +139,7 @@ return nil." ('results-line (org-babel-read-result)) ('table (org-babel-read-table)) ('source-block

  • (setq result (org-babel-execute-src-block t nil args))
  • (setq result (org-babel-execute-src-block t (org-babel-get-src-block-info) args)) (if (symbolp result) (format "%S" result) result)) ('lob (setq result (org-babel-execute-src-block t lob-info args)))))))