F3XTeam / RBX-Try-Library

A library for controlling the flow of error-prone, interdependent functions.
12 stars 3 forks source link

Upgrade to v2.0 #7

Closed Validark closed 5 years ago

Validark commented 7 years ago

Changes:

Fixes #3 Fixes #4 Fixes #5

Here is a test bench if you can't believe it works:

local HttpService = game:GetService("HttpService")

-- Get a clean require of the Module
local Module = script.Try:Clone()
script.Try:Destroy()
Module.Parent = script

local Try = require(Module)

local HttpService = game:GetService('HttpService')

local Attempt = Try(wait, 2)
print("Hey!")
Attempt:Wait()
    :Then(function(...)
        wait(1)
        print("This was returned by wait(2)", ...)
    end)
print("The Attempt has finished yielding!")

local x = Try(wait, 2)

    -- Try hashing the time
    :Then(function(Delta, ElapsedTime)
        print("Done yielding, firing GetAsync")
        return HttpService:GetAsync('http://md5.jsontest.com/?text=' .. Delta)
    end)

    :Wait()

    :Catch(function()
        print("Caught error")
    end)

    -- Try decoding the response
    :Then(function(RawResponse)
        print("Decoding")
        return HttpService:JSONDecode(RawResponse)
    end)

    :Catch(function()
        print("Error!")
    end)

    :Then(function(Response)
        print('Input:', Response.original, '\nMD5:', Response.md5)
        return 0
    end)

    :Then(function()
        HttpService:GetAsync('http://httpstat.us/404')
    end)

    -- Catch when the URL doesn't exist
    :Catch('HTTP 404', function (Error, Stack, Attempt)
        warn('Not found, error:', Error)
    end)

    -- Catch any other error
    :Catch(function (Error, Stack, Attempt)
        warn('Unknown error:', Error)
    end)

print("Yo!")

wait(5)
print("Going")
x
    :Then(function(x)
        print(
            "Running purposeful error", x
        )
        return HttpService:GetAsync('http://httpstat.us/503')
    end)

    :Then(function (Data)
        print('Found', Data)
    end)

    -- Catch when the server is having issues and retry
    :Catch('HTTP 503', 'Timeout was reached', function(Error, Stack, Attempt)

        -- Limit the number of retries to 3
        if Attempt.RetryCount < 3 then

            -- Space out each retry
            local BackoffTime = Attempt.RetryCount * 3 + 3
            print('Retrying in', BackoffTime, 'seconds...')
            wait(BackoffTime)

            -- Retry the attempt
            return Attempt:Retry()
        else
            print('Failed')
        end
        print("Function finished")
    end)

    -- Catch any other errors
    :Catch(function (Error, Stack, Attempt)
        print('Unknown error:', Error)
    end)

    :Wait()

print("Again!")
x
    :Then(function()
        print("Yaya!")
    end)

    :Catch(function()
        print("Houston, we have a problem")
    end)

print("RetryCount", x.RetryCount)
Validark commented 6 years ago

There was an issue with Catch functions having inaccurate debug.traceback data, so I made Attempts track themselves.