When encountering an error with the API, the exception should contain information about the error to help the user understand it.
Any API error will do. For instance, let us give an invalid subscription key.
PermissionDenied (401): Access denied due to invalid subscription key or wrong API endpoint. Make sure to provide a valid key for an active subscription and use a correct regional API endpoint for your resource.
We can enable the logging of the HTTP requests for debugging purposes with the following instruction. See this HTTParty example for details.
Re-running the snippet above now prints the body of the response.
D, [2023-04-05T11:47:18.863532 #490911] DEBUG -- : [HTTParty] [2023-04-05 11:47:18 +0200] > GET https://eastus.api.cognitive.microsoft.com/speechtotext/v3.0/transcriptions
[HTTParty] [2023-04-05 11:47:18 +0200] > Headers:
[HTTParty] [2023-04-05 11:47:18 +0200] > Ocp-Apim-Subscription-Key: invalid
[HTTParty] [2023-04-05 11:47:18 +0200] > Content-Type: application/json
[HTTParty] [2023-04-05 11:47:18 +0200] >
[HTTParty] [2023-04-05 11:47:18 +0200] < HTTP/1.1 401
[HTTParty] [2023-04-05 11:47:18 +0200] < Content-length: 224
[HTTParty] [2023-04-05 11:47:18 +0200] < Content-type: application/json
[HTTParty] [2023-04-05 11:47:18 +0200] < Apim-request-id: da69ffd0-143f-480d-919f-17caa875031d
[HTTParty] [2023-04-05 11:47:18 +0200] < Date: Wed, 05 Apr 2023 09:47:18 GMT
[HTTParty] [2023-04-05 11:47:18 +0200] < Connection: close
[HTTParty] [2023-04-05 11:47:18 +0200] <
{"error":{"code":"401","message":"Access denied due to invalid subscription key or wrong API endpoint. Make sure to provide a valid key for an active subscription and use a correct regional API endpoint for your resource."}}
[HTTParty] [2023-04-05 11:47:18 +0200] <
We can see that the response contains a useful error message about the nature of the problem. This pattern is present in most (if not all) of the API routes, according to the reference.