author | Kim Alvefur <zash@zash.se> |
Sat, 26 Sep 2020 17:16:34 +0200 | |
changeset 11085 | 0b68697450c5 |
parent 11084 | ba77c142c9b1 |
child 11093 | 35d2260644d9 |
permissions | -rw-r--r-- |
11079
d8fad2b48b05
util.error: Add unique 'instance_id' to error objects
Matthew Wild <mwild1@gmail.com>
parents:
11058
diff
changeset
|
1 |
local id = require "util.id"; |
11055
08539aa129ee
util.error: Add configuration for including traceback in tostring()
Matthew Wild <mwild1@gmail.com>
parents:
11054
diff
changeset
|
2 |
|
08539aa129ee
util.error: Add configuration for including traceback in tostring()
Matthew Wild <mwild1@gmail.com>
parents:
11054
diff
changeset
|
3 |
-- Library configuration (see configure()) |
08539aa129ee
util.error: Add configuration for including traceback in tostring()
Matthew Wild <mwild1@gmail.com>
parents:
11054
diff
changeset
|
4 |
local auto_inject_traceback = false; |
08539aa129ee
util.error: Add configuration for including traceback in tostring()
Matthew Wild <mwild1@gmail.com>
parents:
11054
diff
changeset
|
5 |
local display_tracebacks = false; |
08539aa129ee
util.error: Add configuration for including traceback in tostring()
Matthew Wild <mwild1@gmail.com>
parents:
11054
diff
changeset
|
6 |
|
08539aa129ee
util.error: Add configuration for including traceback in tostring()
Matthew Wild <mwild1@gmail.com>
parents:
11054
diff
changeset
|
7 |
|
9750
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 |
local error_mt = { __name = "error" }; |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 |
|
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 |
function error_mt:__tostring() |
11055
08539aa129ee
util.error: Add configuration for including traceback in tostring()
Matthew Wild <mwild1@gmail.com>
parents:
11054
diff
changeset
|
11 |
if display_tracebacks and self.context.traceback then |
08539aa129ee
util.error: Add configuration for including traceback in tostring()
Matthew Wild <mwild1@gmail.com>
parents:
11054
diff
changeset
|
12 |
return ("error<%s:%s:%s:%s>"):format(self.type, self.condition, self.text or "", self.context.traceback); |
08539aa129ee
util.error: Add configuration for including traceback in tostring()
Matthew Wild <mwild1@gmail.com>
parents:
11054
diff
changeset
|
13 |
end |
10073
6f317e51544d
util.error: Fix traceback due to missing text field
Kim Alvefur <zash@zash.se>
parents:
9753
diff
changeset
|
14 |
return ("error<%s:%s:%s>"):format(self.type, self.condition, self.text or ""); |
9750
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 |
end |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 |
|
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 |
local function is_err(e) |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 |
return getmetatable(e) == error_mt; |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 |
end |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 |
|
11054
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10505
diff
changeset
|
21 |
local function configure(opt) |
11055
08539aa129ee
util.error: Add configuration for including traceback in tostring()
Matthew Wild <mwild1@gmail.com>
parents:
11054
diff
changeset
|
22 |
if opt.display_tracebacks ~= nil then |
08539aa129ee
util.error: Add configuration for including traceback in tostring()
Matthew Wild <mwild1@gmail.com>
parents:
11054
diff
changeset
|
23 |
display_tracebacks = opt.display_tracebacks; |
08539aa129ee
util.error: Add configuration for including traceback in tostring()
Matthew Wild <mwild1@gmail.com>
parents:
11054
diff
changeset
|
24 |
end |
11054
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10505
diff
changeset
|
25 |
if opt.auto_inject_traceback ~= nil then |
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10505
diff
changeset
|
26 |
auto_inject_traceback = opt.auto_inject_traceback; |
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10505
diff
changeset
|
27 |
end |
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10505
diff
changeset
|
28 |
end |
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10505
diff
changeset
|
29 |
|
10497
d9132e7412b8
util.error: Write down some thoughts in comments
Kim Alvefur <zash@zash.se>
parents:
10369
diff
changeset
|
30 |
-- Do we want any more well-known fields? |
d9132e7412b8
util.error: Write down some thoughts in comments
Kim Alvefur <zash@zash.se>
parents:
10369
diff
changeset
|
31 |
-- Or could we just copy all fields from `e`? |
d9132e7412b8
util.error: Write down some thoughts in comments
Kim Alvefur <zash@zash.se>
parents:
10369
diff
changeset
|
32 |
-- Sometimes you want variable details in the `text`, how to handle that? |
d9132e7412b8
util.error: Write down some thoughts in comments
Kim Alvefur <zash@zash.se>
parents:
10369
diff
changeset
|
33 |
-- Translations? |
d9132e7412b8
util.error: Write down some thoughts in comments
Kim Alvefur <zash@zash.se>
parents:
10369
diff
changeset
|
34 |
-- Should the `type` be restricted to the stanza error types or free-form? |
d9132e7412b8
util.error: Write down some thoughts in comments
Kim Alvefur <zash@zash.se>
parents:
10369
diff
changeset
|
35 |
-- What to set `type` to for stream errors or SASL errors? Those don't have a 'type' attr. |
d9132e7412b8
util.error: Write down some thoughts in comments
Kim Alvefur <zash@zash.se>
parents:
10369
diff
changeset
|
36 |
|
11057
04ad9555c799
util.error: Add a 'source' parameter where origin module can be mentioned
Kim Alvefur <zash@zash.se>
parents:
11055
diff
changeset
|
37 |
local function new(e, context, registry, source) |
11084
ba77c142c9b1
util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents:
11083
diff
changeset
|
38 |
local template = registry and registry[e]; |
ba77c142c9b1
util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents:
11083
diff
changeset
|
39 |
if not template then |
ba77c142c9b1
util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents:
11083
diff
changeset
|
40 |
if type(e) == "table" then |
ba77c142c9b1
util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents:
11083
diff
changeset
|
41 |
template = { |
ba77c142c9b1
util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents:
11083
diff
changeset
|
42 |
code = e.code; |
ba77c142c9b1
util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents:
11083
diff
changeset
|
43 |
type = e.type; |
ba77c142c9b1
util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents:
11083
diff
changeset
|
44 |
condition = e.condition; |
ba77c142c9b1
util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents:
11083
diff
changeset
|
45 |
text = e.text; |
11085
0b68697450c5
util.error: Add well-known field 'extra'
Kim Alvefur <zash@zash.se>
parents:
11084
diff
changeset
|
46 |
extra = e.extra; |
11084
ba77c142c9b1
util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents:
11083
diff
changeset
|
47 |
}; |
ba77c142c9b1
util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents:
11083
diff
changeset
|
48 |
else |
ba77c142c9b1
util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents:
11083
diff
changeset
|
49 |
template = {}; |
ba77c142c9b1
util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents:
11083
diff
changeset
|
50 |
end |
ba77c142c9b1
util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents:
11083
diff
changeset
|
51 |
end |
11080
505c3e5907a5
util.error: Simplify error creation - remove ability to set context from templates, and remove default context
Matthew Wild <mwild1@gmail.com>
parents:
11079
diff
changeset
|
52 |
context = context or {}; |
11054
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10505
diff
changeset
|
53 |
|
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10505
diff
changeset
|
54 |
if auto_inject_traceback then |
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10505
diff
changeset
|
55 |
context.traceback = debug.traceback("error stack", 2); |
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10505
diff
changeset
|
56 |
end |
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10505
diff
changeset
|
57 |
|
11081
8ea430de5fd3
util.error: Minor tweaks to error creation code to prepare for future changes
Matthew Wild <mwild1@gmail.com>
parents:
11080
diff
changeset
|
58 |
local error_instance = setmetatable({ |
11079
d8fad2b48b05
util.error: Add unique 'instance_id' to error objects
Matthew Wild <mwild1@gmail.com>
parents:
11058
diff
changeset
|
59 |
instance_id = id.short(); |
11081
8ea430de5fd3
util.error: Minor tweaks to error creation code to prepare for future changes
Matthew Wild <mwild1@gmail.com>
parents:
11080
diff
changeset
|
60 |
|
9750
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 |
type = template.type or "cancel"; |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 |
condition = template.condition or "undefined-condition"; |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 |
text = template.text; |
10505
e8186aba1583
util.error: Move default for numeric error code to net.http.server
Kim Alvefur <zash@zash.se>
parents:
10497
diff
changeset
|
64 |
code = template.code; |
11085
0b68697450c5
util.error: Add well-known field 'extra'
Kim Alvefur <zash@zash.se>
parents:
11084
diff
changeset
|
65 |
extra = template.extra; |
9750
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 |
|
11081
8ea430de5fd3
util.error: Minor tweaks to error creation code to prepare for future changes
Matthew Wild <mwild1@gmail.com>
parents:
11080
diff
changeset
|
67 |
context = context; |
11057
04ad9555c799
util.error: Add a 'source' parameter where origin module can be mentioned
Kim Alvefur <zash@zash.se>
parents:
11055
diff
changeset
|
68 |
source = source; |
9750
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 |
}, error_mt); |
11081
8ea430de5fd3
util.error: Minor tweaks to error creation code to prepare for future changes
Matthew Wild <mwild1@gmail.com>
parents:
11080
diff
changeset
|
70 |
|
8ea430de5fd3
util.error: Minor tweaks to error creation code to prepare for future changes
Matthew Wild <mwild1@gmail.com>
parents:
11080
diff
changeset
|
71 |
return error_instance; |
9750
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 |
end |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 |
|
11058
ad07152d7bde
util.error: Add a wrapper for common parameters
Kim Alvefur <zash@zash.se>
parents:
11057
diff
changeset
|
74 |
local function init(source, registry) |
11082
fb3aec3dbe21
util.error: Have init() return an object to allow API extensibility via additional methods
Matthew Wild <mwild1@gmail.com>
parents:
11081
diff
changeset
|
75 |
return { |
fb3aec3dbe21
util.error: Have init() return an object to allow API extensibility via additional methods
Matthew Wild <mwild1@gmail.com>
parents:
11081
diff
changeset
|
76 |
new = function (e, context) |
fb3aec3dbe21
util.error: Have init() return an object to allow API extensibility via additional methods
Matthew Wild <mwild1@gmail.com>
parents:
11081
diff
changeset
|
77 |
return new(e, context, registry, source); |
fb3aec3dbe21
util.error: Have init() return an object to allow API extensibility via additional methods
Matthew Wild <mwild1@gmail.com>
parents:
11081
diff
changeset
|
78 |
end; |
fb3aec3dbe21
util.error: Have init() return an object to allow API extensibility via additional methods
Matthew Wild <mwild1@gmail.com>
parents:
11081
diff
changeset
|
79 |
}; |
11058
ad07152d7bde
util.error: Add a wrapper for common parameters
Kim Alvefur <zash@zash.se>
parents:
11057
diff
changeset
|
80 |
end |
ad07152d7bde
util.error: Add a wrapper for common parameters
Kim Alvefur <zash@zash.se>
parents:
11057
diff
changeset
|
81 |
|
9750
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 |
local function coerce(ok, err, ...) |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 |
if ok or is_err(err) then |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 |
return ok, err, ...; |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 |
end |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 |
|
11083
1e5a0e0469de
util.error: Switch coerce() to use new() and change 'native' to context field 'wrapped_error'
Matthew Wild <mwild1@gmail.com>
parents:
11082
diff
changeset
|
87 |
local new_err = new({ |
1e5a0e0469de
util.error: Switch coerce() to use new() and change 'native' to context field 'wrapped_error'
Matthew Wild <mwild1@gmail.com>
parents:
11082
diff
changeset
|
88 |
type = "cancel", condition = "undefined-condition" |
1e5a0e0469de
util.error: Switch coerce() to use new() and change 'native' to context field 'wrapped_error'
Matthew Wild <mwild1@gmail.com>
parents:
11082
diff
changeset
|
89 |
}, { wrapped_error = err }); |
9750
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 |
|
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 |
return ok, new_err, ...; |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 |
end |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 |
|
9753
9361bd1b9c9b
util.error: Add a function for creating an error object from an error stanza
Kim Alvefur <zash@zash.se>
parents:
9750
diff
changeset
|
94 |
local function from_stanza(stanza, context) |
9361bd1b9c9b
util.error: Add a function for creating an error object from an error stanza
Kim Alvefur <zash@zash.se>
parents:
9750
diff
changeset
|
95 |
local error_type, condition, text = stanza:get_error(); |
9361bd1b9c9b
util.error: Add a function for creating an error object from an error stanza
Kim Alvefur <zash@zash.se>
parents:
9750
diff
changeset
|
96 |
return setmetatable({ |
9361bd1b9c9b
util.error: Add a function for creating an error object from an error stanza
Kim Alvefur <zash@zash.se>
parents:
9750
diff
changeset
|
97 |
type = error_type or "cancel"; |
9361bd1b9c9b
util.error: Add a function for creating an error object from an error stanza
Kim Alvefur <zash@zash.se>
parents:
9750
diff
changeset
|
98 |
condition = condition or "undefined-condition"; |
9361bd1b9c9b
util.error: Add a function for creating an error object from an error stanza
Kim Alvefur <zash@zash.se>
parents:
9750
diff
changeset
|
99 |
text = text; |
9361bd1b9c9b
util.error: Add a function for creating an error object from an error stanza
Kim Alvefur <zash@zash.se>
parents:
9750
diff
changeset
|
100 |
|
9361bd1b9c9b
util.error: Add a function for creating an error object from an error stanza
Kim Alvefur <zash@zash.se>
parents:
9750
diff
changeset
|
101 |
context = context or { stanza = stanza }; |
9361bd1b9c9b
util.error: Add a function for creating an error object from an error stanza
Kim Alvefur <zash@zash.se>
parents:
9750
diff
changeset
|
102 |
}, error_mt); |
9361bd1b9c9b
util.error: Add a function for creating an error object from an error stanza
Kim Alvefur <zash@zash.se>
parents:
9750
diff
changeset
|
103 |
end |
9361bd1b9c9b
util.error: Add a function for creating an error object from an error stanza
Kim Alvefur <zash@zash.se>
parents:
9750
diff
changeset
|
104 |
|
9750
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 |
return { |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 |
new = new; |
11058
ad07152d7bde
util.error: Add a wrapper for common parameters
Kim Alvefur <zash@zash.se>
parents:
11057
diff
changeset
|
107 |
init = init; |
9750
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
108 |
coerce = coerce; |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 |
is_err = is_err; |
9753
9361bd1b9c9b
util.error: Add a function for creating an error object from an error stanza
Kim Alvefur <zash@zash.se>
parents:
9750
diff
changeset
|
110 |
from_stanza = from_stanza; |
11054
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10505
diff
changeset
|
111 |
configure = configure; |
9750
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 |
} |