Xach requested better error reporting from load-foreign-library, so
please review this patch

-- 
Stelian Ionescu a.k.a. fe[nl]ix
Quidquid latine dictum sit, altum videtur.
http://common-lisp.net/project/iolib

diff --git a/src/libraries.lisp b/src/libraries.lisp
index 0134a5e..481e1ac 100644
--- a/src/libraries.lisp
+++ b/src/libraries.lisp
@@ -249,16 +249,26 @@ the USE-FOREIGN-LIBRARY macro."
 ;;; signal this error when something goes wrong. We ignore the host's
 ;;; error. We should probably reuse its error message.
 
-(define-condition load-foreign-library-error (simple-error)
+(define-condition foreign-library-error (simple-error)
   ())
 
-(defun read-new-value ()
-  (format *query-io* "~&Enter a new value (unevaluated): ")
-  (force-output *query-io*)
-  (read *query-io*))
+(defun fl-load-error (control &rest arguments)
+  (error 'foreign-library-error
+         :format-control control
+         :format-arguments arguments))
 
-(defun fl-error (control &rest arguments)
+(define-condition load-foreign-library-error (foreign-library-error)
+  ((pathname :initarg :pathname :reader foreign-library-error-pathname))
+  (:report (lambda (condition stream)
+             (format stream "Error loading ~S: ~A"
+                     (foreign-library-pathname condition)
+                     (apply #'format
+                            (simple-condition-format-control condition)
+                            (simple-condition-format-arguments condition))))))
+
+(defun fl-load-error (pathname control &rest arguments)
   (error 'load-foreign-library-error
+         :pathname pathname
          :format-control control
          :format-arguments arguments))
 
@@ -271,13 +281,12 @@ it signals a LOAD-FOREIGN-LIBRARY-ERROR."
   (let ((framework (find-darwin-framework framework-name)))
     (if framework
         (load-foreign-library-path name (native-namestring framework))
-        (fl-error "Unable to find framework ~A" framework-name))))
+        (fl-load-error framework-name "unable to find framework"))))
 
-(defun report-simple-error (name error)
-  (fl-error "Unable to load foreign library (~A).~%  ~A"
-            name
-            (format nil "~?" (simple-condition-format-control error)
-                    (simple-condition-format-arguments error))))
+(defun wrap-host-simple-error (name error)
+  (fl-load-error name
+                 (simple-condition-format-control error)
+                 (simple-condition-format-arguments error)))
 
 ;;; FIXME: haven't double checked whether all Lisps signal a
 ;;; SIMPLE-ERROR on %load-foreign-library failure.  In any case they
@@ -289,15 +298,15 @@ ourselves."
   (handler-case
       (values (%load-foreign-library name path)
               (pathname path))
-    (error (error)
+    (simple-error (error)
       (if-let (file (find-file path (append search-path
                                             *foreign-library-directories*)))
         (handler-case
             (values (%load-foreign-library name (native-namestring file))
                     file)
           (simple-error (error)
-            (report-simple-error name error)))
-        (report-simple-error name error)))))
+            (wrap-host-simple-error name error)))
+        (wrap-host-simple-error name error)))))
 
 (defun try-foreign-library-alternatives (name library-list)
   "Goes through a list of alternatives and only signals an error when
@@ -310,7 +319,7 @@ none of alternatives were successfully loaded."
           (values handle pathname)))))
   ;; Perhaps we should show the error messages we got for each
   ;; alternative if we can figure out a nice way to do that.
-  (fl-error "Unable to load any of the alternatives:~%   ~S" library-list))
+  (fl-load-error name "Unable to load any of the alternatives:~%   ~S" library-list))
 
 (defparameter *cffi-feature-suffix-map*
   '((:windows . ".dll")
@@ -377,6 +386,11 @@ This will need to be extended as we test on more OSes."
     (pathname (namestring thing))
     (t        thing)))
 
+(defun read-new-value ()
+  (format *query-io* "~&Enter a new value (unevaluated): ")
+  (force-output *query-io*)
+  (read *query-io*))
+
 (defun load-foreign-library (library &key search-path)
   "Loads a foreign LIBRARY which can be a symbol denoting a library defined
 through DEFINE-FOREIGN-LIBRARY; a pathname or string in which case we try to
diff --git a/src/package.lisp b/src/package.lisp
index 4719914..acd32cc 100644
--- a/src/package.lisp
+++ b/src/package.lisp
@@ -97,6 +97,8 @@
    #:list-foreign-libraries
    #:define-foreign-library
    #:load-foreign-library
+   #:foreign-library-error
+   #:foreign-library-error-pathname
    #:load-foreign-library-error
    #:use-foreign-library
    #:close-foreign-library

Attachment: signature.asc
Description: This is a digitally signed message part

_______________________________________________
cffi-devel mailing list
cffi-devel@common-lisp.net
http://lists.common-lisp.net/cgi-bin/mailman/listinfo/cffi-devel

Reply via email to