@@ -0,0 +1,2959 @@
+#%0A# Copyright (c) Elliot Peele %3Celliot@bentlogic.net%3E%0A#%0A# This program is distributed under the terms of the MIT License as found%0A# in a file called LICENSE. If it is not present, the license%0A# is always available at http://www.opensource.org/licenses/mit-license.php.%0A#%0A# This program is distributed in the hope that it will be useful, but%0A# without any waranty; without even the implied warranty of merchantability%0A# or fitness for a particular purpose. See the MIT License for full details.%0A#%0A%0Afrom string import Template%0A%0Afrom pyramid import httpexceptions%0Afrom pyramid.httpexceptions import text_type%0Afrom pyramid.httpexceptions import _no_escape%0Afrom pyramid.httpexceptions import WSGIHTTPException%0A%0Adef _quote_escape(value):%0A    v = _no_escape(value)%0A    return v.replace('%22', '%5C%5C%22')%0A%0A%0Aclass BaseJsonHTTPError(WSGIHTTPException):%0A    %22%22%22%0A    Base error class for rendering errors in JSON.%0A    %22%22%22%0A%0A    json_template_obj = Template('''%5C%0A%7B%0A    %22status%22: %22$%7Bstatus%7D%22,%0A    %22code%22: $%7Bcode%7D,%0A    %22explanation%22: %22$%7Bexplanation%7D%22,%0A    %22detail%22: %22$%7Bdetail%7D%22%0A%7D%0A$%7Bhtml_comment%7D%0A''')%0A%0A    def prepare(self, environ):%0A        %22%22%22%0A        Always return errors in JSON.%0A        %22%22%22%0A%0A        if not self.body and not self.empty_body:%0A            html_comment = ''%0A            comment = self.comment or ''%0A            accept = environ.get('HTTP_ACCEPT', '')%0A            if 'text/plain' in accept:%0A                self.content_type = 'text/plain'%0A                escape = _no_escape%0A                page_template = self.plain_template_obj%0A                br = '%5Cn'%0A                if comment:%0A                    html_comment = escape(comment)%0A            else:%0A                self.content_type = 'aplication/json'%0A                escape = _quote_escape%0A                page_template = self.json_template_obj%0A                br = '%5Cn'%0A                if comment:%0A                    html_comment = '# %25s' %25 comment%0A            args = %7B%0A                'br': br,%0A                'explanation': escape(self.explanation),%0A                'detail': escape(self.detail or ''),%0A                'comment': escape(comment),%0A                'html_comment': html_comment,%0A                %7D%0A            for k, v in environ.items():%0A                if (not k.startswith('wsgi.')) and ('.' in k):%0A                    continue%0A                args%5Bk%5D = escape(v)%0A            for k, v in self.headers.items():%0A                args%5Bk.lower()%5D = escape(v)%0A            page = page_template.substitute(status=self.status,%0A                code=self.code, **args)%0A            if isinstance(page, text_type):%0A                page = page.encode(self.charset)%0A            self.app_iter = %5Bpage%5D%0A            self.body = page%0A%0A%0Aclass HTTPBadRequest(httpexceptions.HTTPBadRequest, BaseJsonHTTPError):%0A    pass%0A%0A%0Aclass HTTPUnauthorized(httpexceptions.HTTPUnauthorized, BaseJsonHTTPError):%0A    pass%0A%0A%0Aclass HTTPMethodNotAllowed(httpexceptions.HTTPMethodNotAllowed,%0A    BaseJsonHTTPError):%0A    pass%0A

switch to json http exceptions