Annotate

util/error.lua @ 9746:848fd204708c

util.error: Add new util library for structured errors
author Matthew Wild <mwild1@gmail.com>
date Sun, 30 Dec 2018 12:55:58 +0000
child 9749:9361bd1b9c9b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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()
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 return ("error<%s:%s:%s>"):format(self.type, self.condition, self.text);
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;
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 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
19 }, error_mt);
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 end
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 local function coerce(ok, err, ...)
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 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
24 return ok, err, ...;
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 end
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 local new_err = setmetatable({
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 native = err;
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 type = "cancel";
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 condition = "undefined-condition";
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 }, error_mt);
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 return ok, new_err, ...;
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 end
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 return {
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 new = new;
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 coerce = coerce;
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 is_err = is_err;
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 }