Software / code / prosody
Annotate
util/error.lua @ 13652:a08065207ef0
net.server_epoll: Call :shutdown() on TLS sockets when supported
Comment from Matthew:
This fixes a potential issue where the Prosody process gets blocked on sockets
waiting for them to close. Unlike non-TLS sockets, closing a TLS socket sends
layer 7 data, and this can cause problems for sockets which are in the process
of being cleaned up.
This depends on LuaSec changes which are not yet upstream.
From Martijn's original email:
So first my analysis of luasec. in ssl.c the socket is put into blocking
mode right before calling SSL_shutdown() inside meth_destroy(). My best
guess to why this is is because meth_destroy is linked to the __close
and __gc methods, which can't exactly be called multiple times and
luasec does want to make sure that a tls session is shutdown as clean
as possible.
I can't say I disagree with this reasoning and don't want to change this
behaviour. My solution to this without changing the current behaviour is
to introduce a shutdown() method. I am aware that this overlaps in a
conflicting way with tcp's shutdown method, but it stays close to the
OpenSSL name. This method calls SSL_shutdown() in the current
(non)blocking mode of the underlying socket and returns a boolean
whether or not the shutdown is completed (matching SSL_shutdown()'s 0
or 1 return values), and returns the familiar ssl_ioerror() strings on
error with a false for completion. This error can then be used to
determine if we have wantread/wantwrite to finalize things. Once
meth_shutdown() has been called once a shutdown flag will be set, which
indicates to meth_destroy() that the SSL_shutdown() has been handled
by the application and it shouldn't be needed to set the socket to
blocking mode. I've left the SSL_shutdown() call in the
LSEC_STATE_CONNECTED to prevent TOCTOU if the application reaches a
timeout for the shutdown code, which might allow SSL_shutdown() to
clean up anyway at the last possible moment.
Another thing I've changed to luasec is the call to socket_setblocking()
right before calling close(2) in socket_destroy() in usocket.c.
According to the latest POSIX[0]:
Note that the requirement for close() on a socket to block for up to
the current linger interval is not conditional on the O_NONBLOCK
setting.
Which I read to mean that removing O_NONBLOCK on the socket before close
doesn't impact the behaviour and only causes noise in system call
tracers. I didn't touch the windows bits of this, since I don't do
windows.
For the prosody side of things I've made the TLS shutdown bits resemble
interface:onwritable(), and put it under a combined guard of self._tls
and self.conn.shutdown. The self._tls bit is there to prevent getting
stuck on this condition, and self.conn.shutdown is there to prevent the
code being called by instances where the patched luasec isn't deployed.
The destroy() method can be called from various places and is read by
me as the "we give up" error path. To accommodate for these unexpected
entrypoints I've added a single call to self.conn:shutdown() to prevent
the socket being put into blocking mode. I have no expectations that
there is any other use here. Same as previous, the self.conn.shutdown
check is there to make sure it's not called on unpatched luasec
deployments and self._tls is there to make sure we don't call shutdown()
on tcp sockets.
I wouldn't recommend logging of the conn:shutdown() error inside
close(), since a lot of clients simply close the connection before
SSL_shutdown() is done.
| author | Martijn van Duren <martijn@openbsd.org> |
|---|---|
| date | Thu, 06 Feb 2025 15:04:38 +0000 |
| parent | 13079:e7a5e5a0dc02 |
| rev | line source |
|---|---|
|
12975
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
11224
diff
changeset
|
1 local id = require "prosody.util.id"; |
|
11051
08539aa129ee
util.error: Add configuration for including traceback in tostring()
Matthew Wild <mwild1@gmail.com>
parents:
11050
diff
changeset
|
2 |
|
11224
8143fd2f138b
util.error: Switch to util.debug traceback tables and remove display_tracebacks option
Matthew Wild <mwild1@gmail.com>
parents:
11223
diff
changeset
|
3 local util_debug; -- only imported on-demand |
|
8143fd2f138b
util.error: Switch to util.debug traceback tables and remove display_tracebacks option
Matthew Wild <mwild1@gmail.com>
parents:
11223
diff
changeset
|
4 |
|
11051
08539aa129ee
util.error: Add configuration for including traceback in tostring()
Matthew Wild <mwild1@gmail.com>
parents:
11050
diff
changeset
|
5 -- Library configuration (see configure()) |
|
08539aa129ee
util.error: Add configuration for including traceback in tostring()
Matthew Wild <mwild1@gmail.com>
parents:
11050
diff
changeset
|
6 local auto_inject_traceback = false; |
|
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() |
|
10069
6f317e51544d
util.error: Fix traceback due to missing text field
Kim Alvefur <zash@zash.se>
parents:
9749
diff
changeset
|
11 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
|
12 end |
|
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 |
|
11222
4b39691a274e
util.error: rename is_err() -> is_error()
Matthew Wild <mwild1@gmail.com>
parents:
11221
diff
changeset
|
14 local function is_error(e) |
|
9746
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 return getmetatable(e) == error_mt; |
|
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 end |
|
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 |
|
11050
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10501
diff
changeset
|
18 local function configure(opt) |
|
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10501
diff
changeset
|
19 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
|
20 auto_inject_traceback = opt.auto_inject_traceback; |
|
11224
8143fd2f138b
util.error: Switch to util.debug traceback tables and remove display_tracebacks option
Matthew Wild <mwild1@gmail.com>
parents:
11223
diff
changeset
|
21 if auto_inject_traceback then |
|
12975
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
11224
diff
changeset
|
22 util_debug = require "prosody.util.debug"; |
|
11224
8143fd2f138b
util.error: Switch to util.debug traceback tables and remove display_tracebacks option
Matthew Wild <mwild1@gmail.com>
parents:
11223
diff
changeset
|
23 end |
|
11050
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10501
diff
changeset
|
24 end |
|
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10501
diff
changeset
|
25 end |
|
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10501
diff
changeset
|
26 |
|
10493
d9132e7412b8
util.error: Write down some thoughts in comments
Kim Alvefur <zash@zash.se>
parents:
10365
diff
changeset
|
27 -- 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
|
28 -- 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
|
29 -- 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
|
30 -- Translations? |
|
d9132e7412b8
util.error: Write down some thoughts in comments
Kim Alvefur <zash@zash.se>
parents:
10365
diff
changeset
|
31 -- 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
|
32 -- 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
|
33 |
|
11053
04ad9555c799
util.error: Add a 'source' parameter where origin module can be mentioned
Kim Alvefur <zash@zash.se>
parents:
11051
diff
changeset
|
34 local function new(e, context, registry, source) |
|
11222
4b39691a274e
util.error: rename is_err() -> is_error()
Matthew Wild <mwild1@gmail.com>
parents:
11221
diff
changeset
|
35 if is_error(e) then return e; end |
|
11080
ba77c142c9b1
util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents:
11079
diff
changeset
|
36 local template = registry and registry[e]; |
|
ba77c142c9b1
util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents:
11079
diff
changeset
|
37 if not template then |
|
ba77c142c9b1
util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents:
11079
diff
changeset
|
38 if type(e) == "table" then |
|
ba77c142c9b1
util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents:
11079
diff
changeset
|
39 template = { |
|
ba77c142c9b1
util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents:
11079
diff
changeset
|
40 code = e.code; |
|
ba77c142c9b1
util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents:
11079
diff
changeset
|
41 type = e.type; |
|
ba77c142c9b1
util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents:
11079
diff
changeset
|
42 condition = e.condition; |
|
ba77c142c9b1
util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents:
11079
diff
changeset
|
43 text = e.text; |
|
11081
0b68697450c5
util.error: Add well-known field 'extra'
Kim Alvefur <zash@zash.se>
parents:
11080
diff
changeset
|
44 extra = e.extra; |
|
11080
ba77c142c9b1
util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents:
11079
diff
changeset
|
45 }; |
|
ba77c142c9b1
util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents:
11079
diff
changeset
|
46 else |
|
ba77c142c9b1
util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents:
11079
diff
changeset
|
47 template = {}; |
|
ba77c142c9b1
util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents:
11079
diff
changeset
|
48 end |
|
ba77c142c9b1
util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents:
11079
diff
changeset
|
49 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
|
50 context = context or {}; |
|
11050
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10501
diff
changeset
|
51 |
|
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10501
diff
changeset
|
52 if auto_inject_traceback then |
|
11224
8143fd2f138b
util.error: Switch to util.debug traceback tables and remove display_tracebacks option
Matthew Wild <mwild1@gmail.com>
parents:
11223
diff
changeset
|
53 context.traceback = util_debug.get_traceback_table(nil, 2); |
|
11050
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10501
diff
changeset
|
54 end |
|
51be24b16e8a
util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents:
10501
diff
changeset
|
55 |
|
11077
8ea430de5fd3
util.error: Minor tweaks to error creation code to prepare for future changes
Matthew Wild <mwild1@gmail.com>
parents:
11076
diff
changeset
|
56 local error_instance = setmetatable({ |
|
11075
d8fad2b48b05
util.error: Add unique 'instance_id' to error objects
Matthew Wild <mwild1@gmail.com>
parents:
11054
diff
changeset
|
57 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
|
58 |
|
11101
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
59 type = template.type or "cancel"; |
|
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
60 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
|
61 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
|
62 code = template.code; |
|
11101
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
63 extra = template.extra; |
|
9746
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 |
|
11077
8ea430de5fd3
util.error: Minor tweaks to error creation code to prepare for future changes
Matthew Wild <mwild1@gmail.com>
parents:
11076
diff
changeset
|
65 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
|
66 source = source; |
|
9746
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 }, 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
|
68 |
|
8ea430de5fd3
util.error: Minor tweaks to error creation code to prepare for future changes
Matthew Wild <mwild1@gmail.com>
parents:
11076
diff
changeset
|
69 return error_instance; |
|
9746
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 end |
|
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 |
|
11101
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
72 -- compact --> normal form |
|
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
73 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
|
74 local mapped = {} |
|
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
75 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
|
76 local e = { |
|
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
77 type = template[1]; |
|
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
78 condition = template[2]; |
|
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
79 text = template[3]; |
|
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
80 }; |
|
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
81 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
|
82 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
|
83 end |
|
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
84 mapped[err] = e; |
|
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
85 end |
|
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
86 return mapped; |
|
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
87 end |
|
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
88 |
|
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
89 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
|
90 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
|
91 -- 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
|
92 registry, namespace = namespace, nil; |
|
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
93 end |
|
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
94 local _, protoerr = next(registry, nil); |
|
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
95 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
|
96 registry = expand_registry(namespace, registry); |
|
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
97 end |
|
11221
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11207
diff
changeset
|
98 |
|
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11207
diff
changeset
|
99 local function wrap(e, context) |
|
11222
4b39691a274e
util.error: rename is_err() -> is_error()
Matthew Wild <mwild1@gmail.com>
parents:
11221
diff
changeset
|
100 if is_error(e) then |
|
11221
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11207
diff
changeset
|
101 return e; |
|
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11207
diff
changeset
|
102 end |
|
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11207
diff
changeset
|
103 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
|
104 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
|
105 }, context, registry, source); |
|
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11207
diff
changeset
|
106 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
|
107 return err; |
|
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11207
diff
changeset
|
108 end |
|
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11207
diff
changeset
|
109 |
|
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
|
110 return { |
|
11098
5b778ec095f0
util.error: Expose source and registry as fields on the registry object
Kim Alvefur <zash@zash.se>
parents:
11095
diff
changeset
|
111 source = source; |
|
5b778ec095f0
util.error: Expose source and registry as fields on the registry object
Kim Alvefur <zash@zash.se>
parents:
11095
diff
changeset
|
112 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
|
113 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
|
114 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
|
115 end; |
|
11221
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11207
diff
changeset
|
116 coerce = function (ok, err, ...) |
|
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11207
diff
changeset
|
117 if ok then |
|
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11207
diff
changeset
|
118 return ok, err, ...; |
|
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11207
diff
changeset
|
119 end |
|
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11207
diff
changeset
|
120 return nil, wrap(err); |
|
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11207
diff
changeset
|
121 end; |
|
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11207
diff
changeset
|
122 wrap = wrap; |
|
11223
b8589256b404
util.error: Expose is_error on registry objects for convenience
Matthew Wild <mwild1@gmail.com>
parents:
11222
diff
changeset
|
123 is_error = is_error; |
|
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
|
124 }; |
|
11054
ad07152d7bde
util.error: Add a wrapper for common parameters
Kim Alvefur <zash@zash.se>
parents:
11053
diff
changeset
|
125 end |
|
ad07152d7bde
util.error: Add a wrapper for common parameters
Kim Alvefur <zash@zash.se>
parents:
11053
diff
changeset
|
126 |
|
9746
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
127 local function coerce(ok, err, ...) |
|
11222
4b39691a274e
util.error: rename is_err() -> is_error()
Matthew Wild <mwild1@gmail.com>
parents:
11221
diff
changeset
|
128 if ok or is_error(err) then |
|
9746
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 return ok, err, ...; |
|
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
130 end |
|
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
131 |
|
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
|
132 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
|
133 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
|
134 }, { wrapped_error = err }); |
|
9746
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
135 |
|
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 return ok, new_err, ...; |
|
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 end |
|
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
138 |
|
11094
03fdf41fd948
util.error: Pass converted stanza errors throguh new()
Kim Alvefur <zash@zash.se>
parents:
11092
diff
changeset
|
139 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
|
140 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
|
141 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
|
142 context = context or {}; |
|
35d2260644d9
util.error: Extract error originator from stanza errors
Kim Alvefur <zash@zash.se>
parents:
11081
diff
changeset
|
143 context.stanza = stanza; |
|
13078
6da83deb8d7f
util.error: Fix error on conversion of invalid error stanza, fix #1805
Kim Alvefur <zash@zash.se>
parents:
11224
diff
changeset
|
144 context.by = error_tag and error_tag.attr.by or stanza.attr.from; |
|
11090
33b6fbdcec88
util.error: Default error originator to stanza sender
Kim Alvefur <zash@zash.se>
parents:
11089
diff
changeset
|
145 |
|
11095
1ea3574b19c8
util.error: Turns out <gone> wasn't alone, there's also <redirect>
Kim Alvefur <zash@zash.se>
parents:
11094
diff
changeset
|
146 local uri; |
|
1ea3574b19c8
util.error: Turns out <gone> wasn't alone, there's also <redirect>
Kim Alvefur <zash@zash.se>
parents:
11094
diff
changeset
|
147 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
|
148 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
|
149 end |
|
1ea3574b19c8
util.error: Turns out <gone> wasn't alone, there's also <redirect>
Kim Alvefur <zash@zash.se>
parents:
11094
diff
changeset
|
150 |
|
11094
03fdf41fd948
util.error: Pass converted stanza errors throguh new()
Kim Alvefur <zash@zash.se>
parents:
11092
diff
changeset
|
151 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
|
152 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
|
153 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
|
154 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
|
155 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
|
156 uri = uri; |
|
11092
bd13aa89262d
util.error: Collect Application-Specific Conditions from stanza errors
Kim Alvefur <zash@zash.se>
parents:
11091
diff
changeset
|
157 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
|
158 } or nil; |
|
11094
03fdf41fd948
util.error: Pass converted stanza errors throguh new()
Kim Alvefur <zash@zash.se>
parents:
11092
diff
changeset
|
159 }, 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
|
160 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
|
161 |
|
9746
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
162 return { |
|
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
163 new = new; |
|
11054
ad07152d7bde
util.error: Add a wrapper for common parameters
Kim Alvefur <zash@zash.se>
parents:
11053
diff
changeset
|
164 init = init; |
|
9746
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
165 coerce = coerce; |
|
11222
4b39691a274e
util.error: rename is_err() -> is_error()
Matthew Wild <mwild1@gmail.com>
parents:
11221
diff
changeset
|
166 is_error = is_error; |
|
4b39691a274e
util.error: rename is_err() -> is_error()
Matthew Wild <mwild1@gmail.com>
parents:
11221
diff
changeset
|
167 is_err = is_error; -- COMPAT w/ older 0.12 trunk |
|
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
|
168 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
|
169 configure = configure; |
|
9746
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
170 } |