Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Dalam banyak situasi, Anda mungkin ingin menggunakan kode status HTTP umum untuk menunjukkan
keberhasilan atau kegagalan permintaan API pengguna. Misalnya, jika pengguna
mencoba mengambil entitas yang tidak ada, Anda dapat mengirim
kode status HTTP 404 untuk menyatakan bahwa tidak ada entitas dengan ID entity_id.
Anda dapat mengirim kode status HTTP umum tersebut dengan memunculkan pengecualian yang disediakan
oleh library endpoint sebagai berikut:
message='No entity with the id "%s" exists.'%entity_idraiseendpoints.NotFoundException(message)
Pengecualian yang disediakan oleh Framework Endpoints
Library endpoint menyediakan pengecualian berikut, yang sesuai dengan kode status HTTP tertentu:
Pengecualian
Kode status HTTP yang sesuai
endpoints.BadRequestException
HTTP 400
endpoints.UnauthorizedException
HTTP 401
endpoints.ForbiddenException
HTTP 403
endpoints.NotFoundException
HTTP 404
endpoints.InternalServerErrorException
HTTP 500
Kode status HTTP yang didukung
Framework Cloud Endpoints mendukung sebagian kode status HTTP
dalam respons API. Tabel berikut menjelaskan kode yang didukung.
Kode status HTTP
Dukungan
HTTP 2xx
HTTP 200 biasanya diasumsikan oleh Framework Endpoint jika metode API berhasil ditampilkan. Jika jenis respons metode API adalah VoidMessage atau nilai yang ditampilkan dari metode API adalah None , HTTP 204 akan ditetapkan.
HTTP 3xx
Kode HTTP 3xx tidak didukung. Penggunaan kode HTTP 3xx akan menghasilkan respons HTTP 404.
HTTP 4xx
Hanya kode HTTP 4xx berikut yang didukung:
400
401
403
404
409
410
412
413
Kode HTTP 4xx lainnya akan ditampilkan sebagai error 404, kecuali untuk kode berikut:
405 ditampilkan sebagai 501
408 ditampilkan sebagai 503
HTTP 5xx
Semua kode status HTTP 5xx dikonversi menjadi HTTP 503 dalam respons klien.
Membuat class pengecualian Anda sendiri
Jika ingin membuat class pengecualian lain untuk kode status HTTP lainnya, Anda
dapat melakukannya dengan membuat subclass endpoints.ServiceException. Cuplikan berikut
menunjukkan cara membuat class pengecualian yang mewakili kode status HTTP 409:
importendpointsimporthttplibclassConflictException(endpoints.ServiceException):"""Conflict exception that is mapped to a 409 response."""http_status=httplib.CONFLICT
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-08-08 UTC."],[[["Endpoints Frameworks allows for the use of common HTTP status codes to indicate the success or failure of API requests, such as using a `404` error to indicate an entity does not exist."],["Specific exceptions, like `BadRequestException`, `UnauthorizedException`, `ForbiddenException`, `NotFoundException`, and `InternalServerErrorException`, are provided by the endpoints library and correspond to specific HTTP error codes."],["Only a subset of HTTP status codes are supported, where HTTP `200` or `204` is used for success, 3xx codes result in a `404` error, specific 4xx codes are supported, and all 5xx codes are converted to `503`."],["Uncaught exceptions in the application will result in an HTTP `503` error from the backend API."],["Custom exception classes for other HTTP status codes can be made by subclassing `endpoints.ServiceException`, as shown in the example of creating a `ConflictException` for an HTTP 409 status code."]]],[]]