-
Notifications
You must be signed in to change notification settings - Fork 26
/
handle-errors.py
36 lines (23 loc) · 988 Bytes
/
handle-errors.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
import logging
from aiohttp import web
from aioalice import Dispatcher, get_new_configured_app
logging.basicConfig(format=u'%(filename)s [LINE:%(lineno)d] #%(levelname)-8s [%(asctime)s] %(message)s',
level=logging.INFO)
WEBHOOK_URL_PATH = '/my-alice-webhook/' # webhook endpoint
WEBAPP_HOST = 'localhost'
WEBAPP_PORT = 3001
dp = Dispatcher()
@dp.request_handler()
async def handle_all_requests(alice_request):
1 / 0 # some unexpected error
return alice_request.response('Hello world!')
# if any error inside handler occur
@dp.errors_handler()
async def the_only_errors_handler(alice_request, e):
# Log the error
logging.error('An error!', exc_info=e)
# Return answer so API doesn't consider your skill non-working
return alice_request.response('Oops! There was an error!')
if __name__ == '__main__':
app = get_new_configured_app(dispatcher=dp, path=WEBHOOK_URL_PATH)
web.run_app(app, host=WEBAPP_HOST, port=WEBAPP_PORT)