Skip to content

Commit 9069daf

Browse files
authored
Export RestError, AbortError, TimeoutError, and ErrorResponse. (Azure#24373)
* Export RestError, AbortError, and TimeoutError * Convert ErrorResponse interface to class * Add Error handling section in README * Fix formatting Co-authored-by: Aman Rao <[email protected]>
1 parent 93bf82b commit 9069daf

File tree

7 files changed

+51
-4
lines changed

7 files changed

+51
-4
lines changed

sdk/cosmosdb/cosmos/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,36 @@ for (const city of resources) {
183183

184184
For more information on querying Cosmos DB databases using the SQL API, see [Query Azure Cosmos DB data with SQL queries][cosmos_sql_queries].
185185

186+
## Error Handling
187+
188+
The SDK generates various types of errors that can occur during an operation.
189+
190+
1. `ErrorResponse` is thrown if the response of an operation returns an error code of >=400.
191+
2. `TimeoutError` is thrown if Abort is called internally due to timeout.
192+
3. `AbortError` is thrown if any user passed signal caused the abort.
193+
4. `RestError` is thrown in case of failure of underlying system call due to network issues.
194+
5. Errors generated by any devDependencies. For Eg. `@azure/identity` package could throw `CredentialUnavailableError`.
195+
196+
Following is an example for handling errors of type `ErrorResponse`, `TimeoutError`, `AbortError`, and `RestError`.
197+
198+
```js
199+
try {
200+
// some code
201+
} catch (err) {
202+
if (err instanceof ErrorResponse) {
203+
// some specific error handling.
204+
} else if (err instanceof RestError) {
205+
// some specific error handling.
206+
}
207+
// handle other type of errors in similar way.
208+
else {
209+
// for any other error.
210+
}
211+
}
212+
```
213+
214+
It's important to properly handle these errors to ensure that your application can gracefully recover from any failures and continue functioning as expected. More details about some of these errors and their possible solutions can be found [here]<!--(https://learn.microsoft.com/azure/cosmos-db/nosql/conceptual-resilient-sdk-applications#should-my-application-retry-on-errors)-->.
215+
186216
## Troubleshooting
187217

188218
### General

sdk/cosmosdb/cosmos/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@
102102
"semaphore": "^1.0.5",
103103
"tslib": "^2.2.0",
104104
"universal-user-agent": "^6.0.0",
105-
"uuid": "^8.3.0"
105+
"uuid": "^8.3.0",
106+
"@azure/abort-controller": "^1.0.0"
106107
},
107108
"devDependencies": {
108109
"@azure/dev-tool": "^1.0.0",

sdk/cosmosdb/cosmos/review/cosmos.api.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@
77
/// <reference lib="dom" />
88
/// <reference lib="esnext.asynciterable" />
99

10+
import { AbortError } from '@azure/abort-controller';
1011
import { AbortSignal as AbortSignal_2 } from 'node-abort-controller';
1112
import { Pipeline } from '@azure/core-rest-pipeline';
13+
import { RestError } from '@azure/core-rest-pipeline';
1214
import { TokenCredential } from '@azure/core-auth';
1315

16+
export { AbortError }
17+
1418
// @public (undocumented)
1519
export interface Agent {
1620
// (undocumented)
@@ -704,7 +708,7 @@ export interface ErrorBody {
704708
}
705709

706710
// @public (undocumented)
707-
export interface ErrorResponse extends Error {
711+
export class ErrorResponse extends Error {
708712
// (undocumented)
709713
[key: string]: any;
710714
// (undocumented)
@@ -1552,6 +1556,8 @@ interface Response_2<T> {
15521556
}
15531557
export { Response_2 as Response }
15541558

1559+
export { RestError }
1560+
15551561
// @public
15561562
export interface RetryOptions {
15571563
fixedRetryIntervalInMilliseconds: number;
@@ -1858,6 +1864,13 @@ export class StoredProcedures {
18581864
// @public (undocumented)
18591865
export type SubStatusCode = number;
18601866

1867+
// @public (undocumented)
1868+
export class TimeoutError extends Error {
1869+
constructor(message?: string);
1870+
// (undocumented)
1871+
readonly code: string;
1872+
}
1873+
18611874
// @public
18621875
export class TimeSpan {
18631876
constructor(days: number, hours: number, minutes: number, seconds: number, milliseconds: number);

sdk/cosmosdb/cosmos/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,5 @@ export { ClientContext } from "./ClientContext";
8585
export { GlobalEndpointManager } from "./globalEndpointManager";
8686
export { SasTokenPermissionKind } from "./common/constants";
8787
export { createAuthorizationSasToken } from "./utils/SasToken";
88+
export { RestError } from "@azure/core-rest-pipeline";
89+
export { AbortError } from "@azure/abort-controller";

sdk/cosmosdb/cosmos/src/request/ErrorResponse.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export interface GroupByAliasToAggregateType {
5555
[key: string]: AggregateType;
5656
}
5757

58-
export interface ErrorResponse extends Error {
58+
export class ErrorResponse extends Error {
5959
code?: number;
6060
substatus?: number;
6161
body?: ErrorBody;

sdk/cosmosdb/cosmos/src/request/RequestHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ async function httpRequest(requestContext: RequestContext): Promise<{
106106
: undefined;
107107

108108
if (response.status >= 400) {
109-
const errorResponse: ErrorResponse = new Error(result.message);
109+
const errorResponse: ErrorResponse = new ErrorResponse(result.message);
110110

111111
logger.warning(
112112
response.status +

sdk/cosmosdb/cosmos/src/request/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ export { SharedOptions } from "./SharedOptions";
1818
export { StatusCode, SubStatusCode } from "./StatusCodes";
1919
export { FeedResponse } from "./FeedResponse";
2020
export { RequestContext } from "./RequestContext";
21+
export { TimeoutError } from "./TimeoutError";

0 commit comments

Comments
 (0)