Hi Alexander,

Your solution is a little more in the functional style than what I did (below). Compared to mine it changes the semantics from a stand-alone statement to an expression returning a value, but that really is not a significant difference. I do like that it encapsulates the definition of the error return variable.

Thanks,
George


(define-syntax with-database-connection
  (syntax-rules ()
    ((with-database-connection conn err body ...)
     ; -- start template
     (let/ec fail-network
       (with-handlers [
                       (exn:fail:network?
                        (lambda (e)
                          (set! err "database connection error")
                          (fail-network)))
                      ]
         (let [
               (conn (connect-database))
               (sql-cmd #f)
              ]
           (let/ec fail-sql
             (with-handlers [
                             (exn:fail:sql?
                              (lambda (e)
                                (let [(info (exn:fail:sql-info e))]
                                  (set! err (cdr (assoc 'message info)))
                                  (fail-sql))))
                            ]

               (call-with-transaction conn
                 (lambda ()

                   body ...

                   )
                 #:isolation 'repeatable-read)

               )
             (disconnect conn))
           )))
     ; -- end template
     )))





On 12/8/2014 9:12 PM, Alexander D. Knauth wrote:
Sorry I missed this:

On Dec 8, 2014, at 6:45 PM, George Neuner <[email protected] <mailto:[email protected]>> wrote:

and code in the  <body> needs to reference “db"

(define (boiler-plate/f f)
  (define err-msg #f)
  (let/ec fail-network
    (with-handlers ([exn:fail:network?
                     (lambda (e)
                       (set! err-msg "database connection error")
 (fail-network err-msg))])
      (let ([db (connect-database)])
        (define (thunk)
          (f db))
        (let/ec fail-sql
          (with-handlers ([exn:fail:sql?
                           (lambda (e)
                             (let ([info (exn:fail:sql-info e)])
 (set! err-msg (cdr (assoc 'message info)))
 (fail-sql err-msg)))])
            (call-with-transaction db thunk #:isolation 'repeatable-read))
          (disconnect db)
          err-msg)))))

(define-syntax-rule (boiler-plate db body ...)
  (boiler-plate/f (λ (db) body ...)))

(let ()
  (define err-msg
    (boiler-plate
     db
     < body ... >
     ))
  ; err-msg needed here
  err-msg
  )


____________________
  Racket Users list:
  http://lists.racket-lang.org/users

Reply via email to