Software / code / prosody
Annotate
util/error.lua @ 10399:270cb2821566
mod_ping: Remove ad-hoc command
17:27:40 <Ge0rG> Zash: the Ping thing is absolutely worthless
17:27:55 <Zash> The command provided by mod_ping?
17:27:59 <pep.> To own server?
17:28:14 <Ge0rG> the Ping command in mod_admin_web, whatever it maps to
17:28:29 <Ge0rG> > Pong
> 2019-11-07T16:28:16Z
What am I supposed to do with that result?
17:28:29 <Zash> Yeah, mod_ping provides that
17:28:41 <Ge0rG> Is it a ping to my own server? Where's the RTT?
17:28:48 <Zash> Dunno if it's useful for more than verifying that the adhoc command system works
17:29:02 <Ge0rG> (it lags, but there is no indication of how much)
17:29:14 <Zash> It can't really test that itself
17:29:52 <Zash> Anyone opposed to deleting it?
17:30:42 <Zash> Half the module
17:42:47 <MattJ> Zash, I'm fine with removing it
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Thu, 07 Nov 2019 19:23:42 +0100 |
| parent | 10365:744ca71a49f7 |
| child | 10493:d9132e7412b8 |
| 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 |
|
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 local function new(e, context, registry) |
|
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 local template = (registry and registry[e]) or e or {}; |
|
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 return setmetatable({ |
|
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 type = template.type or "cancel"; |
|
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 condition = template.condition or "undefined-condition"; |
|
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 text = template.text; |
|
10365
744ca71a49f7
util.error: Add well-known field 'code' in error templates
Kim Alvefur <zash@zash.se>
parents:
10069
diff
changeset
|
17 code = template.code or 500; |
|
9746
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 |
|
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 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
|
20 }, error_mt); |
|
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 end |
|
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 |
|
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 local function coerce(ok, err, ...) |
|
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 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
|
25 return ok, err, ...; |
|
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 end |
|
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 |
|
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 local new_err = setmetatable({ |
|
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 native = err; |
|
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 |
|
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 type = "cancel"; |
|
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 condition = "undefined-condition"; |
|
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 }, error_mt); |
|
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 return ok, new_err, ...; |
|
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 end |
|
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 |
|
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
|
37 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
|
38 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
|
39 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
|
40 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
|
41 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
|
42 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
|
43 |
|
9361bd1b9c9b
util.error: Add a function for creating an error object from an error stanza
Kim Alvefur <zash@zash.se>
parents:
9746
diff
changeset
|
44 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
|
45 }, 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
|
46 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
|
47 |
|
9746
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 return { |
|
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 new = new; |
|
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 coerce = coerce; |
|
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 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
|
52 from_stanza = from_stanza; |
|
9746
848fd204708c
util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 } |