Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,3 +5,4 @@
!/.lua-format
!/.githooks/
/output/
/dist/
6 changes: 6 additions & 0 deletions changelog.d/20260714_090000_yura_errorwithname.md
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
### Added

- `Effect.Exception.errorWithName` (upstream v6.1.0 parity, #269). Following
the fork's string-`Error` model, the name is unobservable: `message`
returns the supplied message and `name` keeps answering the constant
`"Error"`, the same precedent as `errorWithCause`'s dropped cause.
8 changes: 8 additions & 0 deletions src/Effect/Exception.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,6 +12,14 @@ export function errorWithCause(msg) {
};
}

export function errorWithName(msg) {
return function(name) {
const e = new Error(msg);
e.name = name;
return e;
};
}

export function message(e) {
return e.message;
}
Expand Down
7 changes: 7 additions & 0 deletions src/Effect/Exception.lua
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,6 +8,13 @@ return {
return msg
end
end),
errorWithName = (function(msg)
return function(_name)
-- The name is unobservable through this binding (`name` always answers
-- the constant "Error"), so it is dropped like errorWithCause's cause.
return msg
end
end),
message = (function(err) return err end),
name = (function(_err)
-- JS `e.name || "Error"`; the string-Error model carries no name field.
Expand Down
4 changes: 4 additions & 0 deletions src/Effect/Exception.purs
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,7 @@ module Effect.Exception
, catchException
, error
, errorWithCause
, errorWithName
, message
, name
, stack
Expand DownExpand Up@@ -35,6 +36,9 @@ foreign import error :: String -> Error
-- | Create a JavaScript error, specifying a message and a cause
foreign import errorWithCause :: String -> Error -> Error

-- | Create a JavaScript error, specifying a message and a name
foreign import errorWithName :: String -> String -> Error

-- | Get the error message from a JavaScript error
foreign import message :: Error -> String

Expand Down
15 changes: 14 additions & 1 deletion test/regression/exception.lua
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
-- Regression guard for the Lua 5.1 FFI of Effect.Exception.
--
-- Pins the four semantic fixes:
-- Pins the semantic fixes:
-- #81 catchException must RUN the handler Effect (trailing `()`), not return
-- the unexecuted thunk.
-- #82 throwException must preserve the message across throw/catch; Lua's
Expand All@@ -11,6 +11,10 @@
-- #84 errorWithCause keeps the message pristine (the cause is unobservable
-- through this binding — there is no `cause` accessor — so it is dropped
-- rather than concatenated into the message).
-- #269 errorWithName was missing from the fork. Upstream takes the message
-- first and the name second (errorWithName(msg)(name)); the name is
-- dropped like errorWithCause's cause, so `name` keeps answering the
-- documented constant "Error".
--
-- `Effect a` in this backend is a zero-arg thunk that must be invoked to run.
-- Run from the repo root: `lua test/regression/exception.lua`.
Expand DownExpand Up@@ -87,6 +91,15 @@ end
-- Sanity: message of a plain error is the message.
do check("message of a plain error", E.message(E.error("boom")) == "boom", "got " .. tostring(E.message(E.error("boom")))) end

--------------------------------------------------------------------------------
-- #269 errorWithName keeps the message; the name stays unobservable ----------

do
local e = E.errorWithName("boom")("TypeError")
check("errorWithName message is the supplied msg", E.message(e) == "boom", "got " .. tostring(E.message(e)))
check("errorWithName name stays \"Error\"", E.name(e) == "Error", "got " .. tostring(E.name(e)))
end

--------------------------------------------------------------------------------

if failures > 0 then error(failures .. " regression check(s) failed") end
Expand Down