Annotate

net/http/errors.lua @ 11049:f103f59ea2b5

net.http: http.request() promise now resolves with response (breaking change) Promise mode is not (widely?) used, changing this now while we can, as it improves usability of the API. The request is now available as response.request, if needed.
author Matthew Wild <mwild1@gmail.com>
date Tue, 25 Aug 2020 15:59:04 +0100
parent 11025:e47e7185b403
child 11225:8c17c08d100e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11025
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 -- This module returns a table that is suitable for use as a util.error registry,
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 -- and a function to return a util.error object given callback 'code' and 'body'
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 -- parameters.
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local codes = require "net.http.codes";
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local util_error = require "util.error";
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local error_templates = {
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 -- This code is used by us to report a client-side or connection error.
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 -- Instead of using the code, use the supplied body text to get one of
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 -- the more detailed errors below.
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 [0] = {
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 code = 0, type = "cancel", condition = "internal-server-error";
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 text = "Connection or internal error";
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 };
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 -- These are net.http built-in errors, they are returned in
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 -- the body parameter when code == 0
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 ["cancelled"] = {
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 code = 0, type = "cancel", condition = "remote-server-timeout";
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 text = "Request cancelled";
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 };
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 ["connection-closed"] = {
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 code = 0, type = "wait", condition = "remote-server-timeout";
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 text = "Connection closed";
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 };
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 ["certificate-chain-invalid"] = {
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 code = 0, type = "cancel", condition = "remote-server-timeout";
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 text = "Server certificate not trusted";
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 };
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 ["certificate-verify-failed"] = {
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 code = 0, type = "cancel", condition = "remote-server-timeout";
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 text = "Server certificate invalid";
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 };
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 ["connection failed"] = {
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 code = 0, type = "cancel", condition = "remote-server-not-found";
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 text = "Connection failed";
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 };
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 ["invalid-url"] = {
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 code = 0, type = "modify", condition = "bad-request";
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 text = "Invalid URL";
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 };
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 -- This doesn't attempt to map every single HTTP code (not all have sane mappings),
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 -- but all the common ones should be covered. XEP-0086 was used as reference for
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 -- most of these.
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 [400] = { type = "modify", condition = "bad-request" };
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 [401] = { type = "auth", condition = "not-authorized" };
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 [402] = { type = "auth", condition = "payment-required" };
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 [403] = { type = "auth", condition = "forbidden" };
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 [404] = { type = "cancel", condition = "item-not-found" };
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 [405] = { type = "cancel", condition = "not-allowed" };
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 [406] = { type = "modify", condition = "not-acceptable" };
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 [407] = { type = "auth", condition = "registration-required" };
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 [408] = { type = "wait", condition = "remote-server-timeout" };
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 [409] = { type = "cancel", condition = "conflict" };
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 [410] = { type = "cancel", condition = "gone" };
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 [411] = { type = "modify", condition = "bad-request" };
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 [412] = { type = "cancel", condition = "conflict" };
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 [413] = { type = "modify", condition = "resource-constraint" };
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 [414] = { type = "modify", condition = "resource-constraint" };
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 [415] = { type = "cancel", condition = "feature-not-implemented" };
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 [416] = { type = "modify", condition = "bad-request" };
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 [422] = { type = "modify", condition = "bad-request" };
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 [423] = { type = "wait", condition = "resource-constraint" };
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 [429] = { type = "wait", condition = "resource-constraint" };
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 [431] = { type = "modify", condition = "resource-constraint" };
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 [451] = { type = "auth", condition = "forbidden" };
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 [500] = { type = "wait", condition = "internal-server-error" };
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 [501] = { type = "cancel", condition = "feature-not-implemented" };
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 [502] = { type = "wait", condition = "remote-server-timeout" };
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 [503] = { type = "cancel", condition = "service-unavailable" };
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 [504] = { type = "wait", condition = "remote-server-timeout" };
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 [507] = { type = "wait", condition = "resource-constraint" };
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 [511] = { type = "auth", condition = "not-authorized" };
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 };
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 for k, v in pairs(codes) do
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 if error_templates[k] then
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 error_templates[k].code = k;
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 error_templates[k].text = v;
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 else
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 error_templates[k] = { type = "cancel", condition = "undefined-condition", text = v, code = k };
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 end
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 end
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 setmetatable(error_templates, {
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 __index = function(_, k)
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 if type(k) ~= "number" then
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 return nil;
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 end
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 return {
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 type = "cancel";
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 condition = "undefined-condition";
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 text = codes[k] or (k.." Unassigned");
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 code = k;
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 };
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 end
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 });
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 local function new(code, body, context)
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 if code == 0 then
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 return util_error.new(body, context, error_templates);
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 else
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 return util_error.new(code, context, error_templates);
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 end
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 end
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 return {
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 registry = error_templates;
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 new = new;
e47e7185b403 net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 };