|
| 1 | +# cl-win32-errors |
| 2 | + |
| 3 | +A Common Lisp library for translating cryptic Windows API error codes into human-readable information. When working with CFFI, Windows functions often return numeric error codes. This library provides a simple interface to convert these codes into their symbolic names, hexadecimal values, and descriptive strings, complete with a condition system for error handling. |
| 4 | + |
| 5 | +This project is the logical companion to [cl-win32-types](https://github.com/ItsMeForLua/cl-win32-types). |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +- **Error Mapping:** Translates over 2,300 system error codes across numerous categories, including core system, networking, RPC, security, Active Directory, DNS, and more. |
| 10 | + |
| 11 | +- **Extensible:** The library is structured to make adding new error code definitions simple. |
| 12 | + |
| 13 | +- **Condition System:** Provides a `win32-error` condition for idiomatic Lisp error handling. |
| 14 | + |
| 15 | +- **User-Friendly API:** Simple functions to get error details or formatted strings. |
| 16 | + |
| 17 | +- **ASDF System:** Ready for straightforward use via Quicklisp. The library and its tests are defined as separate systems. |
| 18 | + |
| 19 | + |
| 20 | +## Installation (for Users) |
| 21 | + |
| 22 | +Once available in a Quicklisp distribution, you can load the library with: |
| 23 | + |
| 24 | +``` lisp |
| 25 | +(ql:quickload :cl-win32-errors) |
| 26 | +``` |
| 27 | + |
| 28 | +## Developer Setup (for Contributors) |
| 29 | + |
| 30 | +This project uses [Qlot](https://github.com/fukamachi/qlot "null") to manage and lock dependency versions for reproducible builds. |
| 31 | + |
| 32 | +1. **Install Qlot** (one-time setup): |
| 33 | + |
| 34 | + ``` powershell |
| 35 | + ros install qlot |
| 36 | + ``` |
| 37 | + |
| 38 | +2. Install Project Dependencies: |
| 39 | + |
| 40 | + This command reads the qfile and installs the exact versions of all dependencies into a local .qlot/ directory. |
| 41 | + |
| 42 | + ``` powershell |
| 43 | + # Make sure you are in the project's root directory |
| 44 | + qlot install |
| 45 | + ``` |
| 46 | + |
| 47 | +3. Start a REPL: |
| 48 | + |
| 49 | + To work on the project, start your REPL using qlot exec. This ensures your Lisp session uses the project's local dependencies. |
| 50 | + |
| 51 | + ``` powershell |
| 52 | + qlot exec ros run |
| 53 | + ``` |
| 54 | + |
| 55 | + |
| 56 | +## Quick Start / Usage Example |
| 57 | + |
| 58 | +Here is the most common use case for the library. All functions are exported from the `cl-win32-errors` package. |
| 59 | + |
| 60 | +``` lisp |
| 61 | +;; Load the library |
| 62 | +(ql:quickload :cl-win32-errors) |
| 63 | +
|
| 64 | +;; Look up an error code |
| 65 | +(cl-win32-errors:get-error-details 2) |
| 66 | +;; => (:CODE 2 :HEX "0x2" :SYMBOL :ERROR_FILE_NOT_FOUND |
| 67 | +;; :DESCRIPTION "The system cannot find the file specified.") |
| 68 | +
|
| 69 | +;; Get a formatted string directly |
| 70 | +(cl-win32-errors:format-error-message 5 :verbosity :verbose) |
| 71 | +;; => "Win32 Error 5 (0x5/ERROR_ACCESS_DENIED): Access is denied." |
| 72 | +``` |
| 73 | + |
| 74 | +## API Reference |
| 75 | + |
| 76 | +### `get-error-details` (error-code) |
| 77 | + |
| 78 | +Looks up a numeric Win32 error code and returns a property list (plist) of its details. |
| 79 | + |
| 80 | +**Parameters:** |
| 81 | + |
| 82 | +- `error-code`: An integer representing the Windows error code. |
| 83 | + |
| 84 | +**Returns:** A plist with the keys `:code`, `:hex`, `:symbol`, and `:description`, or `NIL` if the code is not found. |
| 85 | + |
| 86 | +**Example:** |
| 87 | + |
| 88 | +``` lisp |
| 89 | +(get-error-details 5) |
| 90 | +;; => (:CODE 5 :HEX "0x5" :SYMBOL :ERROR_ACCESS_DENIED :DESCRIPTION "Access is denied.") |
| 91 | +``` |
| 92 | + |
| 93 | +### `format-error-message` (error-code &key (verbosity :default)) |
| 94 | + |
| 95 | +Formats a Win32 error code into a human-readable string. |
| 96 | + |
| 97 | +**Parameters:** |
| 98 | + |
| 99 | +- `error-code`: An integer representing the Windows error code. |
| 100 | + |
| 101 | +- `:verbosity`: A keyword, either `:default` or `:verbose`. The `:verbose` option includes the hex code and symbolic name. |
| 102 | + |
| 103 | + |
| 104 | +**Returns:** A formatted string. |
| 105 | + |
| 106 | +**Example:** |
| 107 | + |
| 108 | +``` lisp |
| 109 | +(format-error-message 87 :verbosity :verbose) |
| 110 | +;; => "Win32 Error 87 (0x57/ERROR_INVALID_PARAMETER): The parameter is incorrect." |
| 111 | +``` |
| 112 | + |
| 113 | +## Condition System |
| 114 | + |
| 115 | +The library provides a `win32-error` condition for idiomatic error handling. You can signal it after a failed API call and use the provided accessors to inspect its details within a `handler-case`. |
| 116 | + |
| 117 | +**Condition Accessors:** |
| 118 | + |
| 119 | +- `win32-error-code` (condition): Returns the numeric error code. |
| 120 | + |
| 121 | +- `win32-error-symbol` (condition): Returns the keyword symbol for the error. |
| 122 | + |
| 123 | +- `win32-error-description` (condition): Returns the descriptive string for the error. |
| 124 | + |
| 125 | + |
| 126 | +**Example of handling an error:** |
| 127 | + |
| 128 | +``` lisp |
| 129 | +(handler-case |
| 130 | + ;; For example, signal an error after a failed API call |
| 131 | + (error 'cl-win32-errors:win32-error :code 87) |
| 132 | + (cl-win32-errors:win32-error (e) |
| 133 | + (format t "Caught a Win32 Error!~%") |
| 134 | + (format t " Code: ~A~%" (cl-win32-errors:win32-error-code e)) |
| 135 | + (format t " Symbol: ~A~%" (cl-win32-errors:win32-error-symbol e)) |
| 136 | + (format t " Description: ~A~%" (cl-win32-errors:win32-error-description e)))) |
| 137 | +``` |
| 138 | + |
| 139 | +## Running Tests |
| 140 | + |
| 141 | +To run the test suite from the command line, use `qlot exec`: |
| 142 | + |
| 143 | +``` powershell |
| 144 | +qlot exec ros run --eval "(asdf:test-system :cl-win32-errors/tests)" --quit |
| 145 | +``` |
| 146 | + |
| 147 | +Alternatively, from within a REPL started with `qlot exec ros run`: |
| 148 | + |
| 149 | +``` lisp |
| 150 | +(asdf:test-system :cl-win32-errors/tests) |
| 151 | +``` |
| 152 | + |
| 153 | +## Contributing |
| 154 | + |
| 155 | +Bug reports and pull requests are welcome on GitHub. Please ensure the test suite passes before submitting a pull request. |
| 156 | + |
| 157 | +## License |
| 158 | + |
| 159 | +This project is licensed under the MIT License. |
0 commit comments