Feature Overview
When you fail to request COS service via the SDK, such as getting 4xx or 5xx return code, the system will throw an exception (Qcloud\Cos\Exception\ServiceResponseException).
Server Exceptions
CosServiceException contains the status code returned by the server, requestId, error details, etc. After an exception is caught, it is recommended to print the entire exception to get the necessary information for troubleshooting. The following describes member variables of an exception and an example of catching an exception:
Member | Description | Local Disk Types |
requestId | Request ID, used to identify a request. It is very important for troubleshooting. | string |
statusCode | string | |
errorCode | string | |
errorMessage | string |
Example of Exception Capturing
<?phprequire dirname(__FILE__) . '/../vendor/autoload.php';$secretId = "SECRETID"; //Replace it with the actualSecretId, which can be viewed and managed in the CAM console at https://console.cloud.tencent.com/cam/capi$secretKey = "SECRETKEY"; //Replace it with the actualSecretKey, which can be viewed and managed in the CAM console at https://console.cloud.tencent.com/cam/capi$region = "ap-beijing"; //Replace it with the actualregion, which can be viewed in the console at https://console.cloud.tencent.com/cos5/bucket$cosClient = new Qcloud\Cos\Client(array('region' => $region,'schema' => 'https', //Protocol header, default is http'credentials'=> array('secretId' => $secretId ,'secretKey' => $secretKey)));try {$cosClient->listBuckets()} catch (Qcloud\Cos\Exception\ServiceResponseException $e) {$statusCode = $e->getStatusCode(); // Obtain the error code$errorMessage = $e->getMessage(); // Obtain the error message$requestId = $e->getRequestId(); // Get the error requestId$errorCode = $e->getCosErrorCode(); // Retrieve the error name$request = $e->getRequest(); // Obtain the complete request$response = $e->getResponse(); // Get the complete responseecho ($e);} catch (\Exception $e) {}
Note:
If you install the SDK using Phar or the source code, the error messages returned will be clearer. If you install with Composer and the error messages returned do not meet your requirements, you can customize some error messages by modifying
vendor/guzzlehttp/guzzle-services/src/SchemaValidator.php.Using self-diagnose tools
COS provides a self-help diagnosis tool to help you quickly locate request errors and debug the code.
Directions
1. Copy the request ID (
RequestId) returned when the request error occurs.2. Click Diagnosis Tool.
3. Enter the RequestId to be diagnosed in the RequestId input box at the top, and click Start Diagnosis. Please wait patiently for a few minutes to view the corresponding intelligent diagnosis results.