Mercurial > piecrust2
comparison piecrust/page.py @ 1177:e307f61d7034
rendering: Improve error handling.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Thu, 21 May 2020 22:05:30 -0700 |
parents | af57fbe9435a |
children | a7c43131d871 |
comparison
equal
deleted
inserted
replaced
1176:28c388fc18b2 | 1177:e307f61d7034 |
---|---|
256 def load_page(source, content_item): | 256 def load_page(source, content_item): |
257 try: | 257 try: |
258 with source.app.env.stats.timerScope('PageLoad'): | 258 with source.app.env.stats.timerScope('PageLoad'): |
259 return _do_load_page(source, content_item) | 259 return _do_load_page(source, content_item) |
260 except Exception as e: | 260 except Exception as e: |
261 logger.exception("Error loading page: %s" % content_item.spec) | 261 logger.exception("Error loading page '%s': %s" % (content_item.spec, e)) |
262 raise PageLoadingError(content_item.spec) from e | 262 raise PageLoadingError(content_item.spec) from e |
263 | 263 |
264 | 264 |
265 def _do_load_page(source, content_item): | 265 def _do_load_page(source, content_item): |
266 # Check the cache first. | 266 # Check the cache first. |
268 cache = app.cache.getCache('pages') | 268 cache = app.cache.getCache('pages') |
269 cache_token = "%s@%s" % (source.name, content_item.spec) | 269 cache_token = "%s@%s" % (source.name, content_item.spec) |
270 cache_path = hashlib.md5(cache_token.encode('utf8')).hexdigest() + '.json' | 270 cache_path = hashlib.md5(cache_token.encode('utf8')).hexdigest() + '.json' |
271 page_time = source.getItemMtime(content_item) | 271 page_time = source.getItemMtime(content_item) |
272 if cache.isValid(cache_path, page_time): | 272 if cache.isValid(cache_path, page_time): |
273 cache_data = json.loads( | 273 try: |
274 cache.read(cache_path), | 274 cache_data = json.loads( |
275 object_pairs_hook=collections.OrderedDict) | 275 cache.read(cache_path), |
276 config = PageConfiguration( | 276 object_pairs_hook=collections.OrderedDict) |
277 values=cache_data['config'], | 277 config = PageConfiguration( |
278 validate=False) | 278 values=cache_data['config'], |
279 content = json_load_segments(cache_data['content']) | 279 validate=False) |
280 return config, content, True | 280 content = json_load_segments(cache_data['content']) |
281 return config, content, True | |
282 except Exception as e: | |
283 logger.exception("Error loading cache for '%s': %s" % (content_item.spec, e)) | |
284 logger.exception("Falling back on actual page.") | |
281 | 285 |
282 # Nope, load the page from the source file. | 286 # Nope, load the page from the source file. |
283 logger.debug("Loading page configuration from: %s" % content_item.spec) | 287 logger.debug("Loading page configuration from: %s" % content_item.spec) |
284 with source.openItem(content_item, 'r', encoding='utf-8') as fp: | 288 with source.openItem(content_item, 'r', encoding='utf-8') as fp: |
285 raw = fp.read() | 289 raw = fp.read() |