Software / code / prosody
Comparison
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 |
comparison
equal
deleted
inserted
replaced
| 11220:9b25eecde9e6 | 11221:b0a563716334 |
|---|---|
| 96 end | 96 end |
| 97 local _, protoerr = next(registry, nil); | 97 local _, protoerr = next(registry, nil); |
| 98 if protoerr and type(next(protoerr)) == "number" then | 98 if protoerr and type(next(protoerr)) == "number" then |
| 99 registry = expand_registry(namespace, registry); | 99 registry = expand_registry(namespace, registry); |
| 100 end | 100 end |
| 101 | |
| 102 local function wrap(e, context) | |
| 103 if is_err(e) then | |
| 104 return e; | |
| 105 end | |
| 106 local err = new(registry[e] or { | |
| 107 type = "cancel", condition = "undefined-condition" | |
| 108 }, context, registry, source); | |
| 109 err.context.wrapped_error = e; | |
| 110 return err; | |
| 111 end | |
| 112 | |
| 101 return { | 113 return { |
| 102 source = source; | 114 source = source; |
| 103 registry = registry; | 115 registry = registry; |
| 104 new = function (e, context) | 116 new = function (e, context) |
| 105 return new(e, context, registry, source); | 117 return new(e, context, registry, source); |
| 106 end; | 118 end; |
| 119 coerce = function (ok, err, ...) | |
| 120 if ok then | |
| 121 return ok, err, ...; | |
| 122 end | |
| 123 return nil, wrap(err); | |
| 124 end; | |
| 125 wrap = wrap; | |
| 107 }; | 126 }; |
| 108 end | 127 end |
| 109 | 128 |
| 110 local function coerce(ok, err, ...) | 129 local function coerce(ok, err, ...) |
| 111 if ok or is_err(err) then | 130 if ok or is_err(err) then |