-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcl-win32-errors.lisp
More file actions
59 lines (49 loc) · 2.47 KB
/
cl-win32-errors.lisp
File metadata and controls
59 lines (49 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
;;;; cl-win32-errors.lisp
(in-package #:cl-win32-errors)
;;; ----------------------------------------------------------------------------
;;; Error Code Database and API
;;; ----------------------------------------------------------------------------
(defvar *win32-error-map* (make-hash-table)
"A hash table mapping numeric error codes to their details.")
(defun register-error (code symbol description)
"Registers a Win32 error code and its details in the database."
(setf (gethash code *win32-error-map*)
`(:code ,code
:hex ,(format nil "0x~X" code)
:symbol ,symbol
:description ,description)))
(defun get-error-details (error-code)
"Looks up a Win32 error code and returns a plist of its details.
Returns NIL if the code is not found."
(gethash error-code *win32-error-map*))
(defun format-error-message (error-code &key (verbosity :default))
"Formats a Win32 error code into a human-readable string.
VERBOSITY can be :DEFAULT or :VERBOSE."
(let ((details (get-error-details error-code)))
(if details
(case verbosity
(:verbose (format nil "Win32 Error ~A (~A/~A): ~A"
error-code
(getf details :hex)
(getf details :symbol)
(getf details :description)))
(t (format nil "Win32 Error ~A: ~A"
error-code
(getf details :description))))
(format nil "Unknown Win32 Error Code: ~A" error-code))))
;;; ----------------------------------------------------------------------------
;;; Condition System Integration
;;; ----------------------------------------------------------------------------
(define-condition win32-error (error)
((code :initarg :code :reader win32-error-code)
(details :initarg :details :reader win32-error-details))
(:report (lambda (condition stream)
;; The condition report will use the verbose format by default.
(format stream "A Windows API call failed: ~A"
(format-error-message (win32-error-code condition) :verbosity :verbose)))))
(defun win32-error-symbol (condition)
"Returns the symbolic name of the error, e.g., :ERROR_ACCESS_DENIED."
(getf (win32-error-details condition) :symbol))
(defun win32-error-description (condition)
"Returns the descriptive string for the error."
(getf (win32-error-details condition) :description))