-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patherrors.osl
More file actions
163 lines (157 loc) · 4.91 KB
/
Copy patherrors.osl
File metadata and controls
163 lines (157 loc) · 4.91 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
def pruneSiteErrors() (
array stored = errorDocs.sort("created", db.desc).limit(1100).all()
for i stored.len (
if i >= 1000 (
object entry = stored[i].assert(object)
errorDocs.deleteOne({_id: entry["_id"]})
)
)
)
def handleRecordSiteError(*serve.Context c) (
object body = c.bodyJSON()
string message = body.message.toStr().strip()
if message == "" (
c.badRequest("error message is required")
return
)
if message.len > 2000 (
message = message.trim(1, 2000)
)
string stack = ""
if body.contains("stack") and body.stack != null (
stack = body.stack.toStr().trim(1, 20000)
)
if stack == "" and body.contains("trace") and body.trace != null (
stack = body.trace.toStr().trim(1, 20000)
)
string url = ""
if body.contains("url") and body.url != null (
url = body.url.toStr().strip().trim(1, 2000)
)
string projectId = ""
if body.contains("projectId") and body.projectId != null (
projectId = body.projectId.toStr().strip().trim(1, 120)
)
string kind = "uncaught"
if body.contains("kind") and body.kind != null (
kind = body.kind.toStr().toLower().strip().trim(1, 40)
)
if kind != "uncaught" and kind != "rejection" and kind != "react" and kind != "manual" (
kind = "uncaught"
)
string viewport = ""
if body.contains("viewport") and body.viewport != null (
viewport = body.viewport.toStr().strip().trim(1, 40)
)
string appVersion = ""
if body.contains("version") and body.version != null (
appVersion = body.version.toStr().strip().trim(1, 120)
)
if appVersion == "" and body.contains("appVersion") and body.appVersion != null (
appVersion = body.appVersion.toStr().strip().trim(1, 120)
)
string componentStack = ""
if body.contains("componentStack") and body.componentStack != null (
componentStack = body.componentStack.toStr().trim(1, 10000)
)
string userAgent = c.header("User-Agent").strip().trim(1, 500)
string username = c.getString("username")
object entry = {
_id: "e" ++ randomString(16),
message: message,
stack: stack,
url: url,
projectId: projectId,
kind: kind,
username: username,
userAgent: userAgent,
viewport: viewport,
appVersion: appVersion,
componentStack: componentStack,
created: timestamp,
resolved: false
}
sync.lock("site-errors")
errorDocs.insertOne(entry)
pruneSiteErrors()
sync.unlock("site-errors")
c.json(201, {ok: true})
)
def handleAdminGetErrors(*serve.Context c) (
string show = c.query("show").toLower().strip()
array items = errorDocs.sort("created", db.desc).limit(200).all()
if show != "all" and show != "resolved" (
array open = []
for i items.len (
object item = items[i].assert(object)
if item.resolved != true (
open.append(item)
)
)
items = open
) else if show == "resolved" (
array resolved = []
for i items.len (
object item = items[i].assert(object)
if item.resolved == true (
resolved.append(item)
)
)
items = resolved
)
number openCount = errorDocs.where("resolved", db.equal, false).count()
c.json(200, {ok: true, errors: items, openCount: openCount})
)
def handleAdminResolveSiteError(*serve.Context c) (
object body = c.bodyJSON()
string id = body.id.toStr()
if id == "" (
c.badRequest("error id is required")
return
)
boolean resolved = true
if body.contains("resolved") (
resolved = body.resolved == true
)
object entry = errorDocs.findById(id)
if entry.isEmpty() == true (
c.notFound("error not found")
return
)
entry.resolved = resolved
entry.resolvedBy = c.getString("username")
entry.resolvedAt = timestamp
if putDocument(errorDocs, id, entry) (
c.json(200, {ok: true})
return
)
c.internalError("could not update error")
)
def deleteAllSiteErrors() number (
sync.lock("site-errors")
array entries = errorDocs.all()
for i entries.len (
object entry = entries[i].assert(object)
errorDocs.deleteOne({_id: entry["_id"]})
)
sync.unlock("site-errors")
return entries.len
)
def handleAdminDeleteAllSiteErrors(*serve.Context c) (
number deleted = deleteAllSiteErrors()
c.json(200, {ok: true, deleted: deleted})
)
def handleAdminDeleteSiteError(*serve.Context c) (
object body = c.bodyJSON()
string id = body.id.toStr()
if id == "" (
c.badRequest("error id is required")
return
)
if errorDocs.findById(id).isEmpty() == true (
c.notFound("error not found")
return
)
errorDocs.deleteOne({_id: id})
c.json(200, {ok: true})
)