Software /
code /
prosody
Annotate
net/http/errors.lua @ 12093:b446edb2a51e
CHANGES: Add various things
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 21 Dec 2021 21:38:44 +0100 |
parent | 11225:8c17c08d100e |
child | 12974:ba409c67353b |
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 }; |
11225
8c17c08d100e
net.http.errors: Add error class for DNS resolution failures (thanks SouL)
Matthew Wild <mwild1@gmail.com>
parents:
11025
diff
changeset
|
43 ["unable to resolve service"] = { |
8c17c08d100e
net.http.errors: Add error class for DNS resolution failures (thanks SouL)
Matthew Wild <mwild1@gmail.com>
parents:
11025
diff
changeset
|
44 code = 0, type = "cancel", condition = "remote-server-not-found"; |
8c17c08d100e
net.http.errors: Add error class for DNS resolution failures (thanks SouL)
Matthew Wild <mwild1@gmail.com>
parents:
11025
diff
changeset
|
45 text = "DNS resolution failed"; |
8c17c08d100e
net.http.errors: Add error class for DNS resolution failures (thanks SouL)
Matthew Wild <mwild1@gmail.com>
parents:
11025
diff
changeset
|
46 }; |
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
|
47 |
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 -- 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
|
49 -- 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
|
50 -- 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
|
51 [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
|
52 [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
|
53 [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
|
54 [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
|
55 [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
|
56 [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
|
57 [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
|
58 [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
|
59 [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
|
60 [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
|
61 [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
|
62 [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
|
63 [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
|
64 [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
|
65 [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
|
66 [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
|
67 [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
|
68 |
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 [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
|
70 [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
|
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 [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
|
73 [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
|
74 [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
|
75 |
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 [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
|
77 [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
|
78 [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
|
79 [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
|
80 [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
|
81 [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
|
82 [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
|
83 }; |
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 |
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 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
|
86 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
|
87 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
|
88 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
|
89 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
|
90 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
|
91 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
|
92 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
|
93 |
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 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
|
95 __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
|
96 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
|
97 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
|
98 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
|
99 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
|
100 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
|
101 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
|
102 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
|
103 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
|
104 }; |
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 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
|
106 }); |
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 |
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 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
|
109 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
|
110 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
|
111 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
|
112 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
|
113 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
|
114 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
|
115 |
e47e7185b403
net.http.errors: Add new module for converting net.http errors to util.error objects
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 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
|
117 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
|
118 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
|
119 }; |