On Mar 5, 2013, at 12:45 PM, Peter Waller <[email protected]> wrote:
> Hi All, > > First, I love Pyramid's design on the whole. Thanks very much for giving it > to the world. > > I'm writing a pyramid application where one of the ideas is that it should be > quite extensible by potential users of my project. > > One problem which concerns me a lot is that when writing code which gets > called by MyResource.__getitem__ is that it must not raise KeyError, > otherwise you get "resource not found", since those were the chosen semantics > in Pyramid. > > The thing is, this can happen if you accidentally write erroneous code which > tries to get a non-existing item from a dictionary, for example. There are > many ways to get KeyError. > > How can I prevent this from appearing as "resource not found"? At the moment, > the presence of a KeyError anywhere in the code is effectively masked by the > the resource not found logic. To me this seems like a semi-fatal mistake in > the design of Pyramid. > > I don't want such a scenario appear as "resource not found", I want it to be > "hey, there is an error in your code here on this line". > > Thanks in advance for any input, > Hi Peter, There should only really be a single way to get a key error, and that is passing a collection a bad key (of course python is for consenting adults and makes no promises here). As KeyError is the protocol for dictionaries and lists in python by default so I think it's a pretty reasonable design for most traversal use cases which are over hierarchies of dictionary-ish objects. so my question is what are you doing (or expecting to happen) inside __getitem__ where you get unexpected key errors? IIRC, if it's really critical to override the default behavior, you can register a custom traverser and use custom exceptions to create your own protocol. in my experience with traversal, keeping the structure and dispatch(i.e. __getitem__) simple saves a headaches. Factoring complex behavior out of __getitem__ to where you can test it thoroughly also helps a lot. d. "whit" morriss Platform Codemonkey [email protected] -- You received this message because you are subscribed to the Google Groups "pylons-discuss" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/pylons-discuss?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
