NovaBryan/EVE_Dump
Log | Files | 📦 下载最新源码 (.zip) | Refs | README

exceptions.py (7345B)


      1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\carbon\common\lib\werkzeug\exceptions.py
      2 import sys
      3 from werkzeug._internal import HTTP_STATUS_CODES, _get_environ
      4 
      5 class HTTPException(Exception):
      6     code = None
      7     description = None
      8 
      9     def __init__(self, description = None):
     10         Exception.__init__(self, '%d %s' % (self.code, self.name))
     11         if description is not None:
     12             self.description = description
     13 
     14     @classmethod
     15     def wrap(cls, exception, name = None):
     16 
     17         class newcls(cls, exception):
     18 
     19             def __init__(self, arg = None, description = None):
     20                 cls.__init__(self, description)
     21                 exception.__init__(self, arg)
     22 
     23         newcls.__module__ = sys._getframe(1).f_globals.get('__name__')
     24         newcls.__name__ = name or cls.__name__ + exception.__name__
     25         return newcls
     26 
     27     @property
     28     def name(self):
     29         return HTTP_STATUS_CODES[self.code]
     30 
     31     def get_description(self, environ):
     32         environ = _get_environ(environ)
     33         return self.description
     34 
     35     def get_body(self, environ):
     36         return '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">\n<title>%(code)s %(name)s</title>\n<h1>%(name)s</h1>\n%(description)s\n' % {'code': self.code,
     37          'name': escape(self.name),
     38          'description': self.get_description(environ)}
     39 
     40     def get_headers(self, environ):
     41         return [('Content-Type', 'text/html')]
     42 
     43     def get_response(self, environ):
     44         environ = _get_environ(environ)
     45         from werkzeug.wrappers import BaseResponse
     46         headers = self.get_headers(environ)
     47         return BaseResponse(self.get_body(environ), self.code, headers)
     48 
     49     def __call__(self, environ, start_response):
     50         response = self.get_response(environ)
     51         return response(environ, start_response)
     52 
     53     def __str__(self):
     54         return unicode(self).encode('utf-8')
     55 
     56     def __unicode__(self):
     57         if 'description' in self.__dict__:
     58             txt = self.description
     59         else:
     60             txt = self.name
     61         return '%d: %s' % (self.code, txt)
     62 
     63     def __repr__(self):
     64         return "<%s '%s'>" % (self.__class__.__name__, self)
     65 
     66 
     67 class _ProxyException(HTTPException):
     68 
     69     def __init__(self, response):
     70         Exception.__init__(self, 'proxy exception for %r' % response)
     71         self.response = response
     72 
     73     def get_response(self, environ):
     74         return self.response
     75 
     76 
     77 class BadRequest(HTTPException):
     78     code = 400
     79     description = '<p>The browser (or proxy) sent a request that this server could not understand.</p>'
     80 
     81 
     82 class Unauthorized(HTTPException):
     83     code = 401
     84     description = "<p>The server could not verify that you are authorized to access the URL requested.  You either supplied the wrong credentials (e.g. a bad password), or your browser doesn't understand how to supply the credentials required.</p><p>In case you are allowed to request the document, please check your user-id and password and try again.</p>"
     85 
     86 
     87 class Forbidden(HTTPException):
     88     code = 403
     89     description = "<p>You don't have the permission to access the requested resource. It is either read-protected or not readable by the server.</p>"
     90 
     91 
     92 class NotFound(HTTPException):
     93     code = 404
     94     description = '<p>The requested URL was not found on the server.</p><p>If you entered the URL manually please check your spelling and try again.</p>'
     95 
     96 
     97 class MethodNotAllowed(HTTPException):
     98     code = 405
     99 
    100     def __init__(self, valid_methods = None, description = None):
    101         HTTPException.__init__(self, description)
    102         self.valid_methods = valid_methods
    103 
    104     def get_headers(self, environ):
    105         headers = HTTPException.get_headers(self, environ)
    106         if self.valid_methods:
    107             headers.append(('Allow', ', '.join(self.valid_methods)))
    108         return headers
    109 
    110     def get_description(self, environ):
    111         m = escape(environ.get('REQUEST_METHOD', 'GET'))
    112         return '<p>The method %s is not allowed for the requested URL.</p>' % m
    113 
    114 
    115 class NotAcceptable(HTTPException):
    116     code = 406
    117     description = '<p>The resource identified by the request is only capable of generating response entities which have content characteristics not acceptable according to the accept headers sent in the request.</p>'
    118 
    119 
    120 class RequestTimeout(HTTPException):
    121     code = 408
    122     description = "<p>The server closed the network connection because the browser didn't finish the request within the specified time.</p>"
    123 
    124 
    125 class Gone(HTTPException):
    126     code = 410
    127     description = '<p>The requested URL is no longer available on this server and there is no forwarding address.</p><p>If you followed a link from a foreign page, please contact the author of this page.'
    128 
    129 
    130 class LengthRequired(HTTPException):
    131     code = 411
    132     description = '<p>A request with this method requires a valid <code>Content-Length</code> header.</p>'
    133 
    134 
    135 class PreconditionFailed(HTTPException):
    136     code = 412
    137     description = '<p>The precondition on the request for the URL failed positive evaluation.</p>'
    138 
    139 
    140 class RequestEntityTooLarge(HTTPException):
    141     code = 413
    142     description = '<p>The data value transmitted exceeds the capacity limit.</p>'
    143 
    144 
    145 class RequestURITooLarge(HTTPException):
    146     code = 414
    147     description = '<p>The length of the requested URL exceeds the capacity limit for this server.  The request cannot be processed.</p>'
    148 
    149 
    150 class UnsupportedMediaType(HTTPException):
    151     code = 415
    152     description = '<p>The server does not support the media type transmitted in the request.</p>'
    153 
    154 
    155 class InternalServerError(HTTPException):
    156     code = 500
    157     description = '<p>The server encountered an internal error and was unable to complete your request.  Either the server is overloaded or there is an error in the application.</p>'
    158 
    159 
    160 class NotImplemented(HTTPException):
    161     code = 501
    162     description = '<p>The server does not support the action requested by the browser.</p>'
    163 
    164 
    165 class BadGateway(HTTPException):
    166     code = 502
    167     description = '<p>The proxy server received an invalid response from an upstream server.</p>'
    168 
    169 
    170 class ServiceUnavailable(HTTPException):
    171     code = 503
    172     description = '<p>The server is temporarily unable to service your request due to maintenance downtime or capacity problems.  Please try again later.</p>'
    173 
    174 
    175 default_exceptions = {}
    176 __all__ = ['HTTPException']
    177 
    178 def _find_exceptions():
    179     for name, obj in globals().iteritems():
    180         try:
    181             if getattr(obj, 'code', None) is not None:
    182                 default_exceptions[obj.code] = obj
    183                 __all__.append(obj.__name__)
    184         except TypeError:
    185             continue
    186 
    187 
    188 _find_exceptions()
    189 del _find_exceptions
    190 HTTPUnicodeError = BadRequest.wrap(UnicodeError, 'HTTPUnicodeError')
    191 
    192 class Aborter(object):
    193 
    194     def __init__(self, mapping = None, extra = None):
    195         if mapping is None:
    196             mapping = default_exceptions
    197         self.mapping = dict(mapping)
    198         if extra is not None:
    199             self.mapping.update(extra)
    200 
    201     def __call__(self, code, *args, **kwargs):
    202         if not args and not kwargs and not isinstance(code, (int, long)):
    203             raise _ProxyException(code)
    204         if code not in self.mapping:
    205             raise LookupError('no exception for %r' % code)
    206         raise self.mapping[code](*args, **kwargs)
    207 
    208 
    209 abort = Aborter()
    210 from werkzeug.utils import escape