2
votes

In sbcl I can handle errors as I do here when opening a nonexistent file:

(require "SB-POSIX")
(let* (fd)
   (handler-case
       (setf fd (sb-posix:open "w" sb-posix:o-rdonly))
     (sb-posix:syscall-error (c)
       (princ "error ")
       (princ (sb-posix:syscall-errno c))
       (princ " during ")
       (princ (sb-posix:syscall-name c))
       (terpri))))
error 2 during OPEN-WITHOUT-MODE

If I don't catch the error, the top part of the result looks like this:

 (require "SB-POSIX")
 (let* (fd)
   (setf fd (sb-posix:open "w" sb-posix:o-rdonly)))
Unhandled SB-POSIX:SYSCALL-ERROR:
  Error in SB-POSIX::OPEN-WITHOUT-MODE: No such file or directory (2)

Backtrace for: #<SB-THREAD:THREAD "main thread" RUNNING {1001976AB3}>

See that "No such file or directory" in there? Is there a way my code can have access to that string, so my error handler can say something a little less dorky than "error 2"?

1
(sb-int:strerror errno) seems to be how the conditions report function gets it.jkiiski
@jkiiski : Perfect! If you wish to make it an official answer, I'll upvote it and mark it as the preferred solution.Bill Evans at Mariposa

1 Answers

5
votes

The report function for the condition calls SB-INT:STRERROR with the error number to get the string. That in turn is just a simple wrapper around the platform specific way of getting the error code description.

If you're using Emacs/Slime and have SBCL source code available you can use M-. to jump to the definition of functions/classes/etc. which is very useful for finding out how the internals work.