lunarmodules / luassert

Assertion library for Lua
MIT License
202 stars 77 forks source link

Convert error object to string in output_spec #68

Closed o-lim closed 9 years ago

o-lim commented 9 years ago

Update output_spec to convert an error object to string in case it is not already a string. In such a case, the object should be a table with the __tostring metamethod defined, making it convertible to a string.

The assert function does explicitly throw a string, however, the caller can always change the environment such that error will instead throw an error object.

For example:

local olderror = error
error = function(err)
  local mt = { __index = {}, __tostring = function(e) return e.message end }
  olderror(setmetatable({ message = err }, mt))
end