Skip to content

Commit 2e1d586

Browse files
authored
Merge pull request #11 from emacs-php/feature/handling-errors
Add error handler
2 parents fa43128 + e5f2197 commit 2e1d586

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

php-runtime-test.el

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,9 @@ while (($line = fgets(STDIN)) !== false) {
6868
(should (string= "200" (php-runtime-expr "(1+1)*100")))
6969
(should (string= "foo" (php-runtime-expr "'f' . 'oo'"))))
7070

71+
(ert-deftest php-runtime-test-expr-syntax-error ()
72+
"Test that PHP code syntax errors are reported as errors in Emacs."
73+
(should-error (php-runtime-expr "a:b")))
74+
7175
(provide 'php-runtime-test)
7276
;;; php-runtime-test.el ends here

php-runtime.el

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,25 @@ for example, (get-buffer \"foo-buffer\"), '(:file . \"/path/to/file\")."
7878
(member (car obj) (list :file :string))
7979
(stringp (cdr obj))))
8080

81+
(defun php-runtime-default-handler (status output)
82+
"Return `OUTPUT', and raise error when `STATUS' is not 0."
83+
(if (eq 0 status)
84+
output
85+
(error output)))
8186

8287
;; PHP Execute class
8388

8489
;;;###autoload
8590
(defclass php-runtime-execute nil
8691
((executable :initarg :executable :type string)
8792
(code :initarg :code :type (satisfies php-runtime--code-satisfied-p))
93+
(handler :initarg :handler :type function :initform #'php-runtime-default-handler)
8894
(stdin :initarg :stdin :type (satisfies php-runtime--stdin-satisfied-p) :initform nil)
8995
(stdout :initarg :stdout :type (or null buffer-live list) :initform nil)
9096
(stderr :initarg :stderr :type (or null buffer-live list) :initform nil)))
9197

9298
(cl-defmethod php-runtime-run ((php php-runtime-execute))
93-
"Execute PHP process using `php -r' with code.
99+
"Execute PHP process using `php -r' with code and return status code.
94100
95101
This execution method is affected by the number of character limit of OS command arguments.
96102
You can check the limitation by command, for example \(shell-command-to-string \"getconf ARG_MAX\") ."
@@ -99,6 +105,13 @@ You can check the limitation by command, for example \(shell-command-to-string \
99105
(php-runtime--call-php-process-with-input-buffer php args)
100106
(php-runtime--call-php-process php args))))
101107

108+
(cl-defmethod php-runtime-process ((php php-runtime-execute))
109+
"Execute PHP process using `php -r' with code and return output."
110+
(let ((status (php-runtime-run php))
111+
(output (with-current-buffer (php-runtime-stdout-buffer php)
112+
(buffer-substring-no-properties (point-min) (point-max)))))
113+
(funcall (oref php handler) status output)))
114+
102115
(cl-defmethod php-runtime--call-php-process ((php php-runtime-execute) args)
103116
"Execute PHP Process by php-execute `PHP' and `ARGS'."
104117
(apply #'call-process (oref php executable)
@@ -162,13 +175,13 @@ Pass `INPUT-BUFFER' to PHP executable as STDIN."
162175
"Evalute PHP code `CODE' without open tag, and return buffer.
163176
164177
Pass `INPUT-BUFFER' to PHP executable as STDIN."
165-
(let ((execute (php-runtime-execute :code (cons :string code)
178+
(let ((executor (php-runtime-execute :code (cons :string code)
166179
:executable php-runtime-php-executable
167180
:stderr (get-buffer-create php-runtime-error-buffer-name)))
168181
(temp-input-buffer (when (and input-buffer (not (bufferp input-buffer)))
169182
(php-runtime--temp-buffer))))
170183
(when input-buffer
171-
(oset execute stdin
184+
(oset executor stdin
172185
(if (or (bufferp input-buffer)
173186
(and (consp input-buffer) (eq :file (car input-buffer))))
174187
input-buffer
@@ -177,13 +190,11 @@ Pass `INPUT-BUFFER' to PHP executable as STDIN."
177190
(insert input-buffer))))))
178191

179192
(unwind-protect
180-
(progn (php-runtime-run execute)
181-
(with-current-buffer (php-runtime-stdout-buffer execute)
182-
(buffer-substring-no-properties (point-min) (point-max))))
193+
(php-runtime-process executor)
183194
(when (and temp-input-buffer (buffer-live-p temp-input-buffer))
184195
(kill-buffer temp-input-buffer))
185196
(when php-runtime--kill-temp-output-buffer
186-
(kill-buffer (php-runtime-stdout-buffer execute))))))
197+
(kill-buffer (php-runtime-stdout-buffer executor))))))
187198

188199
(provide 'php-runtime)
189200
;;; php-runtime.el ends here

0 commit comments

Comments
 (0)