def handler(event, context): log.info('Serving request from "{}" for "{}"'.format( event['requestContext']['identity']['sourceIp'], event['path'])) log.debug('event: {}'.format(event)) try: resp = render_page(event['path'], format='html', event=event) except FileNotFoundException as e: log.error('Caught 404 while rendering "{}"'.format(event['path'])) resp = error404(event=event) except Exception as e: log.critical('Unexpected exception rendering route "{}": {} - {}' .format(event['path'], e.__class__.__name__, str(e))) try: resp = error500(event=event) except Exception as e: log.critical('Failed to process 500 error, falling back to plain ' '500. {}: {}'.format(e.__class__.__name__, str(e))) resp = { 'statusCode': '500', 'headers': { 'Content-Type': 'text/html' }, 'body': ( '<h1>Error: 500</h1>' '<p>Server encountered an error while ' 'attempting to handle another server error.</p>' ) } log.debug(f'sending response to client:\n{resp}') return resp
def error500(errmsg='Server Error', format='html', event={}): log.info('Rendering 500!') res = render_page('/errors/500', format=format, status_msg=errmsg, event=event) res['statusCode'] = '500' return res
def handler(event, context): log.info('Serving request from "{}" for "{}"'.format( event['requestContext']['identity']['sourceIp'], event['path'])) log.debug('event: {}'.format(event)) queryParams = event['queryStringParameters'] or {} if 'page' in queryParams: try: resp = render_page(queryParams['page'], format='json', event=event) except FileNotFoundException as e: log.error('Caught 404 while rendering "{}"'.format( queryParams['page'])) resp = error404(format='json', event=event) except Exception as e: log.critical( 'Unexpected exception rendering route "{}": {} - {}'.format( queryParams['page'], e.__class__.__name__, str(e))) try: resp = error500(format='json', event=event) except Exception as e: log.critical('Failed to process 500 error, falling back to ' 'plain 500. {}: {}'.format( e.__class__.__name__, str(e))) resp = { 'statusCode': '500', 'headers': { 'Content-Type': 'application/json' }, 'body': json.dumps({ 'title': 'Error: 500', 'content': '<p>Server encountered an error while ' 'attempting to handle another server error.</p>', 'page': queryParams.get('page', None), }) } else: log.error('Request did not provide a page parameter') resp = error400(format='json', errmsg='Missing "page" parameter', event=event) if 'headers' not in resp: resp['headers'] = {} resp['headers']['access-control-allow-methods'] = 'GET,OPTIONS' if stage == 'Beta': resp['headers']['access-control-allow-origin'] = '*' else: resp['headers']['access-control-allow-origin'] = f'https://{webdomain}' log.debug(f'sending response to client:\n{resp}') return resp
def error400(errmsg='Client Error', format='html', event={}): log.info('Rendering 400!') res = render_page('/errors/400', format=format, status_msg=errmsg, event=event) res['statusCode'] = '400' return res
def error404(errmsg='File not found', format='html', event={}): log.info('Rendering 404!') res = render_page('/errors/404', format=format, status_msg=errmsg, event=event) res['statusCode'] = '404' return res