Mercurial > prosody-hg
comparison util/error.lua @ 10411:db2a06b9ff98
Merge 0.11->trunk
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Sat, 16 Nov 2019 16:52:31 +0100 |
| parents | 744ca71a49f7 |
| children | d9132e7412b8 |
comparison
equal
deleted
inserted
replaced
| 10410:659b577f280c | 10411:db2a06b9ff98 |
|---|---|
| 1 local error_mt = { __name = "error" }; | |
| 2 | |
| 3 function error_mt:__tostring() | |
| 4 return ("error<%s:%s:%s>"):format(self.type, self.condition, self.text or ""); | |
| 5 end | |
| 6 | |
| 7 local function is_err(e) | |
| 8 return getmetatable(e) == error_mt; | |
| 9 end | |
| 10 | |
| 11 local function new(e, context, registry) | |
| 12 local template = (registry and registry[e]) or e or {}; | |
| 13 return setmetatable({ | |
| 14 type = template.type or "cancel"; | |
| 15 condition = template.condition or "undefined-condition"; | |
| 16 text = template.text; | |
| 17 code = template.code or 500; | |
| 18 | |
| 19 context = context or template.context or { _error_id = e }; | |
| 20 }, error_mt); | |
| 21 end | |
| 22 | |
| 23 local function coerce(ok, err, ...) | |
| 24 if ok or is_err(err) then | |
| 25 return ok, err, ...; | |
| 26 end | |
| 27 | |
| 28 local new_err = setmetatable({ | |
| 29 native = err; | |
| 30 | |
| 31 type = "cancel"; | |
| 32 condition = "undefined-condition"; | |
| 33 }, error_mt); | |
| 34 return ok, new_err, ...; | |
| 35 end | |
| 36 | |
| 37 local function from_stanza(stanza, context) | |
| 38 local error_type, condition, text = stanza:get_error(); | |
| 39 return setmetatable({ | |
| 40 type = error_type or "cancel"; | |
| 41 condition = condition or "undefined-condition"; | |
| 42 text = text; | |
| 43 | |
| 44 context = context or { stanza = stanza }; | |
| 45 }, error_mt); | |
| 46 end | |
| 47 | |
| 48 return { | |
| 49 new = new; | |
| 50 coerce = coerce; | |
| 51 is_err = is_err; | |
| 52 from_stanza = from_stanza; | |
| 53 } |
