|
| 1 | +# Error Handling |
| 2 | + |
| 3 | +The SDK provides a comprehensive error handling system that helps you handle different types of errors gracefully and get meaningful error information for debugging. |
| 4 | + |
| 5 | +## Error Types |
| 6 | + |
| 7 | +The SDK defines several specific error types that inherit from a base [`UiPathError`](https://uipath.github.io/uipath-typescript/api/classes/UiPathError/) class: |
| 8 | + |
| 9 | +### [`AuthenticationError`](https://uipath.github.io/uipath-typescript/api/classes/AuthenticationError/) |
| 10 | +Thrown when authentication fails (401 status codes). |
| 11 | + |
| 12 | +**Common scenarios:** |
| 13 | +- Invalid credentials |
| 14 | +- Expired token |
| 15 | +- Missing authentication |
| 16 | + |
| 17 | +```typescript |
| 18 | +import { AuthenticationError, isAuthenticationError } from '@uipath/uipath-typescript'; |
| 19 | + |
| 20 | +try { |
| 21 | + await sdk.initialize(); |
| 22 | +} catch (error) { |
| 23 | + if (isAuthenticationError(error)) { |
| 24 | + console.log('Authentication failed:', error.message); |
| 25 | + // Handle re-authentication |
| 26 | + } |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +### [`AuthorizationError`](https://uipath.github.io/uipath-typescript/api/classes/AuthorizationError/) |
| 31 | +Thrown when access is denied (403 status codes). |
| 32 | + |
| 33 | +**Common scenarios:** |
| 34 | +- Insufficient permissions |
| 35 | +- Access denied to specific folder |
| 36 | +- Scope limitations |
| 37 | + |
| 38 | +```typescript |
| 39 | +import { AuthorizationError, isAuthorizationError } from '@uipath/uipath-typescript'; |
| 40 | + |
| 41 | +try { |
| 42 | + const assets = await sdk.assets.getAll({ folderId: 12345 }); |
| 43 | +} catch (error) { |
| 44 | + if (isAuthorizationError(error)) { |
| 45 | + console.log('Access denied:', error.message); |
| 46 | + // Handle permission error |
| 47 | + } |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +### [`ValidationError`](https://uipath.github.io/uipath-typescript/api/classes/ValidationError/) |
| 52 | +Thrown when validation fails (400 status codes). |
| 53 | + |
| 54 | +**Common scenarios:** |
| 55 | +- Invalid input parameters |
| 56 | +- Missing required fields |
| 57 | +- Invalid data format |
| 58 | + |
| 59 | +```typescript |
| 60 | +import { ValidationError, isValidationError } from '@uipath/uipath-typescript'; |
| 61 | + |
| 62 | +try { |
| 63 | + await sdk.processes.start({ |
| 64 | + releaseKey: 'invalid-key' |
| 65 | + }); |
| 66 | +} catch (error) { |
| 67 | + if (isValidationError(error)) { |
| 68 | + console.log('Validation failed:', error.message); |
| 69 | + // Handle validation errors |
| 70 | + } |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +### [`NotFoundError`](https://uipath.github.io/uipath-typescript/api/classes/NotFoundError/) |
| 75 | +Thrown when requested resources are not found (404 status codes). |
| 76 | + |
| 77 | +**Common scenarios:** |
| 78 | +- Resource doesn't exist |
| 79 | +- Folder not found |
| 80 | +- Process not found |
| 81 | + |
| 82 | +```typescript |
| 83 | +import { NotFoundError, isNotFoundError } from '@uipath/uipath-typescript'; |
| 84 | + |
| 85 | +try { |
| 86 | + const asset = await sdk.assets.getById(99999); |
| 87 | +} catch (error) { |
| 88 | + if (isNotFoundError(error)) { |
| 89 | + console.log('Asset not found:', error.message); |
| 90 | + // Handle missing resource |
| 91 | + } |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +### [`RateLimitError`](https://uipath.github.io/uipath-typescript/api/classes/RateLimitError/) |
| 96 | +Thrown when rate limits are exceeded (429 status codes). |
| 97 | + |
| 98 | +**Common scenarios:** |
| 99 | +- Too many requests |
| 100 | +- API rate limiting |
| 101 | + |
| 102 | +```typescript |
| 103 | +import { RateLimitError, isRateLimitError } from '@uipath/uipath-typescript'; |
| 104 | + |
| 105 | +try { |
| 106 | + await sdk.assets.getAll(); |
| 107 | +} catch (error) { |
| 108 | + if (isRateLimitError(error)) { |
| 109 | + console.log('Rate limit exceeded:', error.message); |
| 110 | + // Implement retry logic with backoff |
| 111 | + } |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +### [`ServerError`](https://uipath.github.io/uipath-typescript/api/classes/ServerError/) |
| 116 | +Thrown when server errors occur (5xx status codes). |
| 117 | + |
| 118 | +**Common scenarios:** |
| 119 | +- Internal server error |
| 120 | +- Service unavailable |
| 121 | +- Gateway timeout |
| 122 | + |
| 123 | +```typescript |
| 124 | +import { ServerError, isServerError } from '@uipath/uipath-typescript'; |
| 125 | + |
| 126 | +try { |
| 127 | + await sdk.queues.getAll(); |
| 128 | +} catch (error) { |
| 129 | + if (isServerError(error)) { |
| 130 | + console.log('Server error:', error.message); |
| 131 | + // Handle server-side errors |
| 132 | + } |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +### [`NetworkError`](https://uipath.github.io/uipath-typescript/api/classes/NetworkError/) |
| 137 | +Thrown when network-related errors occur. |
| 138 | + |
| 139 | +**Common scenarios:** |
| 140 | +- Connection timeout |
| 141 | +- Request aborted |
| 142 | +- DNS resolution failure |
| 143 | +- Network connectivity issues |
| 144 | + |
| 145 | +```typescript |
| 146 | +import { NetworkError, isNetworkError } from '@uipath/uipath-typescript'; |
| 147 | + |
| 148 | +try { |
| 149 | + await sdk.processes.getAll(); |
| 150 | +} catch (error) { |
| 151 | + if (isNetworkError(error)) { |
| 152 | + console.log('Network error:', error.message); |
| 153 | + // Handle network issues |
| 154 | + } |
| 155 | +} |
| 156 | +``` |
| 157 | + |
| 158 | +## Error Information |
| 159 | + |
| 160 | +### Getting Error Details |
| 161 | +```typescript |
| 162 | +import { getErrorDetails } from '@uipath/uipath-typescript'; |
| 163 | + |
| 164 | +try { |
| 165 | + await sdk.assets.getAll(); |
| 166 | +} catch (error) { |
| 167 | + const details = getErrorDetails(error); |
| 168 | + console.log('Error message:', details.message); |
| 169 | + console.log('Status code:', details.statusCode); |
| 170 | +} |
| 171 | +``` |
| 172 | + |
| 173 | +### Accessing All Error Properties |
| 174 | +```typescript |
| 175 | +import { UiPathError } from '@uipath/uipath-typescript'; |
| 176 | + |
| 177 | +try { |
| 178 | + const process = await sdk.maestro.processes.getById('invalid-id'); |
| 179 | +} catch (error) { |
| 180 | + if (error instanceof UiPathError) { |
| 181 | + // Access common error properties |
| 182 | + console.log('Error Type:', error.type); |
| 183 | + console.log('Message:', error.message); |
| 184 | + console.log('Status Code:', error.statusCode); |
| 185 | + console.log('Request ID:', error.requestId); |
| 186 | + console.log('Timestamp:', error.timestamp); |
| 187 | + console.log('error stack trace:', error.stack); |
| 188 | + |
| 189 | + // Get detailed debug information including stack trace |
| 190 | + const debugInfo = error.getDebugInfo(); |
| 191 | + } |
| 192 | +} |
| 193 | +``` |
| 194 | + |
| 195 | +### Debug Information |
| 196 | +```typescript |
| 197 | +try { |
| 198 | + await sdk.processes.start({ releaseKey: 'test' }); |
| 199 | +} catch (error) { |
| 200 | + if (error instanceof UiPathError) { |
| 201 | + const debugInfo = error.getDebugInfo(); |
| 202 | + console.log('Debug info:', JSON.stringify(debugInfo, null, 2)); |
| 203 | + } |
| 204 | +} |
| 205 | +``` |
0 commit comments