Re: Minimal foreign error API Lassi Kortela 28 Jul 2020 13:02 UTC

The following is a full sample implementation of the SRFI with the
changes discussed in this thread. There are probably subtle bugs. I left
out `raise-continuable-foreign-error` and `foreign-error->string`.

(define-library (srfi 198)
   (export make-foreign-error raise-foreign-error
           foreign-error? foreign-error-ref)
   (import (scheme base) (srfi 69))
   (begin

     (define-record-type foreign-error
       (%make-foreign-error table)
       foreign-error?
       (table %foreign-error-table))

     (define (make-foreign-error . plist)
       (let ((table (make-hash-table)))
         (let loop ((tail plist))
           (cond ((null? tail)
                  (%make-foreign-error table))
                 ((and (pair? tail) (symbol? (car tail)) (pair? (cdr tail)))
                  (hash-table-set! table (car tail) (cadr tail))
                  (loop (cddr tail)))
                 (else (error "Malformed property list" plist))))))

     (define (raise-foreign-error . plist)
       (raise (apply make-foreign-error plist)))

     (define (foreign-error-ref ferr property . args)
       (let* ((table (%foreign-error-table ferr))
              (value (hash-table-ref/default table property #f)))
         (if (procedure? value) (apply value args) value)))))