-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathresponses.lua
94 lines (83 loc) · 2.5 KB
/
responses.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
-- Response utils
-- ==============
--
-- Written by Bernat Romagosa and Michael Ball
--
-- Copyright (C) 2019 by Bernat Romagosa and Michael Ball
--
-- This file is part of Snap Cloud.
--
-- Snap Cloud is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Affero General Public License as
-- published by the Free Software Foundation, either version 3 of
-- the License, or (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU Affero General Public License for more details.
--
-- You should have received a copy of the GNU Affero General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
-- Responses
jsonResponse = function (json)
return {
layout = false,
status = 200,
readyState = 4,
json = json or {}
}
end
xmlResponse = function (xml)
return {
layout = false,
status = 200,
readyState = 4,
content_type = "text/xml",
xml
}
end
okResponse = function (message)
return jsonResponse({ message = message })
end
rawResponse = function (contents)
return {
layout = false,
status = 200,
readyState = 4,
contents
}
end
local html_error = function (self, error, status)
status = status or 500
self.locale = package.loaded.locale
self.locale.language = self.session.locale or 'en'
self.title = status .. ' Error'
self.contents = error
if status > 404 then
self.contents = [[
An unexpected error occurred.
Reach out to [email protected] if you continue to experience trouble.
Please include the details of the following message:
]] .. error
end
return { layout = 'layout_bs', render = 'message', status = status }
end
errorResponse = function (self, err, status)
local is_html_page = (self.req.headers['accept'] or ''):match('text/html')
if is_html_page then
return html_error(self, err, status)
else
return {
layout = false,
status = status or 500,
readyState = 4,
json = { errors = { err } }
}
end
end
html_message_page = function (self, title, contents)
self.title = title
self.contents = contents
return { render = 'message', layout = 'layout_bs', status = 200 }
end