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