Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Halaman ini mencantumkan pengecualian yang disediakan oleh library endpoints serta kode status HTTP yang didukung oleh Cloud Endpoints Frameworks.
Dalam banyak situasi, Anda mungkin ingin menggunakan kode status HTTP umum untuk menunjukkan
keberhasilan atau kegagalan permintaan API pengguna. Misalnya, jika pengguna
berupaya mengambil entitas yang tidak ada, Anda mungkin ingin mengirim
kode status HTTP 404 untuk menyatakan bahwa tidak ada entitas dengan ID: entityId.
Anda dapat mengirim kode status HTTP umum dengan memunculkan pengecualian yang disediakan
oleh library endpoints sebagai berikut:
thrownewNotFoundException(user.getEmail());
Pengecualian yang diberikan oleh Endpoints Frameworks
Endpoints Frameworks menyediakan pengecualian berikut, yang sesuai
dengan kode status HTTP tertentu:
Framework Cloud Endpoints mendukung subset kode status HTTP dalam respons API. Tabel berikut menjelaskan kode yang didukung.
Kode status HTTP
Dukungan
HTTP 2xx
HTTP 200 biasanya diasumsikan oleh Endpoints Frameworks jika metode API berhasil ditampilkan. Jika jenis respons metode API adalah void atau nilai yang ditampilkan oleh metode API adalah null , HTTP 204 akan ditetapkan.
HTTP 3xx
Kode 3xx HTTP 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 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 Anda ingin membuat class pengecualian lain untuk kode status HTTP lainnya, Anda
harus membuat subclass com.google.api.server.spi.ServiceException. Cuplikan
berikut menunjukkan cara membuat class pengecualian yang merepresentasikan kode status
HTTP 408:
importcom.google.api.server.spi.ServiceException;// Exceptions must inherit from ServiceExceptionpublicclassRequestTimeoutExceptionextendsServiceException{publicRequestTimeoutException(Stringmessage){super(408,message);}}
Menggunakan kode status HTTP yang tidak didukung
Anda dapat menggunakan kode status HTTP yang tidak ada dalam daftar yang didukung di atas. Perhatikan bahwa jika Anda memutuskan untuk melakukannya, hal ini dapat menimbulkan konsekuensi yang tidak diinginkan bagi library klien yang mengakses API. Untuk menggunakan kode error yang tidak didukung,
buat class pengecualian Anda sendiri, seperti yang dijelaskan di bagian sebelumnya, dan
tambahkan parameter inisialisasi servlet baru enableExceptionCompatibility ke
EndpointsServlet:
[[["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-18 UTC."],[[["\u003cp\u003eThe \u003ccode\u003eendpoints\u003c/code\u003e library provides exceptions that correspond to common HTTP status codes, allowing developers to indicate the success or failure of API requests.\u003c/p\u003e\n"],["\u003cp\u003eEndpoints Frameworks offers built-in exception classes for HTTP status codes like \u003ccode\u003e400\u003c/code\u003e, \u003ccode\u003e401\u003c/code\u003e, \u003ccode\u003e403\u003c/code\u003e, \u003ccode\u003e404\u003c/code\u003e, \u003ccode\u003e409\u003c/code\u003e, \u003ccode\u003e500\u003c/code\u003e, and \u003ccode\u003e503\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eHTTP \u003ccode\u003e200\u003c/code\u003e and \u003ccode\u003e204\u003c/code\u003e are supported for successful API methods, while HTTP 3xx codes are not supported and will result in an HTTP \u003ccode\u003e404\u003c/code\u003e response.\u003c/p\u003e\n"],["\u003cp\u003eWhile a subset of HTTP 4xx codes are directly supported, many others are converted to \u003ccode\u003e404\u003c/code\u003e, and all HTTP 5xx codes are converted to \u003ccode\u003e503\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can create custom exception classes for unsupported HTTP status codes by subclassing \u003ccode\u003ecom.google.api.server.spi.ServiceException\u003c/code\u003e and enabling \u003ccode\u003eenableExceptionCompatibility\u003c/code\u003e in the \u003ccode\u003eEndpointsServlet\u003c/code\u003e configuration.\u003c/p\u003e\n"]]],[],null,["# Exceptions and HTTP status codes\n\nThis page lists the exceptions provided by the `endpoints` library as well as\nHTTP status codes supported by Cloud Endpoints Frameworks.\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 exists with ID: `entityId`.\n\nYou can send common HTTP status codes by throwing an exception provided\nby the `endpoints` library as follows: \n\n throw new NotFoundException(user.getEmail());\n\nExceptions provided by Endpoints Frameworks\n-------------------------------------------\n\nEndpoints Frameworks provides the following exceptions, corresponding\nto specific 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\nneed to subclass `com.google.api.server.spi.ServiceException`. The\nfollowing snippet shows how to create an exception class that represents an\nHTTP `408` status code: \n\n import com.google.api.server.spi.ServiceException;\n\n // Exceptions must inherit from ServiceException\n public class RequestTimeoutException extends ServiceException {\n public RequestTimeoutException(String message) {\n super(408, message);\n }\n }\n\n| **Important:** An uncaught exception in your application results in an HTTP `503` error from your Cloud Endpoints API, unless it extends `com.google.api.server.spi.ServiceException`.\n\nUsing unsupported HTTP status codes\n-----------------------------------\n\nIt is possible to use HTTP status codes that aren't in the preceding supported\nlist. Note that if you decide to do so, it might have unintended consequences\nfor client libraries that access the API. To use unsupported error codes,\ncreate your own exception class, as described in the previous section, and\nadd a new servlet initialization parameter `enableExceptionCompatibility` to\n`EndpointsServlet`: \n\n \u003cservlet\u003e\n \u003cservlet-name\u003ecom.google.api.server.spi.EndpointsServlet\u003c/servlet-name\u003e\n \u003cservlet-class\u003ecom.google.api.server.spi.EndpointsServlet\u003c/servlet-class\u003e\n \u003cinit-param\u003e\n \u003cparam-name\u003eservices\u003c/param-name\u003e\n \u003cparam-value\u003e...\u003c/param-value\u003e\n \u003c/init-param\u003e\n \u003cinit-param\u003e\n \u003cparam-name\u003eenableExceptionCompatibility\u003c/param-name\u003e\n \u003cparam-value\u003etrue\u003c/param-value\u003e\n \u003c/init-param\u003e\n \u003c/servlet\u003e"]]