ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 02.01.2026

Просмотров: 2696

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

45.2. EVENT TYPES

get(k[, d ]) ! D[k] if k in D, else d. d defaults to None.

has_key(k) ! True if D has a key k, else False

items() ! list of D’s (key, value) pairs, as 2-tuples

iteritems() ! an iterator over the (key, value) items of D

iterkeys() ! an iterator over the keys of D

itervalues() ! an iterator over the values of D

keys() ! list of D’s keys

pop(k[, d ]) ! v, remove specified key and return the corresponding value. If key is not found, d is returned if given, otherwise KeyError is raised

popitem() ! (k, v), remove and return some (key, value) pair as a 2-tuple; but raise KeyError if D is empty.

setdefault(k[, d ]) ! D.get(k,d), also set D[k]=d if k not in D

values() ! list of D’s values

viewitems() ! a set-like object providing a view on D’s items

viewkeys() ! a set-like object providing a view on D’s keys

viewvalues() ! an object providing a view on D’s values

See Using Events for more information about how to register code which subscribes to these events.

565

45. PYRAMID.EVENTS

566


CHAPTER

FORTYSIX

PYRAMID.EXCEPTIONS

Forbidden

alias of HTTPForbidden

NotFound

alias of HTTPNotFound

class ConfigurationError

Raised when inappropriate input values are supplied to an API method of a Configurator

class URLDecodeError

This exception is raised when Pyramid cannot successfully decode a URL or a URL path segment. This exception it behaves just like the Python builtin UnicodeDecodeError. It is a subclass of the builtin UnicodeDecodeError exception only for identity purposes, mostly so an exception view can be registered when a URL cannot be decoded.

567

46. PYRAMID.EXCEPTIONS

568


CHAPTER

FORTYSEVEN

PYRAMID.HTTPEXCEPTIONS

47.1 HTTP Exceptions

This module contains Pyramid HTTP exception classes. Each class relates to a single HTTP status code. Each class is a subclass of the HTTPException. Each exception class is also a response object.

Each exception class has a status code according to RFC 2068: codes with 100-300 are not really errors; 400s are client errors, and 500s are server errors.

Exception

HTTPException

HTTPOk

200 - HTTPOk

201 - HTTPCreated

202 - HTTPAccepted

203 - HTTPNonAuthoritativeInformation

204 - HTTPNoContent

205 - HTTPResetContent

569

47. PYRAMID.HTTPEXCEPTIONS

• 206 - HTTPPartialContent

HTTPRedirection

300 - HTTPMultipleChoices

301 - HTTPMovedPermanently

302 - HTTPFound

303 - HTTPSeeOther

304 - HTTPNotModified

305 - HTTPUseProxy

306 - Unused (not implemented, obviously)

307 - HTTPTemporaryRedirect

HTTPError

HTTPClientError

400 - HTTPBadRequest

401 - HTTPUnauthorized

402 - HTTPPaymentRequired

403 - HTTPForbidden

404 - HTTPNotFound

405 - HTTPMethodNotAllowed

406 - HTTPNotAcceptable

407 - HTTPProxyAuthenticationRequired

408 - HTTPRequestTimeout

409 - HTTPConflict

570


47.1. HTTP EXCEPTIONS

410 - HTTPGone

411 - HTTPLengthRequired

412 - HTTPPreconditionFailed

413 - HTTPRequestEntityTooLarge

414 - HTTPRequestURITooLong

415 - HTTPUnsupportedMediaType

416 - HTTPRequestRangeNotSatisfiable

417 - HTTPExpectationFailed

422 - HTTPUnprocessableEntity

423 - HTTPLocked

424 - HTTPFailedDependency

HTTPServerError

500 - HTTPInternalServerError

501 - HTTPNotImplemented

502 - HTTPBadGateway

503 - HTTPServiceUnavailable

504 - HTTPGatewayTimeout

505 - HTTPVersionNotSupported

507 - HTTPInsufficientStorage

HTTP exceptions are also response objects, thus they accept most of the same parameters that can be passed to a regular Response. Each HTTP exception also has the following attributes:

code the HTTP status code for the exception

title remainder of the status line (stuff after the code)

571

47. PYRAMID.HTTPEXCEPTIONS

