Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
In molte situazioni, ti consigliamo di utilizzare i codici di stato HTTP comuni per indicare il buon esito o il fallimento della richiesta API di un utente. Ad esempio, se un utente sta tentando di recuperare un'entità inesistente, ti consigliamo di inviare un codice di stato HTTP 404 per indicare che non esiste alcuna entità con l'ID entity_id.
Puoi inviare questi codici di stato HTTP comuni sollevando un'eccezione fornita dalla libreria di endpoint come segue:
message='No entity with the id "%s" exists.'%entity_idraiseendpoints.NotFoundException(message)
Eccezione fornite da Endpoints Frameworks
La libreria di endpoint fornisce le seguenti eccezioni, corrispondenti a
codici di stato HTTP specifici:
Eccezione
Codice di stato HTTP corrispondente
endpoints.BadRequestException
HTTP 400
endpoints.UnauthorizedException
HTTP 401
endpoints.ForbiddenException
HTTP 403
endpoints.NotFoundException
HTTP 404
endpoints.InternalServerErrorException
HTTP 500
Codici di stato HTTP supportati
I framework Cloud Endpoints supportano un sottoinsieme di codici di stato HTTP nelle risposte dell'API. La seguente tabella descrive i codici supportati.
Codici di stato HTTP
Assistenza
HTTP 2xx
In genere, HTTP 200 viene assunto dai framework di endpoint se il metodo API restituisce un valore corretto. Se il tipo di risposta del metodo API è VoidMessage o il valore restituito del metodo API è None, viene impostato HTTP 204.
HTTP 3xx
I codici HTTP 3xx non sono supportati. L'utilizzo di codici HTTP 3xx comporta una risposta HTTP 404.
HTTP 4xx
Sono supportati solo i seguenti codici HTTP 4xx:
400
401
403
404
409
410
412
413
Qualsiasi altro codice HTTP 4xx viene restituito come errore 404, ad eccezione di quanto segue:
405 viene restituito come 501
408 viene restituito come 503
Errori HTTP 5xx
Tutti i codici di stato HTTP 5xx vengono convertiti in HTTP 503 nella risposta del client.
Creare classi di eccezione personalizzate
Se vuoi creare altre classi di eccezione per altri codici di stato HTTP, puoi farlo creando una sottoclasse di endpoints.ServiceException. Lo snippet riportato di seguito mostra come creare una classe di eccezione che rappresenti un codice di stato HTTP 409:
importendpointsimporthttplibclassConflictException(endpoints.ServiceException):"""Conflict exception that is mapped to a 409 response."""http_status=httplib.CONFLICT
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-09-04 UTC."],[[["\u003cp\u003eEndpoints Frameworks allows for the use of common HTTP status codes to indicate the success or failure of API requests, such as using a \u003ccode\u003e404\u003c/code\u003e error to indicate an entity does not exist.\u003c/p\u003e\n"],["\u003cp\u003eSpecific exceptions, like \u003ccode\u003eBadRequestException\u003c/code\u003e, \u003ccode\u003eUnauthorizedException\u003c/code\u003e, \u003ccode\u003eForbiddenException\u003c/code\u003e, \u003ccode\u003eNotFoundException\u003c/code\u003e, and \u003ccode\u003eInternalServerErrorException\u003c/code\u003e, are provided by the endpoints library and correspond to specific HTTP error codes.\u003c/p\u003e\n"],["\u003cp\u003eOnly a subset of HTTP status codes are supported, where HTTP \u003ccode\u003e200\u003c/code\u003e or \u003ccode\u003e204\u003c/code\u003e is used for success, 3xx codes result in a \u003ccode\u003e404\u003c/code\u003e error, specific 4xx codes are supported, and all 5xx codes are converted to \u003ccode\u003e503\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eUncaught exceptions in the application will result in an HTTP \u003ccode\u003e503\u003c/code\u003e error from the backend API.\u003c/p\u003e\n"],["\u003cp\u003eCustom exception classes for other HTTP status codes can be made by subclassing \u003ccode\u003eendpoints.ServiceException\u003c/code\u003e, as shown in the example of creating a \u003ccode\u003eConflictException\u003c/code\u003e for an HTTP 409 status code.\u003c/p\u003e\n"]]],[],null,["# Exceptions and HTTP status codes\n\nIn many situations, you might want to use common HTTP status codes to indicate\nthe success or failure of a user's API request. For example, if a user is\nattempting to retrieve an entity which doesn't exist, you might want to send an\nHTTP `404` status code to say that no entity with the ID `entity_id` exists.\n| **Important:** An uncaught exception in your application results in an HTTP `503` error from your backend API.\n\nYou can send such common HTTP status codes by raising an exception provided\nby the endpoints library as follows: \n\n message = 'No entity with the id \"%s\" exists.' % entity_id\n raise endpoints.NotFoundException(message)\n\nExceptions provided by Endpoints Frameworks\n-------------------------------------------\n\nThe endpoints library provides the following exceptions, corresponding to\nspecific HTTP status codes:\n\nSupported HTTP status codes\n---------------------------\n\nCloud Endpoints Frameworks supports a subset of HTTP status codes\nin API responses. The following table describes the supported codes.\n\n| **Important:** Don't use custom exception classes to return HTTP 2xx codes. Endpoints Frameworks doesn't support returning HTTP `201` or any other 2xx codes except HTTP `200` and HTTP `204` as described in the preceding table.\n\nCreating your own exception classes\n-----------------------------------\n\nIf you want to create other exception classes for other HTTP status codes, you\ncan do so by subclassing `endpoints.ServiceException`. The following snippet\nshows how to create an exception class that represents an HTTP 409 status code: \n\n import endpoints\n import httplib\n\n class ConflictException(endpoints.ServiceException):\n \"\"\"Conflict exception that is mapped to a 409 response.\"\"\"\n http_status = httplib.CONFLICT"]]