Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/stein197/lua-catchify
Tiny try-catch-finally statement for Lua
https://github.com/stein197/lua-catchify
Last synced: 12 days ago
JSON representation
Tiny try-catch-finally statement for Lua
- Host: GitHub
- URL: https://github.com/stein197/lua-catchify
- Owner: stein197
- License: mit
- Created: 2021-11-30T14:20:54.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2021-11-30T16:02:39.000Z (almost 3 years ago)
- Last Synced: 2024-08-01T19:57:42.411Z (3 months ago)
- Language: Lua
- Size: 3.91 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Tiny try-catch-finally implementation for Lua
![](https://img.shields.io/github/license/stein197/lua-catchify)
![](https://img.shields.io/github/v/tag/stein197/lua-catchify?label=Version)
![](https://img.shields.io/luarocks/v/stein197/catchify)Lua does not have a common syntax sugar to wrap the code that will possibly lead to errors. This package provides a very simple way to bring this sugar to Lua.
## Installation
Via LuaRocks:
```
luarocks install catchify
```
Or just download and require `init.lua` file from this repo.## Usage
The whole concept can be illustrated in a single code piece below:
```lua
local try = require "catchify"
try(function ()
error "Something's wrong!"
end):catch(function (e) -- e will contain error message
print(e) -- > stdin:3: Something's wrong!
end):finally(function () -- Will be executed anyway
print "Finally here!"
end)
```You can pass a table containing single function instead of a function to make syntax more "curly":
```lua
try {
function ()
error "Something's wrong!"
end
} :catch {
function (e)
print(e)
end
} :finally {
function ()
print "Finally here!"
end
}
```## Testing
Install luaunit package:
```
luarocks install luaunit
```
Then run from the console:
```
lua test.lua
```