Small fix of assertion message in INITIALIZE-INSTANCE of DYNAMIC-BUFFER.
[iolib.git] / net.sockets / conditions.lisp
blob0bc73f6d05dfe4aeb0d68cd19550eca2737dafa8
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, FD: ~S"
28 (or code "[Unknown code]")
29 (error-identifier socket-error)
30 (if code (nix:strerror code) "[Can't get error string.]")
31 (nix:posix-error-object socket-error)))))
33 (defvar *socket-error-map* (make-hash-table :test 'eq))
35 (defmacro define-socket-error (name identifier &optional documentation)
36 `(progn
37 (setf (gethash ,identifier *socket-error-map*) ',name)
38 (define-condition ,name (,(nix::get-posix-error-condition identifier)
39 socket-error) ()
40 (:default-initargs :code ,(foreign-enum-value 'socket-error-values
41 identifier)
42 :identifier ,identifier)
43 (:documentation ,(or documentation (nix:strerror identifier))))))
45 (defun lookup-socket-error (keyword)
46 (gethash keyword *socket-error-map*))
48 (define-condition unknown-socket-error (socket-error) ()
49 (:documentation "Error signalled upon finding an unknown socket error."))
51 (define-socket-error socket-invalid-argument :einval)
52 (define-socket-error socket-address-in-use-error :eaddrinuse)
53 (define-socket-error socket-address-not-available-error :eaddrnotavail)
54 (define-socket-error socket-network-down-error :enetdown)
55 (define-socket-error socket-network-reset-error :enetreset)
56 (define-socket-error socket-network-unreachable-error :enetunreach)
57 (define-socket-error socket-no-network-error :enonet)
58 (define-socket-error socket-connection-aborted-error :econnaborted)
59 (define-socket-error socket-connection-reset-error :econnreset)
60 (define-socket-error socket-connection-refused-error :econnrefused)
61 (define-socket-error socket-connection-timeout-error :etimedout)
62 (define-socket-error socket-connection-in-progress-error :einprogress)
63 (define-socket-error socket-endpoint-shutdown-error :eshutdown)
64 (define-socket-error socket-no-buffer-space-error :enobufs)
65 (define-socket-error socket-host-down-error :ehostdown)
66 (define-socket-error socket-host-unreachable-error :ehostunreach)
67 (define-socket-error socket-already-connected-error :eisconn)
68 (define-socket-error socket-not-connected-error :enotconn)
69 (define-socket-error socket-option-not-supported-error :enoprotoopt)
70 (define-socket-error socket-operation-not-supported-error :eopnotsupp)
72 (defun %socket-error (id code socket)
73 (when-let (err (lookup-socket-error id))
74 (error err :identifier id :code code :object socket)))
76 ;;; Used in the ERRNO-WRAPPER foreign type.
77 (defun signal-socket-error (&optional return-value socket)
78 (declare (ignore return-value))
79 (let* ((errno (nix:get-errno))
80 (kw (foreign-enum-keyword 'socket-error-values
81 errno :errorp nil)))
82 (or (%socket-error kw errno socket)
83 (error (nix::make-posix-error errno socket)))))