add proper error handling on wrong inputs
This commit is contained in:
parent
a01015cc44
commit
d56d7f9fce
|
@ -2,32 +2,34 @@
|
||||||
(open servlets
|
(open servlets
|
||||||
httpd-request
|
httpd-request
|
||||||
url
|
url
|
||||||
|
handle-fatal-error
|
||||||
|
let-opt
|
||||||
scsh
|
scsh
|
||||||
scheme)
|
scheme)
|
||||||
(begin
|
(begin
|
||||||
|
|
||||||
(define number-input-field (make-number-input-field '(@ (maxlength 10))))
|
(define number-input-field (make-number-input-field '(@ (maxlength 10))))
|
||||||
|
|
||||||
(define (get-number input-text . maybe-title)
|
(define (get-number input-text . maybe-update-text)
|
||||||
(let* ((title (if (pair? maybe-title) (car maybe-title) #f))
|
(let* ((update-text (:optional maybe-update-text ""))
|
||||||
(result
|
(result
|
||||||
(send-html/suspend
|
(send-html/suspend
|
||||||
(lambda (new-url)
|
(lambda (new-url)
|
||||||
`(html ,(if title
|
`(html (title ,input-text)
|
||||||
`(title ,title) '())
|
|
||||||
(body
|
(body
|
||||||
,(if title `(h1 ,title) '())
|
(p (font (@ (color "red")) ,update-text))
|
||||||
(p (a (@ href "reset")
|
|
||||||
"click here to reset server's servlet cache"))
|
|
||||||
(p
|
(p
|
||||||
(servlet-form ,new-url
|
(servlet-form ,new-url
|
||||||
,input-text
|
,input-text " "
|
||||||
,number-input-field
|
,number-input-field
|
||||||
,(make-submit-button)))))))))
|
,(make-submit-button)))))))))
|
||||||
(if result
|
(if result
|
||||||
(input-field-value number-input-field
|
(with-fatal-error-handler
|
||||||
(form-query (http-url:search (request:url result))))
|
(lambda (condition more)
|
||||||
(get-number input-text "Please enter a number"))))
|
(get-number input-text "Please enter a valid number."))
|
||||||
|
(input-field-value number-input-field
|
||||||
|
(form-query (http-url:search (request:url result)))))
|
||||||
|
(get-number input-text "Please enter a number."))))
|
||||||
|
|
||||||
(define (get-number1)
|
(define (get-number1)
|
||||||
(get-number "First number:"))
|
(get-number "First number:"))
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
(define-structure servlet servlet-interface
|
(define-structure servlet servlet-interface
|
||||||
(open scsh
|
(open scsh
|
||||||
scheme
|
scheme
|
||||||
|
handle-fatal-error
|
||||||
|
let-opt
|
||||||
servlets
|
servlets
|
||||||
servlet-handler/admin
|
servlet-handler/admin
|
||||||
httpd-responses
|
httpd-responses
|
||||||
|
@ -26,17 +28,23 @@
|
||||||
(URL ,(make-callback return-to-main-page) "Return to main page")))
|
(URL ,(make-callback return-to-main-page) "Return to main page")))
|
||||||
)))
|
)))
|
||||||
|
|
||||||
(define (handler-options req . update-text)
|
(define (handler-options req . maybe-update-text)
|
||||||
(let* ((number-field
|
(let* ((update-text `(font (@ (color "red"))
|
||||||
|
,(:optional maybe-update-text "")))
|
||||||
|
(number-field
|
||||||
(make-number-input-field `(@ ((value ,(options-instance-lifetime))))))
|
(make-number-input-field `(@ ((value ,(options-instance-lifetime))))))
|
||||||
(req (get-option-change number-field update-text)))
|
(req (get-option-change number-field update-text)))
|
||||||
|
(with-fatal-error-handler
|
||||||
(set-options-instance-lifetime!
|
(lambda (condition more)
|
||||||
(input-field-value number-field (get-bindings req)))
|
(handler-options req "Please enter a valid, positive integer number"))
|
||||||
(handler-options req
|
(set-options-instance-lifetime
|
||||||
`(font (@ (color "red"))
|
(let ((result (input-field-value number-field (get-bindings req))))
|
||||||
,(format #f "Instance lifetime changed to ~a."
|
(if (and (integer? result)
|
||||||
(options-instance-lifetime))))))
|
(> result 0))
|
||||||
|
(handler-options req
|
||||||
|
(format #f "Instance lifetime changed to ~a."
|
||||||
|
(options-instance-lifetime)))
|
||||||
|
(error "not a positive integer")))))))
|
||||||
|
|
||||||
(define (return-to-main-page req)
|
(define (return-to-main-page req)
|
||||||
(send/finish (make-http-error-response http-status/moved-perm req
|
(send/finish (make-http-error-response http-status/moved-perm req
|
||||||
|
|
|
@ -42,7 +42,7 @@
|
||||||
(br)
|
(br)
|
||||||
(servlet-form
|
(servlet-form
|
||||||
,new-url
|
,new-url
|
||||||
(p "This uses " (pre "gnuplot") " that is searched at "
|
(p "This uses " (var "gnuplot") " that is searched at "
|
||||||
,input-field ,(make-submit-button "Change"))))
|
,input-field ,(make-submit-button "Change"))))
|
||||||
(li (URL ,(make-callback reset) "Delete files and reset profile state.")))
|
(li (URL ,(make-callback reset) "Delete files and reset profile state.")))
|
||||||
(hr)
|
(hr)
|
||||||
|
@ -56,11 +56,12 @@
|
||||||
(lambda (condition more)
|
(lambda (condition more)
|
||||||
#f)
|
#f)
|
||||||
(input-field-value input-field bindings))))
|
(input-field-value input-field bindings))))
|
||||||
(if new-gnuplot-location
|
(if (and new-gnuplot-location
|
||||||
|
(file-executable? new-gnuplot-location))
|
||||||
(begin
|
(begin
|
||||||
(set! gnuplot new-gnuplot-location)
|
(set! gnuplot new-gnuplot-location)
|
||||||
(profile req (format #f "Gnuplot is now searched at ~a." gnuplot)))
|
(profile req (format #f "Gnuplot is now searched at ~a." gnuplot)))
|
||||||
(profile req)))))
|
(profile req "Please enter a file name of an existing executable.")))))
|
||||||
|
|
||||||
(define (new-profile req)
|
(define (new-profile req)
|
||||||
(profile-space file-name)
|
(profile-space file-name)
|
||||||
|
|
|
@ -51,6 +51,9 @@
|
||||||
,(make-submit-button "Do it")))
|
,(make-submit-button "Do it")))
|
||||||
,footer)))))
|
,footer)))))
|
||||||
(bindings (get-bindings req))
|
(bindings (get-bindings req))
|
||||||
|
;; No error handling as always something is selected. If
|
||||||
|
;; not, the browser did something wrong and we may yield
|
||||||
|
;; an error anyway.
|
||||||
(action (input-field-value select bindings)))
|
(action (input-field-value select bindings)))
|
||||||
|
|
||||||
(if (string=? action action-title)
|
(if (string=? action action-title)
|
||||||
|
@ -58,10 +61,9 @@
|
||||||
(values
|
(values
|
||||||
action
|
action
|
||||||
(filter-map (lambda (checkbox table-element)
|
(filter-map (lambda (checkbox table-element)
|
||||||
(if(with-fatal-error-handler
|
(if (with-fatal-error-handler
|
||||||
(lambda (condition more)
|
(lambda (condition more) #f)
|
||||||
#f)
|
(input-field-value checkbox bindings))
|
||||||
(input-field-value checkbox bindings))
|
|
||||||
table-element
|
table-element
|
||||||
#f))
|
#f))
|
||||||
checkboxes
|
checkboxes
|
||||||
|
|
|
@ -27,12 +27,9 @@
|
||||||
(if (null? checkboxes)
|
(if (null? checkboxes)
|
||||||
sum
|
sum
|
||||||
(loop (+ sum (string->number
|
(loop (+ sum (string->number
|
||||||
(call-with-current-continuation
|
(with-fatal-error-handler
|
||||||
(lambda (exit)
|
(lambda (condition decline) "0")
|
||||||
(with-fatal-error-handler
|
(input-field-value (car checkboxes) bindings))))
|
||||||
(lambda (condition decline)
|
|
||||||
(exit "0"))
|
|
||||||
(input-field-value (car checkboxes) bindings))))))
|
|
||||||
(cdr checkboxes)))))
|
(cdr checkboxes)))))
|
||||||
checkboxes)))
|
checkboxes)))
|
||||||
|
|
||||||
|
@ -43,7 +40,7 @@
|
||||||
`(html (title "Result")
|
`(html (title "Result")
|
||||||
(body
|
(body
|
||||||
(h2 "Result")
|
(h2 "Result")
|
||||||
(p "You've entered " ,result)))))
|
(p "You've entered " ,result ".")))))
|
||||||
|
|
||||||
(define (get-byte-input)
|
(define (get-byte-input)
|
||||||
(let* ((req (send-html/suspend
|
(let* ((req (send-html/suspend
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
(define-structure servlet servlet-interface
|
(define-structure servlet servlet-interface
|
||||||
(open servlets
|
(open servlets
|
||||||
httpd-request
|
httpd-request
|
||||||
|
handle-fatal-error
|
||||||
|
let-opt
|
||||||
scsh
|
scsh
|
||||||
scheme)
|
scheme)
|
||||||
(begin
|
(begin
|
||||||
|
@ -31,46 +33,70 @@
|
||||||
`(option ,(operator-symbol operator)))
|
`(option ,(operator-symbol operator)))
|
||||||
*operator-alist*)))))
|
*operator-alist*)))))
|
||||||
|
|
||||||
(define number-field1 (make-number-input-field))
|
(define (change-operator-callback)
|
||||||
(define number-field2 (make-number-input-field))
|
|
||||||
|
|
||||||
(define change-operator-callback
|
|
||||||
(make-callback
|
(make-callback
|
||||||
(lambda (req)
|
(lambda (req)
|
||||||
(change-operator
|
(change-operator
|
||||||
|
;; This yields an error only when the browser doing wrong.
|
||||||
(input-field-value operator-input-field
|
(input-field-value operator-input-field
|
||||||
(get-bindings req))))))
|
(get-bindings req))))
|
||||||
|
))
|
||||||
|
|
||||||
(define (show-page operator-pair)
|
|
||||||
(send-html
|
(define (make-number-input-field/default default)
|
||||||
`(html
|
(if default
|
||||||
(title "Simple calculator")
|
(make-number-input-field `(@ (value ,default)))
|
||||||
(body (h1 "Simple calculator")
|
(make-number-input-field)))
|
||||||
(servlet-form
|
|
||||||
,(make-callback (lambda (req)
|
(define (show-page operator-pair number1 number2 . maybe-update-text)
|
||||||
(calculate operator-pair (get-bindings req))))
|
(let* ((update-text (:optional maybe-update-text ""))
|
||||||
(table
|
(number-field1 (make-number-input-field/default number1))
|
||||||
(tr (td "Do calculation:"))
|
(number-field2 (make-number-input-field/default number2))
|
||||||
(tr (td ,number-field1)
|
(req
|
||||||
(td ,(operator-symbol operator-pair))
|
(send-html/suspend
|
||||||
(td ,number-field2)
|
(lambda (new-url)
|
||||||
(td " = ")
|
`(html
|
||||||
(td ,(make-submit-button '(@ (value "calculate")))))))
|
(title "Simple calculator")
|
||||||
(hr)
|
(body (h1 "Simple calculator")
|
||||||
(p "You may choose another operator:")
|
(font (@ (color "red")) ,update-text)
|
||||||
(servlet-form
|
(servlet-form
|
||||||
,change-operator-callback
|
,new-url
|
||||||
(table
|
(table
|
||||||
(tr (td ,operator-input-field)
|
(tr (td "Do calculation:"))
|
||||||
(td ,(make-submit-button '(@ (value "change operator")))))))))))
|
(tr (td ,number-field1)
|
||||||
|
(td ,(operator-symbol operator-pair))
|
||||||
|
(td ,number-field2)
|
||||||
|
(td " = ")
|
||||||
|
(td ,(make-submit-button '(@ (value "calculate")))))))
|
||||||
|
(hr)
|
||||||
|
(p "You may choose another operator:")
|
||||||
|
(servlet-form
|
||||||
|
,(change-operator-callback)
|
||||||
|
(table
|
||||||
|
(tr (td ,operator-input-field)
|
||||||
|
(td ,(make-submit-button
|
||||||
|
'(@ (value "change operator"))))))))))))
|
||||||
|
(bindings (get-bindings req)))
|
||||||
|
(let ((number1
|
||||||
|
(with-fatal-error-handler
|
||||||
|
(lambda (c d) #f)
|
||||||
|
(input-field-value number-field1 bindings)))
|
||||||
|
(number2
|
||||||
|
(with-fatal-error-handler
|
||||||
|
(lambda (c d) #f)
|
||||||
|
(input-field-value number-field2 bindings))))
|
||||||
|
(if number1
|
||||||
|
(if number2
|
||||||
|
(calculate operator-pair number1 number2)
|
||||||
|
(show-page operator-pair number1 number2 "Please enter a valid second number."))
|
||||||
|
(show-page operator-pair number1 number2 "Please enter a valid first number."))
|
||||||
|
)))
|
||||||
|
|
||||||
(define (change-operator to-operation)
|
(define (change-operator to-operation)
|
||||||
(show-page to-operation))
|
(show-page to-operation #f #f))
|
||||||
|
|
||||||
(define (calculate operator-pair bindings)
|
(define (calculate operator-pair number1 number2)
|
||||||
(let ((number1 (input-field-value number-field1 bindings))
|
(let ((operator (operator-operator operator-pair)))
|
||||||
(number2 (input-field-value number-field2 bindings))
|
|
||||||
(operator (operator-operator operator-pair)))
|
|
||||||
(show-result number1 (operator-symbol operator-pair) number2
|
(show-result number1 (operator-symbol operator-pair) number2
|
||||||
(operator number1 number2))))
|
(operator number1 number2))))
|
||||||
|
|
||||||
|
@ -82,6 +108,6 @@
|
||||||
" = " ,result)))))
|
" = " ,result)))))
|
||||||
|
|
||||||
(define (main req)
|
(define (main req)
|
||||||
(show-page (car *operator-alist*))
|
(show-page (car *operator-alist*) #f #f)
|
||||||
(error "This does not return"))
|
(error "This does not return"))
|
||||||
))
|
))
|
Loading…
Reference in New Issue