forked from Liquipedia/Lua-Modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.lua
More file actions
69 lines (54 loc) · 1.94 KB
/
Copy patherror.lua
File metadata and controls
69 lines (54 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
---
-- @Liquipedia
-- wiki=commons
-- page=Module:Error
--
-- Please see https://github.com/Liquipedia/Lua-Modules to contribute
--
local Class = require('Module:Class')
--[[
A minimal error class, whose purpose is to allow additional fields to be
attached to an error message string. It has only one method, the __tostring
metamethod, and one required field, error.message.
The class is intended to be open, meaning that its fields are all public and
can be used for any purpose. By convention, the following fields have specific
meanings:
error.message: A short error message describing the error. (Required)
error.stacks: The stack traces of the error. The first stack trace is the one
last thrown. Subsequent stack traces are from when the error was rethrown by an
error handler handling the same error.
error.originalErrors: When an error handler throws (not a rethrow), the original
error that it was handling is tracked here.
error.childErrors: A composite error is one that aggregates many errors into a
summary. Composite errors will place child errors here.
error.innerError: Used when an error instance wraps something that's not a
message string.
error.header: Generic error handlers like Logic.tryOrElseLog will place a
preamble-like text here to give some context to the error.
error.noStack: Disables the stack trace
]]
local Error = Class.new(function(self, any)
-- Normalize the various ways an error can be thrown
if type(any) == 'string' then
self.message = any
elseif type(any) == 'table' then
local props = any
for key, value in pairs(props) do
self[key] = value
end
elseif any ~= nil then
self.message = tostring(any)
self.innerError = any
end
self.message = self.message or 'Unknown error'
end)
function Error.isError(error)
return type(error) == 'table'
and type(error.is_a) == 'function'
and error:is_a(Error)
and type(error.message) == 'string'
end
function Error:__tostring()
return self.message
end
return Error