Previous: , Up: Guile API Conventions   [Contents][Index]

3.5 Exception Handling

GnuTLS errors are implemented as Scheme exceptions (see exceptions in Guile in The GNU Guile Reference Manual). Each time a GnuTLS function returns an error, an exception with key gnutls-error is raised. The additional arguments that are thrown include an error code and the name of the GnuTLS procedure that raised the exception. The error code is pretty much like an enumerate value: it is one of the error/ variables exported by the (gnutls) module (see Enumerates and Constants). Exceptions can be turned into error messages using the error->string procedure.

The following examples illustrates how GnuTLS exceptions can be handled:

(let ((session (make-session connection-end/server)))

  ;; ...

  (catch 'gnutls-error
    (lambda ()
      (handshake session))
    (lambda (key err function . currently-unused)
      (format (current-error-port)
              "a GnuTLS error was raised by `~a': ~a~%"
              function (error->string err)))))

Again, error values can be compared using eq?:

    ;; `gnutls-error' handler.
    (lambda (key err function . currently-unused)
      (if (eq? err error/fatal-alert-received)
          (format (current-error-port)
                  "a fatal alert was caught!~%")
          (format (current-error-port)
                  "something bad happened: ~a~%"
                  (error->string err))))

Note that the catch handler is currently passed only 3 arguments but future versions might provide it with additional arguments. Thus, it must be prepared to handle more than 3 arguments, as in this example.