-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
152 lines (118 loc) · 4.29 KB
/
server.py
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
"""server for built2break
"""
from jinja2 import StrictUndefined
from flask import (Flask, jsonify, render_template,
redirect, request, flash, session, Markup, copy_current_request_context, has_request_context)
import bugsnag
from bugsnag.flask import handle_exceptions
import os
app = Flask(__name__)
# http://flask.pocoo.org/docs/0.12/appcontext/ ----session error
handle_exceptions(app) # bugsnag config
app.secret_key = "monkey"
app.jinja_env.endefined = StrictUndefined
bugsnag.configure(
api_key=os.environ.get("BUGSNAG_KEY"),
project_root="/",
)
def callback(notification):
"""for unhandled errors, capturing user and session data.
"""
if has_request_context():
rstage = session["rstage"]
if rstage == "staging":
return False
else:
bugsnag.configure(release_stage = rstage)
bugsnag.configure(app_version = session["vsion"])
# You can set properties of the notification
notification.user = {"name": session["user"]}
notification.add_tab("custom", {"account": session["acct"]})
notification.add_tab("custom", {"level": session["impactmsg"]})
else:
pass
# Call `callback` before every notification
bugsnag.before_notify(callback)
def set_user_info(args):
"""accepts the request args from an Ajax call,
and updates info in session.
"""
user_name = args.get("user", "Donkey Kong")
# if user leaves field blank, default string:
if user_name.strip() == "":
user_name = "NEMO"
session["user"] = user_name
session["rstage"] = args.get("rstage", "monkeys")
session["handling"] = args.get("handling", "no")
session["vsion"] = args.get("version", "2.0")
session["acct"] = args.get("acct", "free")
session["impactmsg"] = args.get("impactmsg", "monkeys")
return None
@app.route('/')
def index():
"""Homepage. Sets default session info."""
session["user"] = "Raving Rabid :D"
session["rstage"] = "production"
session["handling"] = "no"
return render_template("homepage.html")
@app.route('/index_error', methods=['POST', 'GET'])
def index_error():
"""Will generate an out of index error."""
user_info = request.args
set_user_info(user_info)
stuff = [1,2,3]
if session["handling"] == "yes":
try:
print(stuff[17])
except Exception as e:
bugsnag.notify(e, context="handled Index Error - ta da!")
# show_error_dialog(). <--what is this?? from bs docs
# return value to page ------------
else:
print("trying ...........")
#deliberate, unhandled out of range error
print(stuff[17])
@app.route('/name_error')
def name_error():
"""Will generate a name error."""
user_info = request.args
set_user_info(user_info)
if session["handling"] == "yes":
# having issues with session - but not for unhandled??? how?
try:
print(doesnt_exist)
except Exception as e:
bugsnag.notify(e, context="handled Name Error - Booyah!")
# show_error_dialog(). <--what is this?? from bs docs
# return value to page ------------
else:
print("trying ...........")
#deliberate, unhandled name error
print(doesnt_exist)
@app.route('/type_error')
def type_error():
"""Will generate a type error."""
user_info = request.args
set_user_info(user_info)
if session["handling"] == "yes":
# having issues with session - but not for unhandled??? how?
try:
print("monkey" + 3)
except Exception as e:
bugsnag.notify(e, context="handled Type Error - HUZZAH!")
# show_error_dialog(). <--what is this?? from bs docs
# return value to page ------------
else:
print("trying ...........")
#deliberate, unhandled name error
print("monkey" + 3)
##################################################
if __name__ == '__main__':
#helps with debugging
app.debug = True
#not caching on reload
app.jinja_env.auto_reload = app.debug
# fires a single error notification:
bugsnag.notify(Exception("Test Error"))
PORT = int(os.environ.get("PORT", 5000))
app.run(host="0.0.0.0", port=PORT)