explanation a plain-text explanation of the error message that is not subject to environment or header substitutions; it is accessible in the template via ${explanation}

detail a plain-text message customization that is not subject to environment or header substitutions; accessible in the template via ${detail}

body_template a String.template-format content fragment used for environment and header substitution; the default template includes both the explanation and further detail provided in the message.

Each HTTP exception accepts the following parameters, any others will be forwarded to its Response superclass:

detail a plain-text override of the default detail

headers a list of (k,v) header pairs

comment a plain-text additional information which is usually stripped/hidden for end-users

body_template a string.Template object containing a content fragment in HTML that frames the explanation and further detail

body a string that will override the body_template and be used as the body of the response.

Substitution of response headers into template values is always performed. Substitution of WSGI environment values is performed if a request is passed to the exception’s constructor.

The subclasses of _HTTPMove (HTTPMultipleChoices, HTTPMovedPermanently, HTTPFound, HTTPSeeOther, HTTPUseProxy and HTTPTemporaryRedirect) are redirections that require a Location field. Reflecting this, these subclasses have one additional keyword argument: location, which indicates the location to which to redirect.

status_map

A mapping of integer status code to exception class (eg. the integer “401” maps to pyramid.httpexceptions.HTTPUnauthorized).

exception_response(status_code, **kw)

Creates an HTTP exception based on a status code. Example:

raise exception_response(404) # raises an HTTPNotFound exception.

The values passed as kw are provided to the exception’s constructor.

572


47.1. HTTP EXCEPTIONS

class HTTPException

Base class for all exception response objects.

class HTTPOk(detail=None, headers=None, comment=None, body_template=None, **kw)

Base class for exceptions with status codes in the 200s (successful responses)

code: 200, title: OK

class HTTPRedirection(detail=None, headers=None, comment=None,

body_template=None, **kw)

base class for exceptions with status codes in the 300s (redirections)

This is an abstract base class for 3xx redirection. It indicates that further action needs to be taken by the user agent in order to fulfill the request. It does not necessarly signal an error condition.

class HTTPError(detail=None, headers=None, comment=None, body_template=None, **kw) base class for exceptions with status codes in the 400s and 500s

This is an exception which indicates that an error has occurred, and that any work in progress should not be committed.

class HTTPClientError(detail=None, headers=None, comment=None,

body_template=None, **kw) base class for the 400s, where the client is in error

This is an error condition in which the client is presumed to be in-error. This is an expected problem, and thus is not considered a bug. A server-side traceback is not warranted. Unless specialized, this is a ‘400 Bad Request’

class HTTPServerError(detail=None, headers=None, comment=None,

body_template=None, **kw) base class for the 500s, where the server is in-error

This is an error condition in which the server is presumed to be in-error. Unless specialized, this is a ‘500 Internal Server Error’.

class HTTPCreated(detail=None, headers=None, comment=None, body_template=None,

**kw) subclass of HTTPOk

This indicates that request has been fulfilled and resulted in a new resource being created.

code: 201, title: Created

573

47. PYRAMID.HTTPEXCEPTIONS

class HTTPAccepted(detail=None, headers=None, comment=None, body_template=None,

**kw) subclass of HTTPOk

This indicates that the request has been accepted for processing, but the processing has not been completed.

code: 202, title: Accepted

class HTTPNonAuthoritativeInformation(detail=None, headers=None, com-

ment=None, body_template=None, **kw)

subclass of HTTPOk

This indicates that the returned metainformation in the entity-header is not the definitive set as available from the origin server, but is gathered from a local or a third-party copy.

code: 203, title: Non-Authoritative Information

class HTTPNoContent(detail=None, headers=None, comment=None, body_template=None,

**kw) subclass of HTTPOk

This indicates that the server has fulfilled the request but does not need to return an entity-body, and might want to return updated metainformation.

code: 204, title: No Content

class HTTPResetContent(detail=None, headers=None, comment=None,

body_template=None, **kw)

subclass of HTTPOk

This indicates that the the server has fulfilled the request and the user agent SHOULD reset the document view which caused the request to be sent.

code: 205, title: Reset Content

class HTTPPartialContent(detail=None, headers=None, comment=None,

body_template=None, **kw)

subclass of HTTPOk

This indicates that the server has fulfilled the partial GET request for the resource.

code: 206, title: Partial Content

574