From 266e265e20082f9df94e980b5a5875dd20a07647 Mon Sep 17 00:00:00 2001 From: Eryn Lynn Date: Mon, 11 May 2020 15:43:35 -0400 Subject: [PATCH] Allow rejection with error objects --- lib/init.lua | 11 ++++++++++- lib/init.spec.lua | 10 ++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/init.lua b/lib/init.lua index 6e49b84..82352bd 100644 --- a/lib/init.lua +++ b/lib/init.lua @@ -131,11 +131,20 @@ end local function makeErrorHandler(traceback) + assert(traceback ~= nil) + return function(err) + -- If the error object is already a table, forward it directly. + -- Should we extend the error here and add our own trace? + + if type(err) == "table" then + return err + end + return Error.new({ error = err, kind = Error.Kind.ExecutionError, - trace = debug.traceback(err, 2), + trace = debug.traceback(tostring(err), 2), context = "Promise created at:\n\n" .. traceback }) end diff --git a/lib/init.spec.lua b/lib/init.spec.lua index a88007c..1b5622d 100644 --- a/lib/init.spec.lua +++ b/lib/init.spec.lua @@ -925,6 +925,16 @@ return function() expect(errorText:find("errortext")).to.be.ok() end) + it("should reject with error objects", function() + local object = {} + local success, value = Promise.try(function() + error(object) + end):_unwrap() + + expect(success).to.equal(false) + expect(value).to.equal(object) + end) + it("should catch asynchronous errors", function() local bindable = Instance.new("BindableEvent") local promise = Promise.try(function()