Changeset 0339ba9 in trunk


Ignore:
Timestamp:
2020-10-16T13:52:41Z (4 years ago)
Author:
Jean-Paul Calderone <exarkun@…>
Branches:
master
Children:
0faa24d
Parents:
fa02e46
git-author:
Jean-Paul Calderone <exarkun@…> (2020-10-15 20:14:06)
git-committer:
Jean-Paul Calderone <exarkun@…> (2020-10-16 13:52:41)
Message:

Turn getChild None and Deferred results into something Twisted Web can manage

File:
1 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified src/allmydata/web/common.py

    rfa02e46 r0339ba9  
    2121)
    2222from twisted.web.util import (
     23    DeferredResource,
    2324    FailureElement,
    2425    redirectTo,
     
    460461
    461462
    462 def exception_to_child(f):
     463def exception_to_child(getChild):
    463464    """
    464465    Decorate ``getChild`` method with exception handling behavior to render an
    465466    error page reflecting the exception.
    466467    """
    467     @wraps(f)
     468    @wraps(getChild)
    468469    def g(self, name, req):
    469         try:
    470             return f(self, name, req)
    471         except Exception as e:
    472             description, status = humanize_exception(e)
    473             return resource.ErrorPage(status, "Error", description)
     470        bound_getChild = getChild.__get__(self, type(self))
     471        result = maybeDeferred(bound_getChild, name, req)
     472        result.addCallbacks(
     473            _getChild_done,
     474            _getChild_failed,
     475            callbackArgs=(self,),
     476        )
     477        return DeferredResource(result)
    474478    return g
     479
     480
     481def _getChild_done(child, parent):
     482    if child is None:
     483        return resource.NoResource()
     484    return child
     485
     486
     487def _getChild_failed(reason):
     488    text, code = humanize_failure(reason)
     489    return resource.ErrorPage(code, "Error", text)
    475490
    476491
Note: See TracChangeset for help on using the changeset viewer.