Fixed DEFMETHOD arg parsing.
[iolib/alendvai.git] / net.sockets / conditions.lisp
blob56ba6b9267d0aaabd41560c7456a36fbe3929d82
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; indent-tabs-mode: nil -*-
2 ;;;
3 ;;; --- Socket conditions.
4 ;;;
6 (in-package :net.sockets)
8 (defgeneric error-code (err)
9 (:method ((err system-error))
10 (osicat-sys:system-error-code err)))
12 (defgeneric error-identifier (err)
13 (:method ((err system-error))
14 (osicat-sys:system-error-identifier err)))
16 (defgeneric error-message (err)
17 (:method ((err system-error))
18 (osicat-sys:system-error-message err)))
20 ;;;; Socket Errors
22 (define-condition socket-error (nix:posix-error) ())
24 (defmethod print-object ((socket-error socket-error) stream)
25 (print-unreadable-object (socket-error stream :type t :identity nil)
26 (let ((code (osicat-sys:system-error-code socket-error)))
27 (format stream "~S ~S ~S"
28 (or code "[Unknown code]")
29 (error-identifier socket-error)
30 (if code (nix:strerror code) "[Can't get error string.]")))))
32 (defvar *socket-error-map* (make-hash-table :test 'eq))
34 (defmacro define-socket-error (name identifier &optional documentation)
35 `(progn
36 (setf (gethash ,identifier *socket-error-map*) ',name)
37 (define-condition ,name (,(nix::get-posix-error-condition identifier)
38 socket-error) ()
39 (:default-initargs :code ,(foreign-enum-value 'socket-error-values
40 identifier)
41 :identifier ,identifier)
42 (:documentation ,(or documentation (nix:strerror identifier))))))
44 (defun lookup-socket-error (keyword)
45 (gethash keyword *socket-error-map*))
47 (define-condition unknown-socket-error (socket-error) ()
48 (:documentation "Error signalled upon finding an unknown socket error."))
50 (define-socket-error socket-invalid-argument :einval)
51 (define-socket-error socket-address-in-use-error :eaddrinuse)
52 (define-socket-error socket-address-not-available-error :eaddrnotavail)
53 (define-socket-error socket-network-down-error :enetdown)
54 (define-socket-error socket-network-reset-error :enetreset)
55 (define-socket-error socket-network-unreachable-error :enetunreach)
56 (define-socket-error socket-no-network-error :enonet)
57 (define-socket-error socket-connection-aborted-error :econnaborted)
58 (define-socket-error socket-connection-reset-error :econnreset)
59 (define-socket-error socket-connection-refused-error :econnrefused)
60 (define-socket-error socket-connection-timeout-error :etimedout)
61 (define-socket-error socket-connection-in-progress-error :einprogress)
62 (define-socket-error socket-endpoint-shutdown-error :eshutdown)
63 (define-socket-error socket-no-buffer-space-error :enobufs)
64 (define-socket-error socket-host-down-error :ehostdown)
65 (define-socket-error socket-host-unreachable-error :ehostunreach)
66 (define-socket-error socket-already-connected-error :eisconn)
67 (define-socket-error socket-not-connected-error :enotconn)
68 (define-socket-error socket-option-not-supported-error :enoprotoopt)
69 (define-socket-error socket-operation-not-supported-error :eopnotsupp)
71 (defun %socket-error (id code)
72 (when-let (err (lookup-socket-error id))
73 (error err :identifier id :code code)))
75 ;;; Used in the ERRNO-WRAPPER foreign type.
76 (defun signal-socket-error (&optional return-value)
77 (declare (ignore return-value))
78 (let* ((errno (nix:get-errno))
79 (kw (foreign-enum-keyword 'socket-error-values
80 errno :errorp nil)))
81 (or (%socket-error kw errno)
82 (error (nix::make-posix-error errno)))))