OHakimen / MorePeripherals

Adds new peripherals for CC:Tweaked
MIT License
8 stars 2 forks source link

Lua functions should return false and an error message instead of throwing an exception #24

Closed crazyvinvin closed 1 year ago

crazyvinvin commented 1 year ago

Enhancement

Many lua functions from the peripherals are currently throwing lua exceptions if it can't succeed. CC Tweaked handles this by returning a succes boolean and a possible error message. This takes out a lot of pcalling and clumsy lua exception handling and function wrapping.

Currently it looks something like this:

local function trade(param1, param2)
    -- wrap peripheral, trade item, etc
end

local function wrapTrade()
    local param1 = "x"
    local param2 = "y"
    trade(param1, param2)
end

local function handleTrade()
    local success, error = pcall(wrapTrade)
    if not success then
        -- handle error
    end
end

Which could be (and is much better to write and maintain in my opinion):

local function trade(param1, param2)
    -- wrap peripheral, trade item, etc
end

local function handleTrade()
    local param1 = "x"
    local param2 = "y"
    local success, error = trade(param1, param2)
    if not success then
        -- handle error
    end
end

An example from CC tweaked would be the turtle.inspect or turtle.place functions, returning false and an error message when they don't succeed. The best thing about changing it in the more peripherals mod is that it would make error handling the same in many more situations.

OHakimen commented 1 year ago

Hello so, this will require me to do some rewriting on the peripherals (not too hard to do), i'm gonna start working on it now, probably will release them tomorrow, this and also the other issues around the beehive

crazyvinvin commented 1 year ago

Amazing tysm!

If you need anything, just let us know, we'll be happy to test. (As soon as we wake up ;))

OHakimen commented 1 year ago

Probably i'm gonna start doing some releases here on github, as dev test builds, so people can report issues before a stable release is made