Software /
code /
prosody
Annotate
util/error.lua @ 11050:51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
This allows extra debug info to be provided for development purposes.
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 28 Aug 2020 12:40:59 +0100 |
parent | 10501:e8186aba1583 |
child | 11051:08539aa129ee |
rev | line source |
---|---|
9746
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 local error_mt = { __name = "error" }; |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 function error_mt:__tostring() |
10069
6f317e51544d
util.error: Fix traceback due to missing text field
Kim Alvefur <zash@zash.se>
parents:
9749
diff
changeset
|
4 return ("error<%s:%s:%s>"):format(self.type, self.condition, self.text or ""); |
9746
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 end |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 local function is_err(e) |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 return getmetatable(e) == error_mt; |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 end |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 |
11050
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10501
diff
changeset
|
11 local auto_inject_traceback = false; |
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10501
diff
changeset
|
12 |
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10501
diff
changeset
|
13 local function configure(opt) |
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10501
diff
changeset
|
14 if opt.auto_inject_traceback ~= nil then |
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10501
diff
changeset
|
15 auto_inject_traceback = opt.auto_inject_traceback; |
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10501
diff
changeset
|
16 end |
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10501
diff
changeset
|
17 end |
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10501
diff
changeset
|
18 |
10493
d9132e7412b8
util.error: Write down some thoughts in comments
Kim Alvefur <zash@zash.se>
parents:
10365
diff
changeset
|
19 -- Do we want any more well-known fields? |
d9132e7412b8
util.error: Write down some thoughts in comments
Kim Alvefur <zash@zash.se>
parents:
10365
diff
changeset
|
20 -- Or could we just copy all fields from `e`? |
d9132e7412b8
util.error: Write down some thoughts in comments
Kim Alvefur <zash@zash.se>
parents:
10365
diff
changeset
|
21 -- 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:
10365
diff
changeset
|
22 -- Translations? |
d9132e7412b8
util.error: Write down some thoughts in comments
Kim Alvefur <zash@zash.se>
parents:
10365
diff
changeset
|
23 -- 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:
10365
diff
changeset
|
24 -- 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:
10365
diff
changeset
|
25 |
9746
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 local function new(e, context, registry) |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 local template = (registry and registry[e]) or e or {}; |
11050
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10501
diff
changeset
|
28 context = context or template.context or { _error_id = e }; |
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10501
diff
changeset
|
29 |
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10501
diff
changeset
|
30 if auto_inject_traceback then |
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10501
diff
changeset
|
31 context.traceback = debug.traceback("error stack", 2); |
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10501
diff
changeset
|
32 end |
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10501
diff
changeset
|
33 |
9746
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 return setmetatable({ |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 type = template.type or "cancel"; |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 condition = template.condition or "undefined-condition"; |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 text = template.text; |
10501
e8186aba1583
util.error: Move default for numeric error code to net.http.server
Kim Alvefur <zash@zash.se>
parents:
10493
diff
changeset
|
38 code = template.code; |
9746
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 context = context or template.context or { _error_id = e }; |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 }, error_mt); |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 end |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 local function coerce(ok, err, ...) |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 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
|
46 return ok, err, ...; |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 end |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 local new_err = setmetatable({ |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 native = err; |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 type = "cancel"; |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 condition = "undefined-condition"; |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 }, error_mt); |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 return ok, new_err, ...; |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 end |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 |
9749
9361bd1b9c9b
util.error: Add a function for creating an error object from an error stanza
Kim Alvefur <zash@zash.se>
parents:
9746
diff
changeset
|
58 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:
9746
diff
changeset
|
59 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:
9746
diff
changeset
|
60 return setmetatable({ |
9361bd1b9c9b
util.error: Add a function for creating an error object from an error stanza
Kim Alvefur <zash@zash.se>
parents:
9746
diff
changeset
|
61 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:
9746
diff
changeset
|
62 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:
9746
diff
changeset
|
63 text = text; |
9361bd1b9c9b
util.error: Add a function for creating an error object from an error stanza
Kim Alvefur <zash@zash.se>
parents:
9746
diff
changeset
|
64 |
9361bd1b9c9b
util.error: Add a function for creating an error object from an error stanza
Kim Alvefur <zash@zash.se>
parents:
9746
diff
changeset
|
65 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:
9746
diff
changeset
|
66 }, error_mt); |
9361bd1b9c9b
util.error: Add a function for creating an error object from an error stanza
Kim Alvefur <zash@zash.se>
parents:
9746
diff
changeset
|
67 end |
9361bd1b9c9b
util.error: Add a function for creating an error object from an error stanza
Kim Alvefur <zash@zash.se>
parents:
9746
diff
changeset
|
68 |
9746
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 return { |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 new = new; |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 coerce = coerce; |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 is_err = is_err; |
9749
9361bd1b9c9b
util.error: Add a function for creating an error object from an error stanza
Kim Alvefur <zash@zash.se>
parents:
9746
diff
changeset
|
73 from_stanza = from_stanza; |
11050
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10501
diff
changeset
|
74 configure = configure; |
9746
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 } |