Software /
code /
prosody
Annotate
util/error.lua @ 11221:b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 09 Dec 2020 13:55:10 +0000 |
parent | 11207:4e060ae8520b |
child | 11222:4b39691a274e |
rev | line source |
---|---|
11075
d8fad2b48b05
util.error: Add unique 'instance_id' to error objects
Matthew Wild <mwild1@gmail.com>
parents:
11054
diff
changeset
|
1 local id = require "util.id"; |
11051
08539aa129ee
util.error: Add configuration for including traceback in tostring()
Matthew Wild <mwild1@gmail.com>
parents:
11050
diff
changeset
|
2 |
08539aa129ee
util.error: Add configuration for including traceback in tostring()
Matthew Wild <mwild1@gmail.com>
parents:
11050
diff
changeset
|
3 -- Library configuration (see configure()) |
08539aa129ee
util.error: Add configuration for including traceback in tostring()
Matthew Wild <mwild1@gmail.com>
parents:
11050
diff
changeset
|
4 local auto_inject_traceback = false; |
08539aa129ee
util.error: Add configuration for including traceback in tostring()
Matthew Wild <mwild1@gmail.com>
parents:
11050
diff
changeset
|
5 local display_tracebacks = false; |
08539aa129ee
util.error: Add configuration for including traceback in tostring()
Matthew Wild <mwild1@gmail.com>
parents:
11050
diff
changeset
|
6 |
08539aa129ee
util.error: Add configuration for including traceback in tostring()
Matthew Wild <mwild1@gmail.com>
parents:
11050
diff
changeset
|
7 |
9746
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() |
11051
08539aa129ee
util.error: Add configuration for including traceback in tostring()
Matthew Wild <mwild1@gmail.com>
parents:
11050
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:
11050
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:
11050
diff
changeset
|
13 end |
10069
6f317e51544d
util.error: Fix traceback due to missing text field
Kim Alvefur <zash@zash.se>
parents:
9749
diff
changeset
|
14 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
|
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 |
11050
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10501
diff
changeset
|
21 local function configure(opt) |
11051
08539aa129ee
util.error: Add configuration for including traceback in tostring()
Matthew Wild <mwild1@gmail.com>
parents:
11050
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:
11050
diff
changeset
|
23 display_tracebacks = opt.display_tracebacks; |
08539aa129ee
util.error: Add configuration for including traceback in tostring()
Matthew Wild <mwild1@gmail.com>
parents:
11050
diff
changeset
|
24 end |
11050
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10501
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:
10501
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:
10501
diff
changeset
|
27 end |
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10501
diff
changeset
|
28 end |
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10501
diff
changeset
|
29 |
10493
d9132e7412b8
util.error: Write down some thoughts in comments
Kim Alvefur <zash@zash.se>
parents:
10365
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:
10365
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:
10365
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:
10365
diff
changeset
|
33 -- Translations? |
d9132e7412b8
util.error: Write down some thoughts in comments
Kim Alvefur <zash@zash.se>
parents:
10365
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:
10365
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:
10365
diff
changeset
|
36 |
11053
04ad9555c799
util.error: Add a 'source' parameter where origin module can be mentioned
Kim Alvefur <zash@zash.se>
parents:
11051
diff
changeset
|
37 local function new(e, context, registry, source) |
11161
f51ed2652602
util.error: Pass through existing error objects passed to new()
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
38 if is_err(e) then return e; end |
11080
ba77c142c9b1
util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents:
11079
diff
changeset
|
39 local template = registry and registry[e]; |
ba77c142c9b1
util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents:
11079
diff
changeset
|
40 if not template then |
ba77c142c9b1
util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents:
11079
diff
changeset
|
41 if type(e) == "table" then |
ba77c142c9b1
util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents:
11079
diff
changeset
|
42 template = { |
ba77c142c9b1
util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents:
11079
diff
changeset
|
43 code = e.code; |
ba77c142c9b1
util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents:
11079
diff
changeset
|
44 type = e.type; |
ba77c142c9b1
util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents:
11079
diff
changeset
|
45 condition = e.condition; |
ba77c142c9b1
util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents:
11079
diff
changeset
|
46 text = e.text; |
11081
0b68697450c5
util.error: Add well-known field 'extra'
Kim Alvefur <zash@zash.se>
parents:
11080
diff
changeset
|
47 extra = e.extra; |
11080
ba77c142c9b1
util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents:
11079
diff
changeset
|
48 }; |
ba77c142c9b1
util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents:
11079
diff
changeset
|
49 else |
ba77c142c9b1
util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents:
11079
diff
changeset
|
50 template = {}; |
ba77c142c9b1
util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents:
11079
diff
changeset
|
51 end |
ba77c142c9b1
util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents:
11079
diff
changeset
|
52 end |
11076
505c3e5907a5
util.error: Simplify error creation - remove ability to set context from templates, and remove default context
Matthew Wild <mwild1@gmail.com>
parents:
11075
diff
changeset
|
53 context = context or {}; |
11050
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10501
diff
changeset
|
54 |
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10501
diff
changeset
|
55 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
|
56 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
|
57 end |
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10501
diff
changeset
|
58 |
11077
8ea430de5fd3
util.error: Minor tweaks to error creation code to prepare for future changes
Matthew Wild <mwild1@gmail.com>
parents:
11076
diff
changeset
|
59 local error_instance = setmetatable({ |
11075
d8fad2b48b05
util.error: Add unique 'instance_id' to error objects
Matthew Wild <mwild1@gmail.com>
parents:
11054
diff
changeset
|
60 instance_id = id.short(); |
11077
8ea430de5fd3
util.error: Minor tweaks to error creation code to prepare for future changes
Matthew Wild <mwild1@gmail.com>
parents:
11076
diff
changeset
|
61 |
11101
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
62 type = template.type or "cancel"; |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
63 condition = template.condition or "undefined-condition"; |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
64 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
|
65 code = template.code; |
11101
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
66 extra = template.extra; |
9746
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 |
11077
8ea430de5fd3
util.error: Minor tweaks to error creation code to prepare for future changes
Matthew Wild <mwild1@gmail.com>
parents:
11076
diff
changeset
|
68 context = context; |
11053
04ad9555c799
util.error: Add a 'source' parameter where origin module can be mentioned
Kim Alvefur <zash@zash.se>
parents:
11051
diff
changeset
|
69 source = source; |
9746
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 }, error_mt); |
11077
8ea430de5fd3
util.error: Minor tweaks to error creation code to prepare for future changes
Matthew Wild <mwild1@gmail.com>
parents:
11076
diff
changeset
|
71 |
8ea430de5fd3
util.error: Minor tweaks to error creation code to prepare for future changes
Matthew Wild <mwild1@gmail.com>
parents:
11076
diff
changeset
|
72 return error_instance; |
9746
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 end |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 |
11101
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
75 -- compact --> normal form |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
76 local function expand_registry(namespace, registry) |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
77 local mapped = {} |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
78 for err,template in pairs(registry) do |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
79 local e = { |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
80 type = template[1]; |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
81 condition = template[2]; |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
82 text = template[3]; |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
83 }; |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
84 if namespace and template[4] then |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
85 e.extra = { namespace = namespace, condition = template[4] }; |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
86 end |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
87 mapped[err] = e; |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
88 end |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
89 return mapped; |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
90 end |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
91 |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
92 local function init(source, namespace, registry) |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
93 if type(namespace) == "table" then |
11207
4e060ae8520b
util.error: Remove a stray word from a comment
Kim Alvefur <zash@zash.se>
parents:
11161
diff
changeset
|
94 -- registry can be given as second argument if namespace is not used |
11101
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
95 registry, namespace = namespace, nil; |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
96 end |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
97 local _, protoerr = next(registry, nil); |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
98 if protoerr and type(next(protoerr)) == "number" then |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
99 registry = expand_registry(namespace, registry); |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
100 end |
11221
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11207
diff
changeset
|
101 |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11207
diff
changeset
|
102 local function wrap(e, context) |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11207
diff
changeset
|
103 if is_err(e) then |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11207
diff
changeset
|
104 return e; |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11207
diff
changeset
|
105 end |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11207
diff
changeset
|
106 local err = new(registry[e] or { |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11207
diff
changeset
|
107 type = "cancel", condition = "undefined-condition" |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11207
diff
changeset
|
108 }, context, registry, source); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11207
diff
changeset
|
109 err.context.wrapped_error = e; |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11207
diff
changeset
|
110 return err; |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11207
diff
changeset
|
111 end |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11207
diff
changeset
|
112 |
11078
fb3aec3dbe21
util.error: Have init() return an object to allow API extensibility via additional methods
Matthew Wild <mwild1@gmail.com>
parents:
11077
diff
changeset
|
113 return { |
11098
5b778ec095f0
util.error: Expose source and registry as fields on the registry object
Kim Alvefur <zash@zash.se>
parents:
11095
diff
changeset
|
114 source = source; |
5b778ec095f0
util.error: Expose source and registry as fields on the registry object
Kim Alvefur <zash@zash.se>
parents:
11095
diff
changeset
|
115 registry = registry; |
11078
fb3aec3dbe21
util.error: Have init() return an object to allow API extensibility via additional methods
Matthew Wild <mwild1@gmail.com>
parents:
11077
diff
changeset
|
116 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:
11077
diff
changeset
|
117 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:
11077
diff
changeset
|
118 end; |
11221
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11207
diff
changeset
|
119 coerce = function (ok, err, ...) |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11207
diff
changeset
|
120 if ok then |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11207
diff
changeset
|
121 return ok, err, ...; |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11207
diff
changeset
|
122 end |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11207
diff
changeset
|
123 return nil, wrap(err); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11207
diff
changeset
|
124 end; |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11207
diff
changeset
|
125 wrap = wrap; |
11078
fb3aec3dbe21
util.error: Have init() return an object to allow API extensibility via additional methods
Matthew Wild <mwild1@gmail.com>
parents:
11077
diff
changeset
|
126 }; |
11054
ad07152d7bde
util.error: Add a wrapper for common parameters
Kim Alvefur <zash@zash.se>
parents:
11053
diff
changeset
|
127 end |
ad07152d7bde
util.error: Add a wrapper for common parameters
Kim Alvefur <zash@zash.se>
parents:
11053
diff
changeset
|
128 |
9746
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 local function coerce(ok, err, ...) |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
130 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
|
131 return ok, err, ...; |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
132 end |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
133 |
11079
1e5a0e0469de
util.error: Switch coerce() to use new() and change 'native' to context field 'wrapped_error'
Matthew Wild <mwild1@gmail.com>
parents:
11078
diff
changeset
|
134 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:
11078
diff
changeset
|
135 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:
11078
diff
changeset
|
136 }, { wrapped_error = err }); |
9746
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
138 return ok, new_err, ...; |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
139 end |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
140 |
11094
03fdf41fd948
util.error: Pass converted stanza errors throguh new()
Kim Alvefur <zash@zash.se>
parents:
11092
diff
changeset
|
141 local function from_stanza(stanza, context, source) |
11092
bd13aa89262d
util.error: Collect Application-Specific Conditions from stanza errors
Kim Alvefur <zash@zash.se>
parents:
11091
diff
changeset
|
142 local error_type, condition, text, extra_tag = stanza:get_error(); |
11089
35d2260644d9
util.error: Extract error originator from stanza errors
Kim Alvefur <zash@zash.se>
parents:
11081
diff
changeset
|
143 local error_tag = stanza:get_child("error"); |
35d2260644d9
util.error: Extract error originator from stanza errors
Kim Alvefur <zash@zash.se>
parents:
11081
diff
changeset
|
144 context = context or {}; |
35d2260644d9
util.error: Extract error originator from stanza errors
Kim Alvefur <zash@zash.se>
parents:
11081
diff
changeset
|
145 context.stanza = stanza; |
11090
33b6fbdcec88
util.error: Default error originator to stanza sender
Kim Alvefur <zash@zash.se>
parents:
11089
diff
changeset
|
146 context.by = error_tag.attr.by or stanza.attr.from; |
33b6fbdcec88
util.error: Default error originator to stanza sender
Kim Alvefur <zash@zash.se>
parents:
11089
diff
changeset
|
147 |
11095
1ea3574b19c8
util.error: Turns out <gone> wasn't alone, there's also <redirect>
Kim Alvefur <zash@zash.se>
parents:
11094
diff
changeset
|
148 local uri; |
1ea3574b19c8
util.error: Turns out <gone> wasn't alone, there's also <redirect>
Kim Alvefur <zash@zash.se>
parents:
11094
diff
changeset
|
149 if condition == "gone" or condition == "redirect" then |
1ea3574b19c8
util.error: Turns out <gone> wasn't alone, there's also <redirect>
Kim Alvefur <zash@zash.se>
parents:
11094
diff
changeset
|
150 uri = error_tag:get_child_text(condition, "urn:ietf:params:xml:ns:xmpp-stanzas"); |
1ea3574b19c8
util.error: Turns out <gone> wasn't alone, there's also <redirect>
Kim Alvefur <zash@zash.se>
parents:
11094
diff
changeset
|
151 end |
1ea3574b19c8
util.error: Turns out <gone> wasn't alone, there's also <redirect>
Kim Alvefur <zash@zash.se>
parents:
11094
diff
changeset
|
152 |
11094
03fdf41fd948
util.error: Pass converted stanza errors throguh new()
Kim Alvefur <zash@zash.se>
parents:
11092
diff
changeset
|
153 return new({ |
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
|
154 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
|
155 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
|
156 text = text; |
11095
1ea3574b19c8
util.error: Turns out <gone> wasn't alone, there's also <redirect>
Kim Alvefur <zash@zash.se>
parents:
11094
diff
changeset
|
157 extra = (extra_tag or uri) and { |
1ea3574b19c8
util.error: Turns out <gone> wasn't alone, there's also <redirect>
Kim Alvefur <zash@zash.se>
parents:
11094
diff
changeset
|
158 uri = uri; |
11092
bd13aa89262d
util.error: Collect Application-Specific Conditions from stanza errors
Kim Alvefur <zash@zash.se>
parents:
11091
diff
changeset
|
159 tag = extra_tag; |
11091
4b4b5188492f
util.error: Add special case handling of <gone> with an URI
Kim Alvefur <zash@zash.se>
parents:
11090
diff
changeset
|
160 } or nil; |
11094
03fdf41fd948
util.error: Pass converted stanza errors throguh new()
Kim Alvefur <zash@zash.se>
parents:
11092
diff
changeset
|
161 }, context, nil, source); |
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
|
162 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
|
163 |
9746
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
164 return { |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
165 new = new; |
11054
ad07152d7bde
util.error: Add a wrapper for common parameters
Kim Alvefur <zash@zash.se>
parents:
11053
diff
changeset
|
166 init = init; |
9746
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
167 coerce = coerce; |
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
168 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
|
169 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
|
170 configure = configure; |
9746
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
171 } |