|
21 | 21 | import natsort
|
22 | 22 | import rfc822 # Used for parsing RFC822 into datetime
|
23 | 23 | import email # Used for formatting TS into RFC822
|
| 24 | +import traceback |
24 | 25 |
|
25 | 26 |
|
26 | 27 | # Prepare global variables
|
@@ -851,19 +852,24 @@ def search_url(country_code, query):
|
851 | 852 | data['result'] = {}
|
852 | 853 | return formatResponse(data, 304)
|
853 | 854 |
|
854 |
| - if country_code is not None: |
855 |
| - if len(country_code) > 3: |
856 |
| - data['result'] = {'message': 'Invalid country code value.'} |
857 |
| - return formatResponse(data, code) |
858 |
| - query_filter = {'country_code': country_code.encode('utf-8').split(',')} |
859 |
| - |
860 |
| - # Common search for query with filters |
861 |
| - rc, result = search(query.encode('utf-8'), query_filter, autocomplete) |
862 |
| - if rc and len(result['matches']) > 0: |
863 |
| - code = 200 |
864 |
| - |
865 |
| - data['query'] = query |
866 |
| - data['result'] = prepareResultJson(result) |
| 855 | + try: |
| 856 | + if country_code is not None: |
| 857 | + if len(country_code) > 3: |
| 858 | + data['result'] = {'message': 'Invalid country code value.'} |
| 859 | + return formatResponse(data, code) |
| 860 | + query_filter = {'country_code': country_code.encode('utf-8').split(',')} |
| 861 | + |
| 862 | + # Common search for query with filters |
| 863 | + rc, result = search(query.encode('utf-8'), query_filter, autocomplete) |
| 864 | + if rc and len(result['matches']) > 0: |
| 865 | + code = 200 |
| 866 | + |
| 867 | + data['query'] = query |
| 868 | + data['result'] = prepareResultJson(result) |
| 869 | + except: |
| 870 | + traceback.print_exc() |
| 871 | + data['result'] = {'message': 'Unexpected failure to handle this request. Please, contact sysadmin.'} |
| 872 | + code = 500 |
867 | 873 |
|
868 | 874 | return formatResponse(data, code)
|
869 | 875 |
|
@@ -1146,38 +1152,43 @@ def reverse_search_url(lon, lat, classes):
|
1146 | 1152 | debug = request.args.get('debug')
|
1147 | 1153 | times = {}
|
1148 | 1154 |
|
1149 |
| - if debug: |
1150 |
| - times['start'] = time() |
1151 |
| - |
1152 | 1155 | try:
|
1153 |
| - lon = float(lon) |
1154 |
| - lat = float(lat) |
1155 |
| - except: |
1156 |
| - data['result'] = {'message': 'Longitude and latitude must be numeric.'} |
1157 |
| - return formatResponse(data, code) |
| 1156 | + if debug: |
| 1157 | + times['start'] = time() |
1158 | 1158 |
|
1159 |
| - if lon < -180.0 or lon > 180.0: |
1160 |
| - data['result'] = {'message': 'Invalid longitude.'} |
1161 |
| - return formatResponse(data, code) |
1162 |
| - if lat < -90.0 or lat > 90.0: |
1163 |
| - data['result'] = {'message': 'Invalid latitude.'} |
1164 |
| - return formatResponse(data, code) |
| 1159 | + try: |
| 1160 | + lon = float(lon) |
| 1161 | + lat = float(lat) |
| 1162 | + except: |
| 1163 | + data['result'] = {'message': 'Longitude and latitude must be numeric.'} |
| 1164 | + return formatResponse(data, code) |
1165 | 1165 |
|
1166 |
| - if debug: |
1167 |
| - times['prepare'] = time() - times['start'] |
| 1166 | + if lon < -180.0 or lon > 180.0: |
| 1167 | + data['result'] = {'message': 'Invalid longitude.'} |
| 1168 | + return formatResponse(data, code) |
| 1169 | + if lat < -90.0 or lat > 90.0: |
| 1170 | + data['result'] = {'message': 'Invalid latitude.'} |
| 1171 | + return formatResponse(data, code) |
1168 | 1172 |
|
1169 |
| - code = 200 |
1170 |
| - filter_classes = [] |
1171 |
| - if classes: |
1172 |
| - # This argument can be list separated by comma |
1173 |
| - filter_classes = classes.encode('utf-8').split(',') |
1174 |
| - result, distance = reverse_search(lon, lat, filter_classes, debug) |
1175 |
| - data['result'] = prepareResultJson(result) |
1176 |
| - if debug: |
1177 |
| - times['process'] = time() - times['start'] |
1178 |
| - data['debug'] = result['debug'] |
1179 |
| - data['debug']['distance'] = distance |
1180 |
| - data['debug_times'] = times |
| 1173 | + if debug: |
| 1174 | + times['prepare'] = time() - times['start'] |
| 1175 | + |
| 1176 | + code = 200 |
| 1177 | + filter_classes = [] |
| 1178 | + if classes: |
| 1179 | + # This argument can be list separated by comma |
| 1180 | + filter_classes = classes.encode('utf-8').split(',') |
| 1181 | + result, distance = reverse_search(lon, lat, filter_classes, debug) |
| 1182 | + data['result'] = prepareResultJson(result) |
| 1183 | + if debug: |
| 1184 | + times['process'] = time() - times['start'] |
| 1185 | + data['debug'] = result['debug'] |
| 1186 | + data['debug']['distance'] = distance |
| 1187 | + data['debug_times'] = times |
| 1188 | + except: |
| 1189 | + traceback.print_exc() |
| 1190 | + data['result'] = {'message': 'Unexpected failure to handle this request. Please, contact sysadmin.'} |
| 1191 | + code = 500 |
1181 | 1192 |
|
1182 | 1193 | return formatResponse(data, code)
|
1183 | 1194 |
|
|
0 commit comments